Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Generating Musical Notes

This thread is locked; no one can reply to it. rss feed Print
Generating Musical Notes
Daniel McKinnon
Member #7,364
June 2006

Hi,

I am trying to make a very simple synthesizer program.

what I have done so far is created a 8-byte saw-wave sample

char saw[8]= { 0, 64, 128, 64, 0, -64, -128, -64 };

Set up the musical scale

       double scale[13] = { 440.00, 466.16, 493.88, 523.25, 554.37, 587.33, 622.25, 659.26, 698.46, 739.99, 783.99, 830.61, 880.00 };
       char note = 0;

initialized the sound

       reserve_voices( 1 );
       install_sound( DIGI_AUTODETECT, MIDI_NONE, 0 );

Create the sample

  s = create_sample( 8, 0, 1, 8 );
  memcpy( s->data, saw, sizeof(char)*8 );

Setup the voice to play the note on a scale

  v = allocate_voice( s );
  voice_set_volume( v, 255 );
  voice_set_frequency( v, scale[note] );
  voice_set_playmode( v, PLAYMODE_LOOP );
  voice_set_pan( v, 128 );

And then start up the voice

       voice_start( v );

This definitely doesn't provide the results I expected, and I am now at a loss as to how this is actually done.

I don't understand fine control of frequencies either. I have to change the frequency ( voice_set_frequency( int voice, int frequency ) ) by somewhere around 150 to change the sound, and that change is very course.

So I was hoping someone would enlighten me about music generation.

miran
Member #2,407
June 2002

1. Use AUDIOSTREAMs, not voices.

2. When you generate samples for the audiostream, the value of each sample is a function of the sample offset in the audiostream and frequency. For example when playing the middle A (440 Hz), every 440th sample will have a value of 0 and the samples will increase linearly from 0 to maximum amplitude for 440 samples. This way you make a simple saw signal.

3. You can mix several saw signals at slightly different frequencies to get a better sound.

4. Modulate frequency and amplitude for more variety...

--
sig used to be here

Johan Halmén
Member #1,550
September 2001

In your sample, one wave length is only 8 bytes. Wave lengths are generally hundreds of bytes. Try using longer samples and adjust everything to that.

If your sound generator works with 44 kHz and you want to play a sound of 440 Hz, one wave length would be exactly 100 bytes.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

miran
Member #2,407
June 2002

Oops yeah, that's what I meant in my first post under item #2. The wave length depends on the sampling frequency and the note frequency, so at 44 kHz a 440 Hz note is 100 samples...

--
sig used to be here

Go to: