comparison audioio/AudioPulseAudioTarget.cpp @ 366:0876ea394902 warnfix_no_size_t

Remove size_t's, fix compiler warnings
author Chris Cannam
date Tue, 17 Jun 2014 16:23:06 +0100
parents 068235cf5bf7
children 53fee450891e
comparison
equal deleted inserted replaced
355:e7a3fa8f4eec 366:0876ea394902
161 161
162 target->streamWrite(length); 162 target->streamWrite(length);
163 } 163 }
164 164
165 void 165 void
166 AudioPulseAudioTarget::streamWrite(size_t requested) 166 AudioPulseAudioTarget::streamWrite(int requested)
167 { 167 {
168 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET_PLAY 168 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET_PLAY
169 cout << "AudioPulseAudioTarget::streamWrite(" << requested << ")" << endl; 169 cout << "AudioPulseAudioTarget::streamWrite(" << requested << ")" << endl;
170 #endif 170 #endif
171 if (m_done) return; 171 if (m_done) return;
179 if (latframes > 0) m_source->setTargetPlayLatency(latframes); 179 if (latframes > 0) m_source->setTargetPlayLatency(latframes);
180 } 180 }
181 181
182 static float *output = 0; 182 static float *output = 0;
183 static float **tmpbuf = 0; 183 static float **tmpbuf = 0;
184 static size_t tmpbufch = 0; 184 static int tmpbufch = 0;
185 static size_t tmpbufsz = 0; 185 static int tmpbufsz = 0;
186 186
187 size_t sourceChannels = m_source->getSourceChannelCount(); 187 int sourceChannels = m_source->getSourceChannelCount();
188 188
189 // Because we offer pan, we always want at least 2 channels 189 // Because we offer pan, we always want at least 2 channels
190 if (sourceChannels < 2) sourceChannels = 2; 190 if (sourceChannels < 2) sourceChannels = 2;
191 191
192 size_t nframes = requested / (sourceChannels * sizeof(float)); 192 int nframes = requested / (sourceChannels * sizeof(float));
193 193
194 if (nframes > m_bufferSize) { 194 if (nframes > m_bufferSize) {
195 cerr << "WARNING: AudioPulseAudioTarget::streamWrite: nframes " << nframes << " > m_bufferSize " << m_bufferSize << endl; 195 cerr << "WARNING: AudioPulseAudioTarget::streamWrite: nframes " << nframes << " > m_bufferSize " << m_bufferSize << endl;
196 } 196 }
197 197
200 #endif 200 #endif
201 201
202 if (!tmpbuf || tmpbufch != sourceChannels || int(tmpbufsz) < nframes) { 202 if (!tmpbuf || tmpbufch != sourceChannels || int(tmpbufsz) < nframes) {
203 203
204 if (tmpbuf) { 204 if (tmpbuf) {
205 for (size_t i = 0; i < tmpbufch; ++i) { 205 for (int i = 0; i < tmpbufch; ++i) {
206 delete[] tmpbuf[i]; 206 delete[] tmpbuf[i];
207 } 207 }
208 delete[] tmpbuf; 208 delete[] tmpbuf;
209 } 209 }
210 210
214 214
215 tmpbufch = sourceChannels; 215 tmpbufch = sourceChannels;
216 tmpbufsz = nframes; 216 tmpbufsz = nframes;
217 tmpbuf = new float *[tmpbufch]; 217 tmpbuf = new float *[tmpbufch];
218 218
219 for (size_t i = 0; i < tmpbufch; ++i) { 219 for (int i = 0; i < tmpbufch; ++i) {
220 tmpbuf[i] = new float[tmpbufsz]; 220 tmpbuf[i] = new float[tmpbufsz];
221 } 221 }
222 222
223 output = new float[tmpbufsz * tmpbufch]; 223 output = new float[tmpbufsz * tmpbufch];
224 } 224 }
225 225
226 size_t received = m_source->getSourceSamples(nframes, tmpbuf); 226 int received = m_source->getSourceSamples(nframes, tmpbuf);
227 227
228 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET_PLAY 228 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET_PLAY
229 cerr << "requested " << nframes << ", received " << received << endl; 229 cerr << "requested " << nframes << ", received " << received << endl;
230 230
231 if (received < nframes) { 231 if (received < nframes) {
233 } 233 }
234 #endif 234 #endif
235 235
236 float peakLeft = 0.0, peakRight = 0.0; 236 float peakLeft = 0.0, peakRight = 0.0;
237 237
238 for (size_t ch = 0; ch < 2; ++ch) { 238 for (int ch = 0; ch < 2; ++ch) {
239 239
240 float peak = 0.0; 240 float peak = 0.0;
241 241
242 if (ch < sourceChannels) { 242 if (ch < sourceChannels) {
243 243
244 // PulseAudio samples are interleaved 244 // PulseAudio samples are interleaved
245 for (size_t i = 0; i < nframes; ++i) { 245 for (int i = 0; i < nframes; ++i) {
246 if (i < received) { 246 if (i < received) {
247 output[i * 2 + ch] = tmpbuf[ch][i] * m_outputGain; 247 output[i * 2 + ch] = tmpbuf[ch][i] * m_outputGain;
248 float sample = fabsf(output[i * 2 + ch]); 248 float sample = fabsf(output[i * 2 + ch]);
249 if (sample > peak) peak = sample; 249 if (sample > peak) peak = sample;
250 } else { 250 } else {
252 } 252 }
253 } 253 }
254 254
255 } else if (ch == 1 && sourceChannels == 1) { 255 } else if (ch == 1 && sourceChannels == 1) {
256 256
257 for (size_t i = 0; i < nframes; ++i) { 257 for (int i = 0; i < nframes; ++i) {
258 if (i < received) { 258 if (i < received) {
259 output[i * 2 + ch] = tmpbuf[0][i] * m_outputGain; 259 output[i * 2 + ch] = tmpbuf[0][i] * m_outputGain;
260 float sample = fabsf(output[i * 2 + ch]); 260 float sample = fabsf(output[i * 2 + ch]);
261 if (sample > peak) peak = sample; 261 if (sample > peak) peak = sample;
262 } else { 262 } else {
263 output[i * 2 + ch] = 0; 263 output[i * 2 + ch] = 0;
264 } 264 }
265 } 265 }
266 266
267 } else { 267 } else {
268 for (size_t i = 0; i < nframes; ++i) { 268 for (int i = 0; i < nframes; ++i) {
269 output[i * 2 + ch] = 0; 269 output[i * 2 + ch] = 0;
270 } 270 }
271 } 271 }
272 272
273 if (ch == 0) peakLeft = peak; 273 if (ch == 0) peakLeft = peak;
306 #endif 306 #endif
307 QMutexLocker locker(&m_mutex); 307 QMutexLocker locker(&m_mutex);
308 308
309 switch (pa_stream_get_state(m_stream)) { 309 switch (pa_stream_get_state(m_stream)) {
310 310
311 case PA_STREAM_CREATING: 311 case PA_STREAM_UNCONNECTED:
312 case PA_STREAM_TERMINATED: 312 case PA_STREAM_CREATING:
313 break; 313 case PA_STREAM_TERMINATED:
314 314 break;
315 case PA_STREAM_READY: 315
316 { 316 case PA_STREAM_READY:
317 SVDEBUG << "AudioPulseAudioTarget::streamStateChanged: Ready" << endl; 317 {
318 318 SVDEBUG << "AudioPulseAudioTarget::streamStateChanged: Ready" << endl;
319 pa_usec_t latency = 0; 319
320 int negative = 0; 320 pa_usec_t latency = 0;
321 if (pa_stream_get_latency(m_stream, &latency, &negative)) { 321 int negative = 0;
322 cerr << "AudioPulseAudioTarget::streamStateChanged: Failed to query latency" << endl; 322 if (pa_stream_get_latency(m_stream, &latency, &negative)) {
323 } 323 cerr << "AudioPulseAudioTarget::streamStateChanged: Failed to query latency" << endl;
324 cerr << "Latency = " << latency << " usec" << endl;
325 int latframes = (latency / 1000000.f) * float(m_sampleRate);
326 cerr << "that's " << latframes << " frames" << endl;
327
328 const pa_buffer_attr *attr;
329 if (!(attr = pa_stream_get_buffer_attr(m_stream))) {
330 SVDEBUG << "AudioPulseAudioTarget::streamStateChanged: Cannot query stream buffer attributes" << endl;
331 m_source->setTarget(this, m_bufferSize);
332 m_source->setTargetSampleRate(m_sampleRate);
333 if (latframes != 0) m_source->setTargetPlayLatency(latframes);
334 } else {
335 int targetLength = attr->tlength;
336 SVDEBUG << "AudioPulseAudioTarget::streamStateChanged: stream target length = " << targetLength << endl;
337 m_source->setTarget(this, targetLength);
338 m_source->setTargetSampleRate(m_sampleRate);
339 if (latframes == 0) latframes = targetLength;
340 cerr << "latency = " << latframes << endl;
341 m_source->setTargetPlayLatency(latframes);
342 }
343 } 324 }
344 break; 325 cerr << "Latency = " << latency << " usec" << endl;
345 326 int latframes = (latency / 1000000.f) * float(m_sampleRate);
346 case PA_STREAM_FAILED: 327 cerr << "that's " << latframes << " frames" << endl;
347 default: 328
348 cerr << "AudioPulseAudioTarget::streamStateChanged: Error: " 329 const pa_buffer_attr *attr;
349 << pa_strerror(pa_context_errno(m_context)) << endl; 330 if (!(attr = pa_stream_get_buffer_attr(m_stream))) {
350 //!!! do something... 331 SVDEBUG << "AudioPulseAudioTarget::streamStateChanged: Cannot query stream buffer attributes" << endl;
351 break; 332 m_source->setTarget(this, m_bufferSize);
333 m_source->setTargetSampleRate(m_sampleRate);
334 if (latframes != 0) m_source->setTargetPlayLatency(latframes);
335 } else {
336 int targetLength = attr->tlength;
337 SVDEBUG << "AudioPulseAudioTarget::streamStateChanged: stream target length = " << targetLength << endl;
338 m_source->setTarget(this, targetLength);
339 m_source->setTargetSampleRate(m_sampleRate);
340 if (latframes == 0) latframes = targetLength;
341 cerr << "latency = " << latframes << endl;
342 m_source->setTargetPlayLatency(latframes);
343 }
344 }
345 break;
346
347 case PA_STREAM_FAILED:
348 default:
349 cerr << "AudioPulseAudioTarget::streamStateChanged: Error: "
350 << pa_strerror(pa_context_errno(m_context)) << endl;
351 //!!! do something...
352 break;
352 } 353 }
353 } 354 }
354 355
355 void 356 void
356 AudioPulseAudioTarget::contextStateChangedStatic(pa_context *context, 357 AudioPulseAudioTarget::contextStateChangedStatic(pa_context *context,
371 #endif 372 #endif
372 QMutexLocker locker(&m_mutex); 373 QMutexLocker locker(&m_mutex);
373 374
374 switch (pa_context_get_state(m_context)) { 375 switch (pa_context_get_state(m_context)) {
375 376
377 case PA_CONTEXT_UNCONNECTED:
376 case PA_CONTEXT_CONNECTING: 378 case PA_CONTEXT_CONNECTING:
377 case PA_CONTEXT_AUTHORIZING: 379 case PA_CONTEXT_AUTHORIZING:
378 case PA_CONTEXT_SETTING_NAME: 380 case PA_CONTEXT_SETTING_NAME:
379 break; 381 break;
380 382