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.
IndexProgramming Questions & HelpSound,  Music Libraries › Sonia LiveInput.signal[] values?
Page Index Toggle Pages: 1
Sonia LiveInput.signal[] values?? (Read 2004 times)
Sonia LiveInput.signal[] values??
Apr 10th, 2006, 5:56pm
 
LiveInput has an accessible data member signal[] that appears to contain the audio buffer data that is being used to compute the FFT that is contained in spectrum[].  Of course, accessing either array requires calling LiveInput.getSpectrum() first.

My questions:
1. What is the meaning of the signal[] array values?  
2. How many elements are there?  
3. In what order are they: most recent to oldest, vice-versa, circular buffer?  If circular, where's the pointer to the current value?

My guesses:  
1. 16bit signed values (max about 32768)
2. 2*X, where X is the value used in LiveInput.start(X)
3. No idea...

Please fill me in if you have better answers!  Thanks.
Re: Sonia LiveInput.signal[] values??
Reply #1 - Apr 11th, 2006, 1:57pm
 
1. The signal[] array contains the most recent sample data after calling LiveInput.getSignal(). For performance reasons it is not updated automatically, so you'll have to call the function explicitly (and repeatedly) BEFORE working with the array. The values are indeed 16bit signed, however stored as float.

2. The array has twice as many elements as you specified for the LiveInput.start(num) call. If no number was given, Sonia initializes with a default window size of 1024 bins (or 2048 samples). At 44.1kHz that equals chunks of approx. 46ms length...

3. Internally Sonia maintains a circular buffer. However, this is abstracted away from the user and the signal[] array will always only contain the X most recent samples (with signal[0] being the oldest of them)

hth!
Re: Sonia LiveInput.signal[] values??
Reply #2 - Apr 11th, 2006, 5:58pm
 
here is an example that stores the signal array from sonia in a float array while the sample is playing and saves a file with the signal data and the max signal value. the problem is... the data is inconsistent... if you run the program twice with the same sample you get different data. why?

import pitaru.sonia_v2_9.*;

Sample mySample;  
float[] signal;
boolean once = false;

void setup(){

 size(200,200);  
 ellipseMode(CENTER);
 rectMode(CENTER);
 noStroke();

 Sonia.start(this);
 LiveInput.start();
 mySample = new Sample("yoursample.wav");  
 mySample.play();  
 mySample.connectLiveInput(true);

 signal = new float[1];
 //LiveInput.useEqualizer(true);
 //LiveInput.useEnvelope(true,2.0);    
}

void draw(){
 background(255);

 if(mySample.isPlaying()){
   updateSignal();
 }
 else if(!mySample.isPlaying()){
   if(!once) {

     float max_signal = 0;
     String signal_str = "";

     for (int i=0; i < signal.length; i++) {
       max_signal = max(max_signal,signal[i]);
       signal_str += signal[i]+", ";
     }  

     signal_str += ", max_signal: "+max_signal;

     String[] signal_strs = split(signal_str, ",");
     saveStrings("signal.txt", signal_strs);
     
     println("signal saved");
   }
 }
}

void updateSignal() {  
 LiveInput.getSignal();
 LiveInput.getSpectrum();

 for(int i = 0; i < LiveInput.signal.length; i++){
   signal = append(signal,LiveInput.signal[i]);
 }
}


Re: Sonia LiveInput.signal[] values??
Reply #3 - Apr 12th, 2006, 3:08am
 
toxi - Thanks for the fabulous reply.  Is there more documentation on Sonia than what's available at sonia.pitaru.com?  Things like getSignal() and signal[] are not shown there... wondering what other Easter eggs are hiding?
Re: Sonia LiveInput.signal[] values??
Reply #4 - Apr 12th, 2006, 11:58am
 
mjm, you're welcome! AFAIK apart from the Sonia site and this forum there's not much more documentation available. My answers above were based on the v2.5 source code I've still got from when I helped Amit with the FFT parts. But I don't think the behaviour of signal[] has changed in more recent versions. I've written down a few more details about the FFT process over here.

Maybe we should petition Amit to release the library under an OpenSource license... Wink Btw. I've just read on his site he's looking for co-developers to help him build the next version, wink, wink...
Re: Sonia LiveInput.signal[] values??
Reply #5 - Apr 12th, 2006, 12:34pm
 
