-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalsathreads.c
200 lines (153 loc) · 7.58 KB
/
alsathreads.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
#include "alsathreads.h"
#include <errno.h>
bool listdev(char *devname)
{
bool result = false;
char** hints;
int err;
char** n;
char* name;
char* desc;
char* ioid;
/* Enumerate sound devices */
err = snd_device_name_hint(-1, "pcm", (void***)&hints);
if (err != 0) {
fprintf(stderr, "*** Cannot get device names\n");
exit(1);
}
n = hints;
while (*n != NULL) {
name = snd_device_name_get_hint(*n, "NAME");
desc = snd_device_name_get_hint(*n, "DESC");
ioid = snd_device_name_get_hint(*n, "IOID");
if(strcmp(devname,name)==0) result = true;
if (name && strcmp("null", name)) free(name);
if (desc && strcmp("null", desc)) free(desc);
if (ioid && strcmp("null", ioid)) free(ioid);
n++;
}
//Free hint buffer too
snd_device_name_free_hint((void**)hints);
return result;
}
void* alsapbth(void *input)
{
int index = ((struct thargs*)input)->index;
char *adrx = ((struct thargs*)input)->adrx;
unsigned int samplerate = ((struct thargs*)input)->samplerate;
unsigned int alsabuffersize = ((struct thargs*)input)->alsabuffersize;
int SamplesPerRx = ((struct thargs*)input)->SamplesPerRx;
unsigned RxWriteCounterth=((struct thargs*)input)->RxWriteCounter;
fprintf (stdout,"new th %i on %s with samplerate %i\n", index, adrx,samplerate);
int waittotimeperframe = ((SamplesPerRx*1000000)/samplerate)/4;
int err;
snd_pcm_t *playback_handle;
snd_pcm_hw_params_t *hw_params;
if(!listdev(adrx)) {
fprintf (stderr,"\033[1;31m");
fprintf (stderr, "rx%d %s no such device \n",index,adrx);
fprintf (stderr,"\033[0m");
((struct thargs*)input)->state=-1;
pthread_exit( NULL );
}
if ((err = snd_pcm_open (&playback_handle, adrx, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
fprintf (stderr,"\033[1;31m");
fprintf (stderr, "rx%d cannot open audio device %s (%s)\n",index,adrx,snd_strerror (err));
fprintf (stderr,"\033[0m");
((struct thargs*)input)->state=-1;
pthread_exit( NULL );
}
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
fprintf (stderr,"\033[1;31m");
fprintf (stderr, "rx%d cannot allocate hardware parameter structure %s (%s)\n",index,adrx,snd_strerror (err));
fprintf (stderr,"\033[0m");
((struct thargs*)input)->state=-1;
pthread_exit( NULL );
}
if ((err = snd_pcm_hw_params_any (playback_handle, hw_params)) < 0) {
fprintf (stderr,"\033[1;31m");
fprintf (stderr, "rx%d cannot initialize hardware parameter structure %s (%s)\n",index,adrx,snd_strerror (err));
fprintf (stderr,"\033[0m");
((struct thargs*)input)->state=-1;
pthread_exit( NULL );
}
if ((err = snd_pcm_hw_params_set_access (playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
fprintf (stderr,"\033[1;31m");
fprintf (stderr, "rx%d cannot set access type %s (%s)\n",index,adrx,snd_strerror (err));
fprintf (stderr,"\033[0m");
((struct thargs*)input)->state=-1;
pthread_exit( NULL );
}
if ((err = snd_pcm_hw_params_set_format (playback_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
fprintf (stderr,"\033[1;31m");
fprintf (stderr, "rx%d cannot set sample format %s (%s)\n",index,adrx,snd_strerror (err));
fprintf (stderr,"\033[0m");
((struct thargs*)input)->state=-1;
pthread_exit( NULL );
}
if ((err = snd_pcm_hw_params_set_rate_near (playback_handle, hw_params, &samplerate, 0)) < 0) {
fprintf (stderr,"\033[1;31m");
fprintf (stderr, "rx%d cannot set sample rate %s (%s)\n",index,adrx,snd_strerror (err));
fprintf (stderr,"\033[0m");
((struct thargs*)input)->state=-1;
pthread_exit( NULL );
}
snd_pcm_uframes_t frames = samplerate / 100;
if ((err = snd_pcm_hw_params_set_period_size_near (playback_handle, hw_params, &frames, 0)) < 0) {
fprintf (stderr,"\033[1;31m");
fprintf (stderr, "rx%d cannot set period size %s (%s)\n",index,adrx,snd_strerror (err));
fprintf (stderr,"\033[0m");
((struct thargs*)input)->state=-1;
pthread_exit( NULL );
}
if ((err = snd_pcm_hw_params_set_rate_near (playback_handle, hw_params, &alsabuffersize, 0)) < 0) {
fprintf (stderr,"\033[1;31m");
fprintf (stderr, "rx%d cannot set buf size %s (%s)\n",index,adrx,snd_strerror (err));
fprintf (stderr,"\033[0m");
((struct thargs*)input)->state=-1;
pthread_exit( NULL );
}
if ((err = snd_pcm_hw_params_set_channels (playback_handle, hw_params, 2)) < 0) {
fprintf (stderr,"\033[1;31m");
fprintf (stderr, "rx%d cannot set channel count %s (%s)\n",index,adrx,snd_strerror (err));
fprintf (stderr,"\033[0m");
((struct thargs*)input)->state=-1;
pthread_exit( NULL );
}
if ((err = snd_pcm_hw_params (playback_handle, hw_params)) < 0) {
fprintf (stderr,"\033[1;31m");
fprintf (stderr, "rx%d cannot set parameters %s (%s)\n",index,adrx,snd_strerror (err));
fprintf (stderr,"\033[0m");
((struct thargs*)input)->state=-1;
pthread_exit( NULL );
}
snd_pcm_hw_params_free (hw_params);
if ((err = snd_pcm_prepare (playback_handle)) < 0) {
fprintf (stderr,"\033[1;31m");
fprintf (stderr, "rx%d cannot prepare audio interface for use %s (%s)\n",index,adrx,snd_strerror (err));
fprintf (stderr,"\033[0m");
((struct thargs*)input)->state=-1;
pthread_exit( NULL );
}
while((int)((struct thargs*)input)->RxWriteCounter < (int)(RxWriteCounterth+(((struct thargs*)input)->hermesbuffersize/2))) {
usleep(waittotimeperframe);
((struct thargs*)input)->state=2;
}
fprintf (stdout,"start play th %i on %s with samplerate %i\n", index, adrx,samplerate);
while(!((struct thargs*)input)->stopth) {
if(((struct thargs*)input)->RxWriteCounter != RxWriteCounterth) {
if ((err = snd_pcm_writei (playback_handle, ((struct thargs*)input)->output_items+(RxWriteCounterth * SamplesPerRx * 2), SamplesPerRx)) != SamplesPerRx) {
snd_pcm_recover(playback_handle, err, 0);
fprintf (stderr, "write to audio interface failed (%s)\n",
snd_strerror (err));
}
++RxWriteCounterth; if(RxWriteCounterth > ((struct thargs*)input)->hermesbuffersize) RxWriteCounterth=0;
}else{usleep(waittotimeperframe/10);}
}
snd_pcm_close (playback_handle);
fprintf (stdout,"end of th %i on %s with samplerate %i\n", index, adrx,samplerate);
pthread_exit( NULL );
}