Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/media/sound/AudioFileSoundbankReader.java
38924 views
1
/*
2
* Copyright (c) 2007, 2013, 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
public Soundbank getSoundbank(URL url)
49
throws InvalidMidiDataException, IOException {
50
try {
51
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
52
Soundbank sbk = getSoundbank(ais);
53
ais.close();
54
return sbk;
55
} catch (UnsupportedAudioFileException e) {
56
return null;
57
} catch (IOException e) {
58
return null;
59
}
60
}
61
62
public Soundbank getSoundbank(InputStream stream)
63
throws InvalidMidiDataException, IOException {
64
stream.mark(512);
65
try {
66
AudioInputStream ais = AudioSystem.getAudioInputStream(stream);
67
Soundbank sbk = getSoundbank(ais);
68
if (sbk != null)
69
return sbk;
70
} catch (UnsupportedAudioFileException e) {
71
} catch (IOException e) {
72
}
73
stream.reset();
74
return null;
75
}
76
77
public Soundbank getSoundbank(AudioInputStream ais)
78
throws InvalidMidiDataException, IOException {
79
try {
80
byte[] buffer;
81
if (ais.getFrameLength() == -1) {
82
ByteArrayOutputStream baos = new ByteArrayOutputStream();
83
byte[] buff = new byte[1024
84
- (1024 % ais.getFormat().getFrameSize())];
85
int ret;
86
while ((ret = ais.read(buff)) != -1) {
87
baos.write(buff, 0, ret);
88
}
89
ais.close();
90
buffer = baos.toByteArray();
91
} else {
92
buffer = new byte[(int) (ais.getFrameLength()
93
* ais.getFormat().getFrameSize())];
94
new DataInputStream(ais).readFully(buffer);
95
}
96
ModelByteBufferWavetable osc = new ModelByteBufferWavetable(
97
new ModelByteBuffer(buffer), ais.getFormat(), -4800);
98
ModelPerformer performer = new ModelPerformer();
99
performer.getOscillators().add(osc);
100
101
SimpleSoundbank sbk = new SimpleSoundbank();
102
SimpleInstrument ins = new SimpleInstrument();
103
ins.add(performer);
104
sbk.addInstrument(ins);
105
return sbk;
106
} catch (Exception e) {
107
return null;
108
}
109
}
110
111
public Soundbank getSoundbank(File file)
112
throws InvalidMidiDataException, IOException {
113
try {
114
AudioInputStream ais = AudioSystem.getAudioInputStream(file);
115
ais.close();
116
ModelByteBufferWavetable osc = new ModelByteBufferWavetable(
117
new ModelByteBuffer(file, 0, file.length()), -4800);
118
ModelPerformer performer = new ModelPerformer();
119
performer.getOscillators().add(osc);
120
SimpleSoundbank sbk = new SimpleSoundbank();
121
SimpleInstrument ins = new SimpleInstrument();
122
ins.add(performer);
123
sbk.addInstrument(ins);
124
return sbk;
125
} catch (UnsupportedAudioFileException e1) {
126
return null;
127
} catch (IOException e) {
128
return null;
129
}
130
}
131
}
132
133