-
Notifications
You must be signed in to change notification settings - Fork 8
/
lwfsource.cpp
249 lines (203 loc) · 5.35 KB
/
lwfsource.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**************************************************************************
* copyright : (C) 2004-2006 by Petr Schwarz & Pavel Matejka *
* UPGM,FIT,VUT,Brno *
* email : {schwarzp,matejkap}@fit.vutbr.cz *
**************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
**************************************************************************/
#include "lwfsource.h"
#include "soundcard.h"
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
LWFSource::LWFSource()
{
device = WFS_DEFAULTDEVICE;
dp = 0;
sampleFreq = 16000;
channels = 1;
bitsPerSample = 16;
buffer = 0;
isRecording = false;
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
}
LWFSource::~LWFSource()
{
close();
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
}
void LWFSource::open()
{
if(dp)
return;
// check setting
assert(channels == 1 || channels == 2);
assert(bitsPerSample == 8 || bitsPerSample == 16);
// open device
dp = fopen(device, "rb");
if(!dp)
{
char msg[1024];
sprintf(msg, "Can not open the sound device: %900s.", device);
throw wf_error(msg);
}
// An input can be either device or file. If it is a device, configure it.
struct stat st;
fstat(fileno(dp), &st);
if(S_ISCHR(st.st_mode))
{
ioctl(fileno(dp), SNDCTL_DSP_RESET, 0);
int stereo_req = (channels == 1 ? 0 : 1);
int stereo = stereo_req;
if(ioctl(fileno(dp), SNDCTL_DSP_STEREO, &stereo) == -1 || stereo != stereo_req)
{
fclose(dp);
dp = 0;
throw wf_error("Can not set mono mode for the sound card.");
}
int audio_format_req = (bitsPerSample == 8 ? AFMT_S8 : AFMT_S16_LE);
int audio_format = audio_format_req;
if(ioctl(fileno(dp), SNDCTL_DSP_SETFMT, &audio_format) == -1 || audio_format != audio_format_req)
{
fclose(dp);
dp = 0;
throw wf_error("The sound card does not support little-endien shorts.");
}
int sample_freq = sampleFreq;
if(ioctl(fileno(dp), SNDCTL_DSP_SPEED, &sample_freq) == -1 || sample_freq != sampleFreq)
{
fclose(dp);
dp = 0;
char msg[1024];
sprintf(msg, "The sound card does not support desired sample frequncy: %d.", sampleFreq);
throw wf_error(msg);
}
}
// block align
blockAlign = (bitsPerSample == 8 ? 1 : 2);
if(channels == 2)
blockAlign *= 2;
// allocate buffer
bufferLen = (int)((float)(blockAlign * sampleFreq * WFS_BUFFERLENGTH) / 1000.0f + 0.5f);
frameLen = bufferLen * WFS_FRAMELENGTH / WFS_BUFFERLENGTH;
bufferLen -= (bufferLen % frameLen);
if(bufferLen == 0 || frameLen == 0)
{
fclose(dp);
dp = 0;
throw wf_error("Bad seting of the WFS_BUFFERLENGTH or the the WFS_FRAMELENGTH constant.");
}
try
{
buffer = new char[bufferLen];
bufferLastByte = buffer + bufferLen - 1;
}
catch(std::bad_alloc &) // Handler acording ANSI C++
{
fclose(dp);
dp = 0;
throw std::bad_alloc();
}
if(!buffer) // VC
{
fclose(dp);
dp = 0;
throw std::bad_alloc();
}
}
void LWFSource::runRecording()
{
isRecording = true;
reading = buffer;
storing = buffer;
bytesRecorded = 0;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
if(pthread_create(&thread, &attr, recordingBypass, this) != 0)
throw wf_error("Not enough system resources for creating thread.");
pthread_attr_destroy(&attr);
}
void *LWFSource::recordingBypass(void *arg)
{
((LWFSource *)arg)->recording();
pthread_exit(0);
};
void LWFSource::recording()
{
pthread_mutex_lock(&mutex);
while(bytesRecorded + frameLen <= bufferLen && isRecording)
{
pthread_cond_signal(&cond);
pthread_mutex_unlock (&mutex);
::read(fileno(dp), storing, frameLen);
pthread_mutex_lock(&mutex);
storing += frameLen;
if(storing > bufferLastByte)
storing = buffer;
bytesRecorded += frameLen;
}
isRecording = false;
pthread_mutex_unlock (&mutex);
}
void LWFSource::close()
{
if(!dp)
return;
if(isRecording)
{
isRecording = false;
int status;
pthread_join(thread, (void **)&status);
}
fclose(dp);
dp = 0;
}
void LWFSource::read(char *buff, int len)
{
if(!dp)
throw wf_error("The device is not open.");
if(!isRecording)
runRecording();
pthread_mutex_lock(&mutex);
int i;
for(i = 0; isRecording && i < len; ++i)
{
while(isRecording && bytesRecorded == 0)
pthread_cond_wait(&cond, &mutex);
if(isRecording)
{
buff[i] = *reading;
reading++;
if(reading > bufferLastByte)
reading = buffer;
bytesRecorded--;
}
}
pthread_mutex_unlock (&mutex);
}
bool LWFSource::isAvailable(char *ret_whay_not)
{
if(dp)
return true;
FILE *dp1;
dp1 = fopen(device, "rb");
if(dp1)
{
fclose(dp1);
return true;
}
if(ret_whay_not)
strcpy(ret_whay_not, "can not open device");
return false;
}