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 › OpenAL - synthesized sound / signal processsing
Page Index Toggle Pages: 1
OpenAL - synthesized sound / signal processsing (Read 2277 times)
OpenAL - synthesized sound / signal processsing
Nov 1st, 2009, 1:12pm
 
Hi, i've been messing around with the toxi.audio library to provide 3D sound to a sketch, and I was wondering if anyone knew if it was possible to dynamically fill the AudioBuffers with generated signals like sine waves etc. I can see a function -

ALuint alutCreateBufferWaveform (ALenum waveshape, ALfloat frequency, ALfloat phase, ALfloat duration);

in the openAL documentation, but am not sure if there is similar thing in toxi.audio? or maybe there is a different way of doing it?

Any help/pointers would be much appreciated.
Re: OpenAL - synthesized sound / signal processsing
Reply #1 - Nov 4th, 2009, 5:42am
 
Hey,

my library is only a wrapper around JOAL and does provide some additional management tasks and tie it in with the Vec3D class, so you can apply the usual vector maths directly on the sound sources...

Because it is a wrapper only, you can still do everything JOAL offers. You can get a direct handle to the AL instance like this:

Code:
AL al = JOALUtil.getInstance().getAL(); 



Having said this, I'm not using the ALut class at all and looking at the Javadocs for JOAL it doesn't list that function you're mentioning above, so I guess there's a discrepancy between the C implementation of ALut and the JOAL one. However, you could have a look at the C version of the source and figure out the actual AL calls it does to populate the buffer.

Using the AudioBuffer class from my audioutils, you could synthesize a waveform with other outside means (e.g. the wave generators in toxiclibscore) and then convert these into the PWM format used by WAV files and then set this as buffer data:

Code:
import java.nio.*;

import toxi.math.waves.*;
import toxi.geom.*;
import toxi.math.*;

import toxi.audio.*;

int SAMPLE_FREQ=44100;

JOALUtil audio;
AudioBuffer buffer;
AudioSource source;

void setup() {
 size(100,100);
 // create an array for stereo samples
 byte[] sample=new byte[SAMPLE_FREQ*2];
 // calculate the base frequency in radians
 float freq=AbstractWave.hertzToRadians(220,SAMPLE_FREQ);
 // create an oscillator for the left channel
 AbstractWave osc=new FMSawtoothWave(0,freq,127,128);
 // and another for the right one (at 1/2 the base freq)
 AbstractWave osc2=new SineWave(0,freq/2,127,128);
 // populate the sample buffer
 for(int i=0; i<sample.length; i+=2) {
   sample[i]=(byte)osc.update();
   sample[i+1]=(byte)osc2.update();
 }
 // init the audio library
 audio=JOALUtil.getInstance();
 audio.init();
 // allocate a buffer via JOAL
 buffer=audio.generateBuffers(1)[0];
 // set the computed sample as buffer data
 buffer.configure(ByteBuffer.wrap(sample),AudioBuffer.FORMAT_STEREO8,SAMPLE_FREQ);
 // create a sound source, enable looping & play it
 source=audio.generateSource();
 source.setBuffer(buffer);
 source.setLooping(true);
 source.setReferenceDistance(width/2);
 source.play();
}


This example will be part of the upcoming release. I still have to do more testing in regards to 16bit samples, but the principle works...

Hth!
Re: OpenAL - synthesized sound / signal processsing
Reply #2 - Nov 4th, 2009, 7:03am
 
Here's another a bit more advanced example using oscillator cross-fading, frequency modulation and delay:

Code:
import java.nio.*;

import toxi.math.waves.*;
import toxi.geom.*;
import toxi.math.*;

import toxi.audio.*;

int SAMPLE_FREQ=44100;

int delayTime=11025;
float feedback = 0.9;

JOALUtil audio;
AudioBuffer buffer;
AudioSource source;

void setup() {
 size(100,100);
 // create an array for stereo samples
 byte[] sample=new byte[SAMPLE_FREQ*2*16];
 float[] raw=new float[sample.length];
 // calculate the base frequency in radians
 float freq=AbstractWave.hertzToRadians(110,SAMPLE_FREQ);
 // create an square wave oscillator for the left channel
 AbstractWave osc=new FMHarmonicSquareWave(0,freq,1,0);
 ((FMHarmonicSquareWave)osc).maxHarmonics=8;
 // and a frequency modulated sinewave for the right one (at 1/2 the base freq)
 AbstractWave osc2=new FMSineWave(0,freq*2,new SineWave(0,AbstractWave.hertzToRadians(1/16.0,SAMPLE_FREQ)));
 // another wave to be used for cross fading the 2 oscillators
 AbstractWave xfade=new SineWave(0,AbstractWave.hertzToRadians(1,SAMPLE_FREQ),0.5,0.5);
 // populate the sample buffer
 for(int i=0; i<raw.length; i+=2) {
   float x=xfade.update();
   float a=osc.update();
   float b=osc2.update();
   // calculate crossfaded contributions for each channel
   raw[i]=a+(b-a)*x;
   raw[i+1]=b+(a-b)*x;
   // if index > delay time, apply delay/echo effect
   if (i>delayTime) {
     raw[i]=raw[i]*(1-feedback)+raw[i-delayTime]*feedback;
     raw[i+1]=raw[i+1]*(1-feedback)+raw[i-delayTime+1]*feedback;
   }
 }
 // convert into 8bit stereo PCM
 for(int i=0; i<sample.length; i++) {
   sample[i]=(byte)(raw[i]*127+128);
 }
 // init the audio library
 audio=JOALUtil.getInstance();
 audio.init();
 // allocate a buffer via JOAL
 buffer=audio.generateBuffers(1)[0];
 // set the computed sample as buffer data
 buffer.configure(ByteBuffer.wrap(sample),AudioBuffer.FORMAT_STEREO8,SAMPLE_FREQ);
 // create a sound source, enable looping & play it
 source=audio.generateSource();
 source.setBuffer(buffer);
 source.setLooping(true);
 source.play();
}


Much fun to be had... E.g. try adjusting the delayTime and feedback (if feedback goes >1.0 than it gets super glitchy)
Page Index Toggle Pages: 1