1 | .\" RoarAudio |
---|
2 | .TH "roartut" "7" "July 2008" "RoarAudio" "System Manager's Manual: RoarAuido" |
---|
3 | |
---|
4 | .SH NAME |
---|
5 | roartut \- RoarAudio sound library developer tutorial |
---|
6 | |
---|
7 | .\".SH SYNOPSIS |
---|
8 | |
---|
9 | .\" #include <roaraudio.h> |
---|
10 | |
---|
11 | .SH "DESCRIPTION" |
---|
12 | This tutorial descipes some basics with working with libroar. We will create a simple application |
---|
13 | that can play some sines. A lot of other examples can be found in RoarAudio's sources in the roarclients |
---|
14 | directory. |
---|
15 | |
---|
16 | .SH "PLAYING A SINE" |
---|
17 | First of all we need the correct header files to be included: |
---|
18 | #include <math.h> /* sin() */ |
---|
19 | #include <stdio.h> /* *printf*() */ |
---|
20 | #include <roaraudio.h> /* libroar */ |
---|
21 | |
---|
22 | After that we need some basic varibales with data about the audio we want to play back: |
---|
23 | int rate = ROAR_RATE_DEFAULT; |
---|
24 | int bits = 8; |
---|
25 | int channels = 1; /* mono */ |
---|
26 | |
---|
27 | Next we need to set the 'codec'. The codec is how the data is encoded. |
---|
28 | We want PCM as signed ints in the native byte order of our machine. |
---|
29 | int codec = ROAR_CODEC_DEFAULT; |
---|
30 | |
---|
31 | Now we need to store the frequency of our sine: |
---|
32 | float freq = 523.2; /* middle C */ |
---|
33 | float step = M_PI*2*freq/rate; /* how much time per sample we have to encode ... */ |
---|
34 | |
---|
35 | In addition we need some variables to store the current time and the length of time sine: |
---|
36 | float t = 0; /* current time */ |
---|
37 | float length = 5; /* 5 sec */ |
---|
38 | |
---|
39 | The final thing we need is a socket to send data to the demon, a buffer to store the audio data and |
---|
40 | a varibale to go thru the array: |
---|
41 | int fh; |
---|
42 | int i; |
---|
43 | char out[1024]; |
---|
44 | |
---|
45 | To open the connection we use the call \fBroar_simple_play\fR(3): |
---|
46 | if ( (fh = roar_simple_play(rate, channels, bits, codec, NULL, "sine gen")) == -1 ) { |
---|
47 | fprintf(stderr, "Error: can not open playback!\n"); |
---|
48 | exit(1); |
---|
49 | } |
---|
50 | |
---|
51 | Now we want to loop for \fBlength\fR secunds: |
---|
52 | while (t < 2*M_PI*freq*length) { |
---|
53 | } |
---|
54 | |
---|
55 | In this loop we need to calculate our samples: |
---|
56 | for (i = 0; i < 1024; i++) { |
---|
57 | out[i] = 127*sin(t); |
---|
58 | t += step; |
---|
59 | } |
---|
60 | |
---|
61 | The sine is multiplyed by 127 as our amplitude range for 8 bit signed int is from -128 to +127. |
---|
62 | |
---|
63 | After we have our current data in \fBout\fR we want to write them to the server: |
---|
64 | write(fh, out, 1024); |
---|
65 | |
---|
66 | \fBNOTE:\fR In a real application you may want to check the return value but in our simple application |
---|
67 | we should get \fBSIGPIPE\fR on error and simply die. |
---|
68 | |
---|
69 | After we are finished with our main loop we have to close the socket to the server. |
---|
70 | This is done by \fBroar_simple_close\fR(3): |
---|
71 | roar_simple_close(fh); |
---|
72 | |
---|
73 | After adding some standard main() construct we should have something like this: |
---|
74 | #include <math.h> /* sin() */ |
---|
75 | #include <stdio.h> /* *printf*() */ |
---|
76 | #include <roaraudio.h> /* libroar */ |
---|
77 | |
---|
78 | int main (void) { |
---|
79 | int rate = ROAR_RATE_DEFAULT; |
---|
80 | int bits = 8; |
---|
81 | int channels = 1; /* mono */ |
---|
82 | int codec = ROAR_CODEC_DEFAULT; |
---|
83 | float freq = 523.2; /* middle C */ |
---|
84 | float step = M_PI*2*freq/rate; /* how much time per sample we have to encode ... */ |
---|
85 | float t = 0; /* current time */ |
---|
86 | float length = 5; /* 5 sec */ |
---|
87 | int fh; |
---|
88 | int i; |
---|
89 | char out[1024]; |
---|
90 | |
---|
91 | if ( (fh = roar_simple_play(rate, channels, bits, codec, NULL, "sine gen")) == -1 ) { |
---|
92 | fprintf(stderr, "Error: can not open playback!\n"); |
---|
93 | exit(1); |
---|
94 | } |
---|
95 | |
---|
96 | while (t < 2*M_PI*freq*length) { |
---|
97 | for (i = 0; i < 1024; i++) { |
---|
98 | out[i] = 127*sin(t); |
---|
99 | t += step; |
---|
100 | } |
---|
101 | write(fh, out, 1024); |
---|
102 | } |
---|
103 | |
---|
104 | roar_simple_close(fh); |
---|
105 | |
---|
106 | return 0; |
---|
107 | } |
---|
108 | |
---|
109 | To compile and link we can use a command like this one: |
---|
110 | cc -o roarsin roarsin.c -lm `roar-config --libs --cflags` |
---|
111 | |
---|
112 | We need to use \fB-lm\fR to link the math library for \fBsin()\fR. |
---|
113 | The tool \fBroar-config\fR(1) will keep care for us about all flags needed for libraor. |
---|
114 | |
---|
115 | Now we should have a working binary \fBroarsin\fR playing a sin() for 5 sec. |
---|
116 | |
---|
117 | Happy hacking! |
---|
118 | |
---|
119 | .SH "SEE ALSO" |
---|
120 | \fBroar-config\fR(1), |
---|
121 | \fBroarcat\fR(1), |
---|
122 | \fBlibroar\fR(7). |
---|
123 | \fBRoarAudio\fR(7). |
---|
124 | |
---|
125 | .\"ll |
---|