All pastes #743463 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-10-20 13:45 UTC
rendered paste body
float freq = Float.parseFloat(args[0]);
Wave wave = new Wave(freq);

// audio format
AudioFormat format = new AudioFormat(44100f, 16, 2, true, false);
System.out.println("Sound Format: " + format);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

Mixer.Info [] mixerInfo = AudioSystem.getMixerInfo();
// buscamos un mixer que soporte el DataLine.Info
Mixer mixer = null;
// imprimimos todos los mixers
System.out.println("Mixers presentes:");
for (int i = 0; i < mixerInfo.length; i++) {
	mixer = AudioSystem.getMixer(mixerInfo[i]);
	System.out.println("Mixer: " + mixerInfo[i].getName() + ": " + mixerInfo[i].getDescription());
}
		
SourceDataLine line = null;
for (int i = 0; line == null && i < mixerInfo.length; i++) {
	mixer = AudioSystem.getMixer(mixerInfo[i]);
	try {
		line = (SourceDataLine) mixer.getLine(info);
	} catch (LineUnavailableException e) {
		line = null;
	} catch (IllegalArgumentException e) {
		line = null;
	}
}
		
if (line == null) {
	System.out.println("Couldn't find a line");
	System.exit(1);
} else {
	Mixer.Info info2 = mixer.getMixerInfo();
	System.out.println("Line found in mixer " + info2.getName() + ": " + info2.getDescription());
	System.out.println();
}
		
// Obtain and open the line.
try {
	//line.open();
	line.open(format);
	line.start();
} catch (LineUnavailableException ex) {
	ex.printStackTrace();
	System.exit(1);
}

System.out.println("Listo para sacar sonido!");
byte[] sample = new byte[4 * 44100 * 100];
for (int i = 0; i < sample.length / 4; i++) {
	int valor = (int) ((2 << 15 - 1) * wave.height());
	//System.out.println(valor);
	// parte inferior
	byte parte = (byte) (valor & 0xff);
	sample[i * 4] = parte;
	sample[i*4 + 2] = parte;
	// parte superior
	parte = (byte) (valor >> 8);
	sample[i * 4 + 1] = parte;
	sample[i*4 + 3] = parte;
	wave.tic();
}
		
int escritos = 0;
escritos = line.write(sample, 0, sample.length);
if (escritos != sample.length) {
	System.err.println("No se escribieron todos los bytes ("
			+ escritos + ")!");
	System.exit(1);
} else {
	System.out.println("Bytes escritos: " + escritos);
}
line.drain();
line.close();