Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/java.desktop/share/classes/com/sun/media/sound/AudioFileSoundbankReader.java
66646 views
1
/*
2
* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
package com.sun.media.sound;
26
27
import java.io.ByteArrayOutputStream;
28
import java.io.DataInputStream;
29
import java.io.File;
30
import java.io.IOException;
31
import java.io.InputStream;
32
import java.net.URL;
33
34
import javax.sound.midi.InvalidMidiDataException;
35
import javax.sound.midi.Soundbank;
36
import javax.sound.midi.spi.SoundbankReader;
37
import javax.sound.sampled.AudioInputStream;
38
import javax.sound.sampled.AudioSystem;
39
import javax.sound.sampled.UnsupportedAudioFileException;
40
41
/**
42
* Soundbank reader that uses audio files as soundbanks.
43
*
44
* @author Karl Helgason
45
*/
46
public final class AudioFileSoundbankReader extends SoundbankReader {
47
48
@Override
49
public Soundbank getSoundbank(URL url)
50
throws InvalidMidiDataException, IOException {
51
try {
52
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
53
Soundbank sbk = getSoundbank(ais);
54
ais.close();
55
return sbk;
56
} catch (UnsupportedAudioFileException e) {
57
return null;
58
} catch (IOException e) {
59
return null;
60
}
61
}
62
63
@Override
64
public Soundbank getSoundbank(InputStream stream)
65
throws InvalidMidiDataException, IOException {
66
stream.mark(512);
67
try {
68
AudioInputStream ais = AudioSystem.getAudioInputStream(stream);
69
Soundbank sbk = getSoundbank(ais);
70
if (sbk != null)
71
return sbk;
72
} catch (UnsupportedAudioFileException e) {
73
} catch (IOException e) {
74
}
75
stream.reset();
76
return null;
77
}
78
79
public Soundbank getSoundbank(AudioInputStream ais)
80
throws InvalidMidiDataException, IOException {
81
int MEGABYTE = 1048576;
82
int DEFAULT_BUFFER_SIZE = 65536;
83
int MAX_FRAME_SIZE = 1024;
84
try {
85
byte[] buffer;
86
int frameSize = ais.getFormat().getFrameSize();
87
if (frameSize <= 0 || frameSize > MAX_FRAME_SIZE) {
88
throw new InvalidMidiDataException("Formats with frame size "
89
+ frameSize + " are not supported");
90
}
91
92
long totalSize = ais.getFrameLength() * frameSize;
93
if (totalSize >= Integer.MAX_VALUE - 2) {
94
throw new InvalidMidiDataException(
95
"Can not allocate enough memory to read audio data.");
96
}
97
98
if (ais.getFrameLength() == -1 || totalSize > MEGABYTE) {
99
ByteArrayOutputStream baos = new ByteArrayOutputStream();
100
byte[] buff = new byte[DEFAULT_BUFFER_SIZE - (DEFAULT_BUFFER_SIZE % frameSize)];
101
int ret;
102
while ((ret = ais.read(buff)) != -1) {
103
baos.write(buff, 0, ret);
104
}
105
ais.close();
106
buffer = baos.toByteArray();
107
} else {
108
buffer = new byte[(int) totalSize];
109
new DataInputStream(ais).readFully(buffer);
110
}
111
ModelByteBufferWavetable osc = new ModelByteBufferWavetable(
112
new ModelByteBuffer(buffer), ais.getFormat(), -4800);
113
ModelPerformer performer = new ModelPerformer();
114
performer.getOscillators().add(osc);
115
116
SimpleSoundbank sbk = new SimpleSoundbank();
117
SimpleInstrument ins = new SimpleInstrument();
118
ins.add(performer);
119
sbk.addInstrument(ins);
120
return sbk;
121
} catch (Exception e) {
122
return null;
123
}
124
}
125
126
@Override
127
public Soundbank getSoundbank(File file)
128
throws InvalidMidiDataException, IOException {
129
try {
130
AudioInputStream ais = AudioSystem.getAudioInputStream(file);
131
ais.close();
132
ModelByteBufferWavetable osc = new ModelByteBufferWavetable(
133
new ModelByteBuffer(file, 0, file.length()), -4800);
134
ModelPerformer performer = new ModelPerformer();
135
performer.getOscillators().add(osc);
136
SimpleSoundbank sbk = new SimpleSoundbank();
137
SimpleInstrument ins = new SimpleInstrument();
138
ins.add(performer);
139
sbk.addInstrument(ins);
140
return sbk;
141
} catch (UnsupportedAudioFileException e1) {
142
return null;
143
} catch (IOException e) {
144
return null;
145
}
146
}
147
}
148
149