table of contents
roartut(7) | System Manager's Manual: RoarAuido | roartut(7) |
NAME¶
roartut - RoarAudio sound library developer tutorialDESCRIPTION¶
This tutorial descipes some basics with working with libroar. We will create a simple application that can play a file and one that can play some sines. A lot of other examples can be found in RoarAudio's sources in the roarclients directory.PLAYING A FILE¶
Playing back a file is a easy task with libroar. The VS API has some special support to play back files in a very simple way. This is shown here.#include <roaraudio.h> /* libroar */
roar_vs_t * vss;
int err; /* see later */
- server address
- This is the address of the server. In general case This
should be set to NULL.
- program name
- This is the name of our program. This should be set to some
name the user will recognize like "some App", "some
Game". It should not contain the filename of the process like
"/usr/bin/someapp.bin".
- file name
- This is the name of the file we want to play. In fact this
is a URL. VS API uses so called DSTR API to open files. DSTR API supports
local files as well as for example HTTP. Examples include:
"somefile.ogg", "file:///data/bla.wav",
"http://radiostation.org:8000/bla.ogg".
- error var
- This is a pointer to a int used to store the error value in
case of error. This can be set to NULL but should not. The function
roar_vs_strerr(3) can be used to get a lion readable string of the
error.
vss = roar_vs_new_from_file(NULL, "some App", "somefile.ogg", &err);
if ( vss == NULL ) {
roar_vio_printf(roar_stderr, "Error: Can not connect to server: %s0, roar_vs_strerr(err));
return 1;
}
if ( roar_vs_run(vss, &err) == -1 ) {
roar_vio_printf(roar_stderr, "Error: can not loop: %s0, roar_vs_strerr(err));
}
if ( roar_vs_close(vss, ROAR_VS_FALSE, &err) == -1 ) {
roar_vio_printf(roar_stderr, "Error: Can not close connection to server: %s0, roar_vs_strerr(err));
return 1;
}
//vsfile.c:
#include <roaraudio.h>
int main (void) {
roar_vs_t * vss;
int err; /* see later */
vss = roar_vs_new_from_file(NULL, "some App", "somefile.ogg", &err);
if ( vss == NULL ) {
roar_vio_printf(roar_stderr, "Error: Can not connect to server: %s0, roar_vs_strerr(err));
return 1;
}
if ( roar_vs_run(vss, &err) == -1 ) {
roar_vio_printf(roar_stderr, "Error: can not loop: %s0, roar_vs_strerr(err));
}
if ( roar_vs_close(vss, ROAR_VS_FALSE, &err) == -1 ) {
roar_vio_printf(roar_stderr, "Error: Can not close connection to server: %s0, roar_vs_strerr(err));
return 1;
}
return 0;
}
//ll
cc -o vsfile vsfile.c `roar-config --libs --cflags`
PLAYING A SINE¶
Now we want to write a application playing a sine for some secs. We start the same way by including the correct header files:#include <math.h> /* sin() */
#include <roaraudio.h> /* libroar */
int rate = ROAR_RATE_DEFAULT;
int bits = 16;
int channels = 1; /* mono */
int codec = ROAR_CODEC_DEFAULT;
float freq = 523.2; /* middle C */
float step = M_PI*2*freq/rate; /* how much time per sample we have to encode ... */
float t = 0; /* current time */
float length = 5; /* 5 sec */
int16_t out[1024];
int i;
roar_vs_t * vss;
int err;
- server address
- Same as above.
- program name
- same as above.
- sample rate
- The number of audio frames per sec.
- channels
- The number of samples (one per channel) per audio frame.
- codec
- The codec to be used. This is one of ROAR_CODEC_*. In our
case we use ROAR_CODEC_DEFAULT which is signed PCM in CPU native format.
- bits
- The number of bits per sample.
- error var
- same as above.
vss = roar_vs_new_playback(NULL, "vssin", rate, channels, codec, bits, &err);
if ( vss == NULL ) {
roar_vio_printf(roar_stderr, "Error: Can not connect to server: %s0, roar_vs_strerr(err));
return 1;
}
while (t < 2*M_PI*freq*length) {
}
for (i = 0; i < (sizeof(out)/sizeof(*out)); i++) {
out[i] = 32767.f*sin(t);
t += step;
}
if ( roar_vs_write(vss, out, sizeof(out), &err) == -1 ) {
roar_vio_printf(roar_stderr, "Error: Can not write audio data to server: %s0, roar_vs_strerr(err));
break;
}
if ( roar_vs_close(vss, ROAR_VS_FALSE, &err) == -1 ) {
roar_vio_printf(roar_stderr, "Error: Can not close connection to server: %s0, roar_vs_strerr(err));
return 1;
}
//vssin.c:
#include <roaraudio.h>
#include <math.h>
int main (void) {
roar_vs_t * vss;
int rate = ROAR_RATE_DEFAULT;
int bits = 16;
int channels = 1; /* mono */
int codec = ROAR_CODEC_DEFAULT;
float freq = 523.2; /* middle C */
float step = M_PI*2*freq/rate; /* how much time per sample we have to encode ... */
float t = 0; /* current time */
float length = 5; /* 5 sec */
int16_t out[1024];
size_t i;
int err;
vss = roar_vs_new_playback(NULL, "vssin", rate, channels, codec, bits, &err);
if ( vss == NULL ) {
roar_vio_printf(roar_stderr, "Error: Can not connect to server: %s0, roar_vs_strerr(err));
return 1;
}
while (t < 2*M_PI*freq*length) {
for (i = 0; i < (sizeof(out)/sizeof(*out)); i++) {
out[i] = 32768.f*sin(t);
t += step;
}
if ( roar_vs_write(vss, out, sizeof(out), &err) == -1 ) {
roar_vio_printf(roar_stderr, "Error: Can not write audio data to server: %s0, roar_vs_strerr(err));
break;
}
}
if ( roar_vs_close(vss, ROAR_VS_FALSE, &err) == -1 ) {
roar_vio_printf(roar_stderr, "Error: Can not close connection to server: %s0, roar_vs_strerr(err));
return 1;
}
return 0;
}
//ll
cc -o vssin vssin.c -lm `roar-config --libs --cflags`
SEE ALSO¶
roar-config(1), roarcat(1), libroar(7). RoarAudio(7).November 2010 | RoarAudio |