We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
sound (Read 2325 times)
sound
Mar 29th, 2007, 7:26am
 

I am trying to synthesize sound using a byte array.

Here are some snippets:

import javax.sound.sampled.*;
import java.io.*;

AudioFormat audioFormat;
AudioInputStream audioInputStream;
SourceDataLine sourceDataLine;

//variables that set the audio format
float sampleRate = 16000.0F; //allowable: 8000, 16000, 22050, 44100
int sampleSizeInBits = 16; //allowable: 8, 16.
int channels = 1; //allowable: 1 or 2 (mono or stereo)
int bytesPerSamp = 2 * channels; //each channel requires 2 bytes per sample.
boolean signed = true;
boolean bigEndian = false;

int duration = 8;

//changing the size of the array audioData alters the length of the audio
byte audioData[] = new byte[ 16000 * bytesPerSamp * duration ];
int byteLength  = audioData.length;  
double sampLength = byteLength / bytesPerSamp;


<lots of code>


InputStream byteArrayInputStream = new ByteArrayInputStream(audioData);
audioFormat = new AudioFormat(
sampleRate, sampleSizeInBits, channels, signed, bigEndian);  
audioInputStream = new AudioInputStream(
byteArrayInputStream, audioFormat,
audioData.length / audioFormat.getFrameSize() );


DataLine.Info dataLineInfo = new DataLine.Info( SourceDataLine.class, audioFormat);
sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);

<start a thread, etc.>


I get an error message about the DataLine.Info...

Same happens when I import Ess.

perhaps there is an easier way of doing this??????

Hector
Re: sound
Reply #1 - Mar 29th, 2007, 4:21pm
 
I ran into the same problem when trying to use the JavaSound API in Processing. It seemed like the problem was that the preprocessor didn't understand what SourceDataLine.class meant. I don't think Ess supports playing a sound using your own byte array and neither does the audio library I just released (see this thread).

What you could try is putting all of the code that uses the JavaSound API into a separate tab and giving it a .java extension. I think if you do that the preprocessor won't mess with it. But it also means you'll have to wrap all of your audio code into a class of some kind.

The other thing you could do is not use a byte array. Just use a float array and then copy it into the samples array of an Ess AudioChannel. What kind of sound are you trying to sythesize
Re: sound
Reply #2 - Mar 30th, 2007, 3:14am
 
Thanks for your response. I will stop trying to use a byte array, then.

I am not really after any particular kind of sound, just testing the possibilities of Processing in this area. Your suggestion about using a float array and then copying it to a samples array is possible. Can I do the same with your audio library? I will have a closer look at it this weekend.  
Re: sound
Reply #3 - Mar 30th, 2007, 4:43am
 
You can, just not in the same way. Check out the docs for AudioOutput and AudioSignal.
Re: sound
Reply #4 - May 1st, 2007, 5:30am
 
I ran into this problem earlier today. Use it this way because it is looking for an object of type 'Class':

DataLine.Info dataLineInfo = new DataLine.Info(
 Class.forName("javax.sound.sampled.SourceDataLine"),
 audioFormat);
Page Index Toggle Pages: 1