我试图写一个声音输出,可以支持以下格式
无符号8位pcm
有符号16位pcm
有符号24位pcm
有符号32位浮点pcm
有符号32位pcm和
有符号64位pcm
我可以输出前两个,但其余的都有一个例外
一段时间以来,我一直在使用这段代码的大部分输出wave文件,但我刚刚开始直接输出到音频。
package sound.streamer;
import effects.AdsrEnvelope;
import effects.AmpLimiter;
import effects.Tremulant;
import effects.Vibrato;
import java.nio.ByteBuffer;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioFormat.Encoding;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import music.note.NoteRange;
import sound.generator.waveforms.*;
/**
*
* @author Edward Jenkins
*/
public class SoundOutputter {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
NoteRange noteRange = new NoteRange("C-1", "C9");
double frequency = noteRange.getFrequency("C4");
int sampleRate = 44100;
int bitRate = 32;
boolean signed;
boolean isFloat = false;
Encoding encoding;
int bytesPerSample = bitRate / 8;
SourceDataLine sdl;
byte[] outputBytes;
if (bitRate == 8) {
signed = false;
encoding = new Encoding("PCM_UNSIGNED");
} else {
if (isFloat) {
encoding = new Encoding("PCM_FLOAT");
} else {
signed = true;
encoding = new Encoding("PCM_SIGNED");
}
}
// harmoncis
double[] harmonicVolumes = {1, 1, 0, 1};
PwmPulseGenerator asg = new PwmPulseGenerator(frequency, 5);
AdsrEnvelope ae = new AdsrEnvelope(0.5, 0.3, 0.5, true, 0.8, 1, sampleRate);
Vibrato v = new Vibrato(4, 0.5, frequency, 1, sampleRate);
Tremulant t = new Tremulant(7, 0.25, 0.5, sampleRate, false);
int sampleLength = asg.getSampleLength();
AmpLimiter al = new AmpLimiter(bitRate, isFloat);
AudioFormat af = new AudioFormat(encoding, (float) sampleRate, bitRate,
1, bytesPerSample, sampleRate, true);
try {
sdl = AudioSystem.getSourceDataLine(af);
double point;
double[] points = new double[sampleLength];
double pointVolume;
sdl.open(af, 44100);
sdl.start();
for (int exportIndex = 0; exportIndex < sampleLength; exportIndex++) {
if (exportIndex == sampleRate * 4) {
ae.setSustain(false);
}
//asg.setFrequency(v.getVibratoFrequency(exportIndex));
point = asg.generateWaveformPoint();
pointVolume = ae.getADSRvolume(exportIndex);
//pointVolume *= t.drawTremulantPoint(pointVolume, ae.getSustainStartPoint());
asg.setAmplitude(pointVolume);
points[exportIndex] = point;
points[exportIndex] = al.calculateAmpLimit(points[exportIndex]);
outputBytes = convertToBytes(bitRate, isFloat, points[exportIndex]);
sdl.write(outputBytes, 0, outputBytes.length);
}
sdl.drain();
sdl.stop();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
public static byte[] convertToBytes(int bitRate, boolean isFloat, double point) {
byte[] output = {64};
ByteBuffer buffer;
// switch on bitrate
switch (bitRate) {
case 64:
buffer = ByteBuffer.allocate(8);
buffer.putDouble(point);
output = buffer.array();
break;
case 32:
if (isFloat) {
buffer = ByteBuffer.allocate(4);
buffer.putFloat((float)point);
output = buffer.array();
} else {
buffer = ByteBuffer.allocate(4);
buffer.putInt((int)point);
output = buffer.array();
}
break;
case 16:
buffer = ByteBuffer.allocate(2);
buffer.putShort((short)point);
output = buffer.array();
break;
case 8:
point = (int)point + 128;
output = new byte[1];
output[0] = (byte)point;
break;
}
return output;
}
}
1条答案
按热度按时间flvtvl501#
我很难找到支持哪些音频格式的文档。据我所知,java不支持24位或32位格式。也许javafx会?
好的,我找到了这个Java8文档,java声音技术,它说只支持8位和16位。
我一点也不知道该怎么做。如果你知道如何写正确的wav头之类的东西,这是可能的。上一次我看wav规范时,它让我头痛。不会再去那里了。