-
Notifications
You must be signed in to change notification settings - Fork 0
/
alsa.c
57 lines (45 loc) · 1.47 KB
/
alsa.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <alsa/asoundlib.h>
#include <alsa/pcm.h>
#include <math.h>
#define BUFFER_LEN 480000
static char *device = "default"; //soundcard
snd_output_t *output = NULL;
float buffer [BUFFER_LEN];
int main(void)
{
int err;
int j,k;
//int f = 440; //frequency
int f = 880; //frequency
int fs = 48000; //sampling frequency
snd_pcm_t *handle;
snd_pcm_sframes_t frames;
// ERROR HANDLING
if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
printf("Playback open error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
if ((err = snd_pcm_set_params(handle,
SND_PCM_FORMAT_FLOAT,
SND_PCM_ACCESS_RW_INTERLEAVED,
1,
48000,
1,
500000)) < 0) {
printf("Playback open error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
// SINE WAVE
int freq = 0;
for ( freq = 1000; freq < 20000; freq *= 2 ) {
printf("Sine tone at %dHz\n ",freq);
for (k=0; k<BUFFER_LEN; k++){
buffer[k] = ( sin(2*M_PI * k * freq)); //sine wave value generation
}
for (j=0; j<5; j++){
frames = snd_pcm_writei(handle, buffer, BUFFER_LEN); //sending values to sound driver
}
}
snd_pcm_close(handle);
return 0;
}