dj hype Smiley

i'm not surprised at all that your sketch is creating inconsistent results since you always only manage to read parts of the original sample at only more or less regular intervals...

You initialize LiveInput with the default setting, so the time window used is 2048 samples long (approx. 46 milliseconds).

Now, since you didn't specify any framerate Processing will attempt to run your sketch as fast as possible, meaning draw() is executed at unpredictable time intervals (probably faster than the 46ms per audio frame). Yet every draw frame you recalculate the spectrum as if it would be an entire new audio buffer and with the same assumption also add the latest signal to your array - with the result of you probably getting loads of duplicate (or missing) sample values, depending if your sketch is running faster or slower than realtime.

Am actually embarking on a similar tool than you and haven't really looked yet into how to solve this properly. But having had a brief look at the available Sonia methods I'd try to progress reading the sample "manually" via this method:

Code:
mySample.read(dataArray, firstDataFrame, firstSampleFrame, numFrames); 



You'd then need a audio frame counter variable and read and process a new frame at every draw() iteration. Hope that works, haven't tried it yet...
Re: Sonia LiveInput.signal[] values??
Reply #6 - Apr 12th, 2006, 3:57pm
 
well. seems the read method is the most accurate method for storing signal data, although why call

mySample.read(dataArray, firstDataFrame, firstSampleFrame, numFrames)

as opposed to?

mySample.read(dataArray)  

Re: Sonia LiveInput.signal[] values??
Reply #7 - Apr 13th, 2006, 5:34am
 
here is another quick test program which uses both versions of the read method. here the number of output values vary intensely. the frame counter works... im not sure how the second version of read works.

import pitaru.sonia_v2_9.*;

Sample mySample;  
float[] sample_data;
boolean once = false;

int current_frame, last_frame, frame_amount, frame_count;

void setup(){

 size(200,200);  
 ellipseMode(CENTER);
 rectMode(CENTER);
 noStroke();

 Sonia.start(this);
 LiveInput.start();
 mySample = new Sample("hello.wav");  
 mySample.setRate(10);
 mySample.play();  
 mySample.connectLiveInput(true);  

 framerate(100);

 current_frame = 0;
 last_frame = 0;
 frame_amount = mySample.getNumFrames();

 sample_data = new float[mySample.getNumFrames()];
 mySample.read(sample_data);

 String[] sample_data_strs = new String[mySample.getNumFrames()];

 for (int i=0; i < sample_data.length; i++) {
   sample_data_strs[i] = str(sample_data[i]);
 }

 saveStrings("sample_data_1.txt", sample_data_strs);

 sample_data = new float[mySample.getNumFrames()];
}

void draw(){
 background(255);

 if(mySample.isPlaying()){  
   if(last_frame != mySample.getCurrentFrame()) {
     last_frame = frame_count;
     frame_count++;
     println("the sample is currently on frame:" + frame_count);
     //mySample.read(sample_data, sample_data.length-1, frame_count, 1);
     mySample.read(sample_data, frame_count, frame_count, 1);
   }      
   //println("the sample is currently on frame:" + mySample.getCurrentFrame());
 }  
 else if(!mySample.isPlaying()){  
   if(!once) {
     String[] sample_data_strs = new String[mySample.getNumFrames()];

     for (int i=0; i < sample_data.length; i++) {
       sample_data_strs[i] = str(sample_data[i]);
     }

     saveStrings("sample_data_2.txt", sample_data_strs);
     println("SAMPLE DATA SAVED");
   }    
 }
}

Re: Sonia LiveInput.signal[] values??
Reply #8 - Apr 13th, 2006, 12:43pm
 
Since you are using liveInput - has the bug finally been fixed wich prevented the security popup window from being clicked? The last time I tested it it was still there. So I'm always using version 0090 whenever I need Sonia + LiveInput.

If it's not fixed yet - please, please, please could this bug be looked at anytime soon?
Re: Sonia LiveInput.signal[] values??
Reply #9 - Apr 23rd, 2006, 1:08am
 
Q-
Are you speaking of the dialog box that comes up when you try to run the applet in a browser?  It seems to just hang, forcing me to force-quit (Safari on OS X.4.6).  Maybe I'll try version 0090...
Page Index Toggle Pages: 1