问题描述
- 无符号 8 位 PCM
- 有符号的 16 位 PCM
- 有符号的 24 位 PCM
- 有符号的 32 位浮点 PCM
- 签名的 32 位 PCM 和
- 签名的 64 位 PCM
我使用这段代码的大部分输出波形文件已经有一段时间了,但我刚刚开始直接输出到音频。
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,1};
PwmpulseGenerator asg = new PwmpulseGenerator(frequency,5);
AdsrEnvelope ae = new AdsrEnvelope(0.5,0.3,0.5,true,0.8,sampleRate);
Vibrato v = new Vibrato(4,frequency,sampleRate);
Tremulant t = new Tremulant(7,0.25,sampleRate,false);
int sampleLength = asg.getSampleLength();
AmpLimiter al = new AmpLimiter(bitRate,isFloat);
AudioFormat af = new AudioFormat(encoding,(float) sampleRate,bitRate,bytesPerSample,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,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;
}
}
解决方法
我无法找到有关支持哪些音频格式的文档。据我所知,Java 不支持 24 位或 32 位格式。也许 JavaFX 可以?
好的,我发现这个 Java 8 文档 Java Sound Technology 说只支持 8 位和 16 位。
我不知道如何自己动手。如果您知道如何编写正确的 wav 标头等,那可能是可能的。上次我看 wav 规范时,它让我头疼。不会再去了。