OpenAL is a very powerful library for audio playback and recording. However, I just needed a minimal example on how to play a simple sine wave of a given frequency, synthesized by myself. This is basically quite easy using OpenAL as well. In a syntax similar to OpenGL, a buffer is generated and filled with samples (here a sine wave), which is then passed on to OpenAL for playback.
I've attached the sample code to this post. Now, this can be used to do fun stuff, like superimposing several sine (or, sawtooth!) waves, maybe add a LFO, some ASDR enveloping and build your own synthesizer from scratch. Though, if you like to code your sounds, you might want to take a look at ChucK, the audio programming language.
sine.cpp
2011-03-16 14:27
1 Comments:
anonymous wrote
Comment on this?
2011-07-19 21:02
I was wondering if you could help me. I am trying to write a program that will generate a certain wave given certain parameters. I have written a code but the parameters that are supposed to produce a sine wave are not producing one so I feel that my code is wrong. Would you be so kind as to take a look at it. Any help would be greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
#include <math.h>
main (int argc, char *argv[])
{
int i;
int err;
int t;
int buf[128];
snd_pcm_t *playback_handle;
snd_pcm_hw_params_t *hw_params;
if ((err = snd_pcm_open (&playback_handle, argv[1], SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
fprintf (stderr, "cannot open audio device %s (%s)\n",
argv[1],
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_any (playback_handle, hw_params)) < 0) {
fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_access (playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
fprintf (stderr, "cannot set access type (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_format (playback_handle, hw_params, SND_PCM_FORMAT_S32_LE)) < 0) {
fprintf (stderr, "cannot set sample format (%s)\n",
snd_strerror (err));
exit (1);
}
int rate = 96000;
if ((err = snd_pcm_hw_params_set_rate_near (playback_handle, hw_params, &rate, 0)) < 0) {
fprintf (stderr, "cannot set sample rate (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_channels (playback_handle, hw_params, 2)) < 0) {
fprintf (stderr, "cannot set channel count (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params (playback_handle, hw_params)) < 0) {
fprintf (stderr, "cannot set parameters (%s)\n",
snd_strerror (err));
exit (1);
}
snd_pcm_hw_params_free (hw_params);
if ((err = snd_pcm_prepare (playback_handle)) < 0) {
fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
snd_strerror (err));
exit (1);
}
for (t=0; t<128; t++ ) {
buf[t] = 0.000001 * sin( 2 * 3.14159 *11 / 128 * t ) ;
}
while( 1 ) {
if ((err = snd_pcm_writei (playback_handle, buf, 128)) != 128) {
fprintf (stderr, "write to audio interface failed (%s)\n",
snd_strerror (err));
exit (1);
}
}
snd_pcm_close (playback_handle);
exit (0);
}