comparison audioio/AudioPulseAudioTarget.cpp @ 195:d9c21e7bff21

* Some debug, turn off PulseAudio autospawn
author Chris Cannam
date Thu, 15 Jul 2010 15:25:59 +0000
parents 0b3aa9b702bb
children 8c13e8219903
comparison
equal deleted inserted replaced
194:302dae1f6016 195:d9c21e7bff21
22 22
23 #include <iostream> 23 #include <iostream>
24 #include <cassert> 24 #include <cassert>
25 #include <cmath> 25 #include <cmath>
26 26
27 //#define DEBUG_AUDIO_PULSE_AUDIO_TARGET 1 27 #define DEBUG_AUDIO_PULSE_AUDIO_TARGET 1
28 //#define DEBUG_AUDIO_PULSE_AUDIO_TARGET_PLAY 1
28 29
29 AudioPulseAudioTarget::AudioPulseAudioTarget(AudioCallbackPlaySource *source) : 30 AudioPulseAudioTarget::AudioPulseAudioTarget(AudioCallbackPlaySource *source) :
30 AudioCallbackPlayTarget(source), 31 AudioCallbackPlayTarget(source),
31 m_mutex(QMutex::Recursive), 32 m_mutex(QMutex::Recursive),
32 m_loop(0), 33 m_loop(0),
60 } 61 }
61 m_spec.rate = m_sampleRate; 62 m_spec.rate = m_sampleRate;
62 m_spec.channels = 2; 63 m_spec.channels = 2;
63 m_spec.format = PA_SAMPLE_FLOAT32NE; 64 m_spec.format = PA_SAMPLE_FLOAT32NE;
64 65
66 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET
67 std::cerr << "AudioPulseAudioTarget: Creating context" << std::endl;
68 #endif
69
65 m_context = pa_context_new(m_api, source->getClientName().toLocal8Bit().data()); 70 m_context = pa_context_new(m_api, source->getClientName().toLocal8Bit().data());
66 if (!m_context) { 71 if (!m_context) {
67 std::cerr << "ERROR: AudioPulseAudioTarget: Failed to create context object" << std::endl; 72 std::cerr << "ERROR: AudioPulseAudioTarget: Failed to create context object" << std::endl;
68 return; 73 return;
69 } 74 }
70 75
71 pa_context_set_state_callback(m_context, contextStateChangedStatic, this); 76 pa_context_set_state_callback(m_context, contextStateChangedStatic, this);
72 77
73 pa_context_connect(m_context, 0, (pa_context_flags_t)0, 0); // default server 78 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET
79 std::cerr << "AudioPulseAudioTarget: Connecting to default server..." << std::endl;
80 #endif
81
82 pa_context_connect(m_context, 0, // default server
83 (pa_context_flags_t)PA_CONTEXT_NOAUTOSPAWN, 0);
84
85 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET
86 std::cerr << "AudioPulseAudioTarget: Starting main loop" << std::endl;
87 #endif
74 88
75 m_loopThread = new MainLoopThread(m_loop); 89 m_loopThread = new MainLoopThread(m_loop);
76 m_loopThread->start(); 90 m_loopThread->start();
77 91
78 #ifdef DEBUG_PULSE_AUDIO_TARGET 92 #ifdef DEBUG_PULSE_AUDIO_TARGET
149 } 163 }
150 164
151 void 165 void
152 AudioPulseAudioTarget::streamWrite(size_t requested) 166 AudioPulseAudioTarget::streamWrite(size_t requested)
153 { 167 {
154 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET 168 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET_PLAY
155 std::cout << "AudioPulseAudioTarget::streamWrite(" << requested << ")" << std::endl; 169 std::cout << "AudioPulseAudioTarget::streamWrite(" << requested << ")" << std::endl;
156 #endif 170 #endif
157 if (m_done) return; 171 if (m_done) return;
158 172
159 QMutexLocker locker(&m_mutex); 173 QMutexLocker locker(&m_mutex);
179 193
180 if (nframes > m_bufferSize) { 194 if (nframes > m_bufferSize) {
181 std::cerr << "WARNING: AudioPulseAudioTarget::streamWrite: nframes " << nframes << " > m_bufferSize " << m_bufferSize << std::endl; 195 std::cerr << "WARNING: AudioPulseAudioTarget::streamWrite: nframes " << nframes << " > m_bufferSize " << m_bufferSize << std::endl;
182 } 196 }
183 197
184 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET 198 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET_PLAY
185 std::cout << "AudioPulseAudioTarget::streamWrite: nframes = " << nframes << std::endl; 199 std::cout << "AudioPulseAudioTarget::streamWrite: nframes = " << nframes << std::endl;
186 #endif 200 #endif
187 201
188 if (!tmpbuf || tmpbufch != sourceChannels || int(tmpbufsz) < nframes) { 202 if (!tmpbuf || tmpbufch != sourceChannels || int(tmpbufsz) < nframes) {
189 203
209 output = new float[tmpbufsz * tmpbufch]; 223 output = new float[tmpbufsz * tmpbufch];
210 } 224 }
211 225
212 size_t received = m_source->getSourceSamples(nframes, tmpbuf); 226 size_t received = m_source->getSourceSamples(nframes, tmpbuf);
213 227
214 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET 228 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET_PLAY
215 std::cerr << "requested " << nframes << ", received " << received << std::endl; 229 std::cerr << "requested " << nframes << ", received " << received << std::endl;
216 230
217 if (received < nframes) { 231 if (received < nframes) {
218 std::cerr << "*** WARNING: Wrong number of frames received" << std::endl; 232 std::cerr << "*** WARNING: Wrong number of frames received" << std::endl;
219 } 233 }
258 272
259 if (ch == 0) peakLeft = peak; 273 if (ch == 0) peakLeft = peak;
260 if (ch > 0 || sourceChannels == 1) peakRight = peak; 274 if (ch > 0 || sourceChannels == 1) peakRight = peak;
261 } 275 }
262 276
263 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET 277 #ifdef DEBUG_AUDIO_PULSE_AUDIO_TARGET_PLAY
264 std::cerr << "calling pa_stream_write with " 278 std::cerr << "calling pa_stream_write with "
265 << nframes * tmpbufch * sizeof(float) << " bytes" << std::endl; 279 << nframes * tmpbufch * sizeof(float) << " bytes" << std::endl;
266 #endif 280 #endif
267 281
268 pa_stream_write(m_stream, output, nframes * tmpbufch * sizeof(float), 282 pa_stream_write(m_stream, output, nframes * tmpbufch * sizeof(float),