-
Notifications
You must be signed in to change notification settings - Fork 2
/
AlsaWriter.cpp
97 lines (81 loc) · 2.68 KB
/
AlsaWriter.cpp
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "AlsaWriter.h"
#include <unistd.h>
#include <iostream>
#include <cstring>
using namespace std;
void AlsaWriter::execute(size_t num_items)
{
if (!pool) return;
if (num_items <= 0) return;
this->num_items = num_items;
sample_rate = SAMPLE_RATE;
alsa_setup("default",&sample_rate);
start();
}
void AlsaWriter::run()
{
int rc;
if (!pool) return;
for (size_t i = 0; i < num_items; ++i)
{
short *data = pool->pop();
rc = snd_pcm_writei(handle,data,TONE_BUFFER_SIZE/CHANNELS);
delete [] data;
//fwrite((char*)data, sizeof(short), TONE_BUFFER_SIZE, stdout);
if(rc == -EPIPE){
snd_pcm_prepare(handle);
}
}
sleep(4);
}
void AlsaWriter::alsa_setup(const char *device, unsigned int *sample_rate){
int err;
snd_pcm_hw_params_t *hw_params;
if ((err = snd_pcm_open (&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
fprintf (stderr, "cannot open audio device %s (%s)\n",
device,
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 (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 (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 (handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
fprintf (stderr, "cannot set sample format (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_rate_near (handle, hw_params, sample_rate, 0)) < 0) {
fprintf (stderr, "cannot set sample rate (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_channels (handle, hw_params, CHANNELS)) < 0) {
fprintf (stderr, "cannot set channel count (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params (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 (handle)) < 0) {
fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
snd_strerror (err));
exit (1);
}
}