Mercurial > hg > svapp
comparison audioio/AudioPulseAudioTarget.cpp @ 293:068235cf5bf7
Drop std:: from cout, cerr, endl -- pull these in through Debug.h
author | Chris Cannam |
---|---|
date | Tue, 26 Nov 2013 14:12:50 +0000 |
parents | 8aace2d9f1c2 |
children | 0876ea394902 |
comparison
equal
deleted
inserted
replaced
291:3fc0df289533 | 293:068235cf5bf7 |
---|---|
39 m_sampleRate(0), | 39 m_sampleRate(0), |
40 m_latency(0), | 40 m_latency(0), |
41 m_done(false) | 41 m_done(false) |
42 { | 42 { |
43 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET | 43 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET |
44 std::cerr << "AudioPulseAudioTarget: Initialising for PulseAudio" << std::endl; | 44 cerr << "AudioPulseAudioTarget: Initialising for PulseAudio" << endl; |
45 #endif | 45 #endif |
46 | 46 |
47 m_loop = pa_mainloop_new(); | 47 m_loop = pa_mainloop_new(); |
48 if (!m_loop) { | 48 if (!m_loop) { |
49 std::cerr << "ERROR: AudioPulseAudioTarget: Failed to create main loop" << std::endl; | 49 cerr << "ERROR: AudioPulseAudioTarget: Failed to create main loop" << endl; |
50 return; | 50 return; |
51 } | 51 } |
52 | 52 |
53 m_api = pa_mainloop_get_api(m_loop); | 53 m_api = pa_mainloop_get_api(m_loop); |
54 | 54 |
62 m_spec.rate = m_sampleRate; | 62 m_spec.rate = m_sampleRate; |
63 m_spec.channels = 2; | 63 m_spec.channels = 2; |
64 m_spec.format = PA_SAMPLE_FLOAT32NE; | 64 m_spec.format = PA_SAMPLE_FLOAT32NE; |
65 | 65 |
66 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET | 66 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET |
67 std::cerr << "AudioPulseAudioTarget: Creating context" << std::endl; | 67 cerr << "AudioPulseAudioTarget: Creating context" << endl; |
68 #endif | 68 #endif |
69 | 69 |
70 m_context = pa_context_new(m_api, source->getClientName().toLocal8Bit().data()); | 70 m_context = pa_context_new(m_api, source->getClientName().toLocal8Bit().data()); |
71 if (!m_context) { | 71 if (!m_context) { |
72 std::cerr << "ERROR: AudioPulseAudioTarget: Failed to create context object" << std::endl; | 72 cerr << "ERROR: AudioPulseAudioTarget: Failed to create context object" << endl; |
73 return; | 73 return; |
74 } | 74 } |
75 | 75 |
76 pa_context_set_state_callback(m_context, contextStateChangedStatic, this); | 76 pa_context_set_state_callback(m_context, contextStateChangedStatic, this); |
77 | 77 |
78 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET | 78 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET |
79 std::cerr << "AudioPulseAudioTarget: Connecting to default server..." << std::endl; | 79 cerr << "AudioPulseAudioTarget: Connecting to default server..." << endl; |
80 #endif | 80 #endif |
81 | 81 |
82 pa_context_connect(m_context, 0, // default server | 82 pa_context_connect(m_context, 0, // default server |
83 (pa_context_flags_t)PA_CONTEXT_NOAUTOSPAWN, 0); | 83 (pa_context_flags_t)PA_CONTEXT_NOAUTOSPAWN, 0); |
84 | 84 |
85 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET | 85 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET |
86 std::cerr << "AudioPulseAudioTarget: Starting main loop" << std::endl; | 86 cerr << "AudioPulseAudioTarget: Starting main loop" << endl; |
87 #endif | 87 #endif |
88 | 88 |
89 m_loopThread = new MainLoopThread(m_loop); | 89 m_loopThread = new MainLoopThread(m_loop); |
90 m_loopThread->start(); | 90 m_loopThread->start(); |
91 | 91 |
92 #ifdef DEBUG_PULSE_AUDIO_TARGET | 92 #ifdef DEBUG_PULSE_AUDIO_TARGET |
93 std::cerr << "AudioPulseAudioTarget: initialised OK" << std::endl; | 93 cerr << "AudioPulseAudioTarget: initialised OK" << endl; |
94 #endif | 94 #endif |
95 } | 95 } |
96 | 96 |
97 AudioPulseAudioTarget::~AudioPulseAudioTarget() | 97 AudioPulseAudioTarget::~AudioPulseAudioTarget() |
98 { | 98 { |
164 | 164 |
165 void | 165 void |
166 AudioPulseAudioTarget::streamWrite(size_t requested) | 166 AudioPulseAudioTarget::streamWrite(size_t requested) |
167 { | 167 { |
168 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET_PLAY | 168 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET_PLAY |
169 std::cout << "AudioPulseAudioTarget::streamWrite(" << requested << ")" << std::endl; | 169 cout << "AudioPulseAudioTarget::streamWrite(" << requested << ")" << endl; |
170 #endif | 170 #endif |
171 if (m_done) return; | 171 if (m_done) return; |
172 | 172 |
173 QMutexLocker locker(&m_mutex); | 173 QMutexLocker locker(&m_mutex); |
174 | 174 |
190 if (sourceChannels < 2) sourceChannels = 2; | 190 if (sourceChannels < 2) sourceChannels = 2; |
191 | 191 |
192 size_t nframes = requested / (sourceChannels * sizeof(float)); | 192 size_t nframes = requested / (sourceChannels * sizeof(float)); |
193 | 193 |
194 if (nframes > m_bufferSize) { | 194 if (nframes > m_bufferSize) { |
195 std::cerr << "WARNING: AudioPulseAudioTarget::streamWrite: nframes " << nframes << " > m_bufferSize " << m_bufferSize << std::endl; | 195 cerr << "WARNING: AudioPulseAudioTarget::streamWrite: nframes " << nframes << " > m_bufferSize " << m_bufferSize << endl; |
196 } | 196 } |
197 | 197 |
198 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET_PLAY | 198 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET_PLAY |
199 std::cout << "AudioPulseAudioTarget::streamWrite: nframes = " << nframes << std::endl; | 199 cout << "AudioPulseAudioTarget::streamWrite: nframes = " << nframes << endl; |
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) { |
224 } | 224 } |
225 | 225 |
226 size_t received = m_source->getSourceSamples(nframes, tmpbuf); | 226 size_t 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 std::cerr << "requested " << nframes << ", received " << received << std::endl; | 229 cerr << "requested " << nframes << ", received " << received << endl; |
230 | 230 |
231 if (received < nframes) { | 231 if (received < nframes) { |
232 std::cerr << "*** WARNING: Wrong number of frames received" << std::endl; | 232 cerr << "*** WARNING: Wrong number of frames received" << endl; |
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 |
317 SVDEBUG << "AudioPulseAudioTarget::streamStateChanged: Ready" << endl; | 317 SVDEBUG << "AudioPulseAudioTarget::streamStateChanged: Ready" << endl; |
318 | 318 |
319 pa_usec_t latency = 0; | 319 pa_usec_t latency = 0; |
320 int negative = 0; | 320 int negative = 0; |
321 if (pa_stream_get_latency(m_stream, &latency, &negative)) { | 321 if (pa_stream_get_latency(m_stream, &latency, &negative)) { |
322 std::cerr << "AudioPulseAudioTarget::streamStateChanged: Failed to query latency" << std::endl; | 322 cerr << "AudioPulseAudioTarget::streamStateChanged: Failed to query latency" << endl; |
323 } | 323 } |
324 std::cerr << "Latency = " << latency << " usec" << std::endl; | 324 cerr << "Latency = " << latency << " usec" << endl; |
325 int latframes = (latency / 1000000.f) * float(m_sampleRate); | 325 int latframes = (latency / 1000000.f) * float(m_sampleRate); |
326 std::cerr << "that's " << latframes << " frames" << std::endl; | 326 cerr << "that's " << latframes << " frames" << endl; |
327 | 327 |
328 const pa_buffer_attr *attr; | 328 const pa_buffer_attr *attr; |
329 if (!(attr = pa_stream_get_buffer_attr(m_stream))) { | 329 if (!(attr = pa_stream_get_buffer_attr(m_stream))) { |
330 SVDEBUG << "AudioPulseAudioTarget::streamStateChanged: Cannot query stream buffer attributes" << endl; | 330 SVDEBUG << "AudioPulseAudioTarget::streamStateChanged: Cannot query stream buffer attributes" << endl; |
331 m_source->setTarget(this, m_bufferSize); | 331 m_source->setTarget(this, m_bufferSize); |
335 int targetLength = attr->tlength; | 335 int targetLength = attr->tlength; |
336 SVDEBUG << "AudioPulseAudioTarget::streamStateChanged: stream target length = " << targetLength << endl; | 336 SVDEBUG << "AudioPulseAudioTarget::streamStateChanged: stream target length = " << targetLength << endl; |
337 m_source->setTarget(this, targetLength); | 337 m_source->setTarget(this, targetLength); |
338 m_source->setTargetSampleRate(m_sampleRate); | 338 m_source->setTargetSampleRate(m_sampleRate); |
339 if (latframes == 0) latframes = targetLength; | 339 if (latframes == 0) latframes = targetLength; |
340 std::cerr << "latency = " << latframes << std::endl; | 340 cerr << "latency = " << latframes << endl; |
341 m_source->setTargetPlayLatency(latframes); | 341 m_source->setTargetPlayLatency(latframes); |
342 } | 342 } |
343 } | 343 } |
344 break; | 344 break; |
345 | 345 |
346 case PA_STREAM_FAILED: | 346 case PA_STREAM_FAILED: |
347 default: | 347 default: |
348 std::cerr << "AudioPulseAudioTarget::streamStateChanged: Error: " | 348 cerr << "AudioPulseAudioTarget::streamStateChanged: Error: " |
349 << pa_strerror(pa_context_errno(m_context)) << std::endl; | 349 << pa_strerror(pa_context_errno(m_context)) << endl; |
350 //!!! do something... | 350 //!!! do something... |
351 break; | 351 break; |
352 } | 352 } |
353 } | 353 } |
354 | 354 |
392 if (pa_stream_connect_playback | 392 if (pa_stream_connect_playback |
393 (m_stream, 0, 0, | 393 (m_stream, 0, 0, |
394 pa_stream_flags_t(PA_STREAM_INTERPOLATE_TIMING | | 394 pa_stream_flags_t(PA_STREAM_INTERPOLATE_TIMING | |
395 PA_STREAM_AUTO_TIMING_UPDATE), | 395 PA_STREAM_AUTO_TIMING_UPDATE), |
396 0, 0)) { //??? return value | 396 0, 0)) { //??? return value |
397 std::cerr << "AudioPulseAudioTarget: Failed to connect playback stream" << std::endl; | 397 cerr << "AudioPulseAudioTarget: Failed to connect playback stream" << endl; |
398 } | 398 } |
399 | 399 |
400 break; | 400 break; |
401 | 401 |
402 case PA_CONTEXT_TERMINATED: | 402 case PA_CONTEXT_TERMINATED: |
404 //!!! do something... | 404 //!!! do something... |
405 break; | 405 break; |
406 | 406 |
407 case PA_CONTEXT_FAILED: | 407 case PA_CONTEXT_FAILED: |
408 default: | 408 default: |
409 std::cerr << "AudioPulseAudioTarget::contextStateChanged: Error: " | 409 cerr << "AudioPulseAudioTarget::contextStateChanged: Error: " |
410 << pa_strerror(pa_context_errno(m_context)) << std::endl; | 410 << pa_strerror(pa_context_errno(m_context)) << endl; |
411 //!!! do something... | 411 //!!! do something... |
412 break; | 412 break; |
413 } | 413 } |
414 } | 414 } |
415 | 415 |