annotate audioio/AudioPortAudioTarget.cpp @ 177:5bde373ad5ca

* Layer tree view updating when visibility / audibility changed (and layers updating when they are changed in view) -- still some problems when a model is loaded while the tree is visible * FFTW_MEASURE throughout -- it does turn out to make an appreciable difference sometimes
author Chris Cannam
date Thu, 16 Aug 2007 16:47:07 +0000
parents 006c90387f40
children
rev   line source
Chris@94 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@94 2
Chris@94 3 /*
Chris@94 4 Sonic Visualiser
Chris@94 5 An audio file viewer and annotation editor.
Chris@94 6 Centre for Digital Music, Queen Mary, University of London.
Chris@94 7 This file copyright 2006 Chris Cannam.
Chris@94 8
Chris@94 9 This program is free software; you can redistribute it and/or
Chris@94 10 modify it under the terms of the GNU General Public License as
Chris@94 11 published by the Free Software Foundation; either version 2 of the
Chris@94 12 License, or (at your option) any later version. See the file
Chris@94 13 COPYING included with this distribution for more information.
Chris@94 14 */
Chris@94 15
Chris@94 16 #ifdef HAVE_PORTAUDIO
Chris@94 17
Chris@94 18 #include "AudioPortAudioTarget.h"
Chris@94 19 #include "AudioCallbackPlaySource.h"
Chris@94 20
Chris@94 21 #include <iostream>
Chris@94 22 #include <cassert>
Chris@94 23 #include <cmath>
Chris@94 24
Chris@99 25 //#define DEBUG_AUDIO_PORT_AUDIO_TARGET 1
Chris@94 26
Chris@94 27 AudioPortAudioTarget::AudioPortAudioTarget(AudioCallbackPlaySource *source) :
Chris@94 28 AudioCallbackPlayTarget(source),
Chris@94 29 m_stream(0),
Chris@94 30 m_bufferSize(0),
Chris@94 31 m_sampleRate(0),
Chris@94 32 m_latency(0)
Chris@94 33 {
Chris@94 34 PaError err;
Chris@94 35
Chris@94 36 #ifdef DEBUG_AUDIO_PORT_AUDIO_TARGET
Chris@96 37 #ifdef HAVE_PORTAUDIO_V18
Chris@94 38 std::cerr << "AudioPortAudioTarget: Initialising for PortAudio v18" << std::endl;
Chris@94 39 #else
Chris@94 40 std::cerr << "AudioPortAudioTarget: Initialising for PortAudio v19" << std::endl;
Chris@94 41 #endif
Chris@94 42 #endif
Chris@94 43
Chris@94 44 err = Pa_Initialize();
Chris@94 45 if (err != paNoError) {
Chris@94 46 std::cerr << "ERROR: AudioPortAudioTarget: Failed to initialize PortAudio: " << Pa_GetErrorText(err) << std::endl;
Chris@94 47 return;
Chris@94 48 }
Chris@94 49
Chris@94 50 m_bufferSize = 1024;
Chris@94 51 m_sampleRate = 44100;
Chris@94 52 if (m_source && (m_source->getSourceSampleRate() != 0)) {
Chris@94 53 m_sampleRate = m_source->getSourceSampleRate();
Chris@94 54 }
Chris@94 55
Chris@96 56 #ifdef HAVE_PORTAUDIO_V18
Chris@94 57 m_latency = Pa_GetMinNumBuffers(m_bufferSize, m_sampleRate) * m_bufferSize;
Chris@94 58 #endif
Chris@94 59
Chris@96 60 #ifdef HAVE_PORTAUDIO_V18
Chris@94 61 err = Pa_OpenDefaultStream(&m_stream, 0, 2, paFloat32,
Chris@94 62 m_sampleRate, m_bufferSize, 0,
Chris@94 63 processStatic, this);
Chris@94 64 #else
Chris@94 65 err = Pa_OpenDefaultStream(&m_stream, 0, 2, paFloat32,
Chris@94 66 m_sampleRate, m_bufferSize,
Chris@94 67 processStatic, this);
Chris@94 68 #endif
Chris@94 69
Chris@94 70 if (err != paNoError) {
Chris@94 71 std::cerr << "ERROR: AudioPortAudioTarget: Failed to open PortAudio stream: " << Pa_GetErrorText(err) << std::endl;
Chris@94 72 m_stream = 0;
Chris@94 73 Pa_Terminate();
Chris@94 74 return;
Chris@94 75 }
Chris@94 76
Chris@96 77 #ifndef HAVE_PORTAUDIO_V18
Chris@94 78 const PaStreamInfo *info = Pa_GetStreamInfo(m_stream);
Chris@94 79 m_latency = int(info->outputLatency * m_sampleRate + 0.001);
Chris@94 80 #endif
Chris@94 81
Chris@94 82 std::cerr << "PortAudio latency = " << m_latency << " frames" << std::endl;
Chris@94 83
Chris@94 84 err = Pa_StartStream(m_stream);
Chris@94 85
Chris@94 86 if (err != paNoError) {
Chris@94 87 std::cerr << "ERROR: AudioPortAudioTarget: Failed to start PortAudio stream: " << Pa_GetErrorText(err) << std::endl;
Chris@94 88 Pa_CloseStream(m_stream);
Chris@94 89 m_stream = 0;
Chris@94 90 Pa_Terminate();
Chris@94 91 return;
Chris@94 92 }
Chris@94 93
Chris@94 94 if (m_source) {
Chris@94 95 std::cerr << "AudioPortAudioTarget: block size " << m_bufferSize << std::endl;
Chris@94 96 m_source->setTargetBlockSize(m_bufferSize);
Chris@94 97 m_source->setTargetSampleRate(m_sampleRate);
Chris@94 98 m_source->setTargetPlayLatency(m_latency);
Chris@94 99 }
Chris@94 100
Chris@94 101 #ifdef DEBUG_PORT_AUDIO_TARGET
Chris@94 102 std::cerr << "AudioPortAudioTarget: initialised OK" << std::endl;
Chris@94 103 #endif
Chris@94 104 }
Chris@94 105
Chris@94 106 AudioPortAudioTarget::~AudioPortAudioTarget()
Chris@94 107 {
Chris@94 108 if (m_stream) {
Chris@94 109 PaError err;
Chris@94 110 err = Pa_CloseStream(m_stream);
Chris@94 111 if (err != paNoError) {
Chris@94 112 std::cerr << "ERROR: AudioPortAudioTarget: Failed to close PortAudio stream: " << Pa_GetErrorText(err) << std::endl;
Chris@94 113 }
Chris@94 114 err = Pa_Terminate();
Chris@94 115 if (err != paNoError) {
Chris@94 116 std::cerr << "ERROR: AudioPortAudioTarget: Failed to terminate PortAudio: " << Pa_GetErrorText(err) << std::endl;
Chris@94 117 }
Chris@94 118 }
Chris@94 119 }
Chris@94 120
Chris@94 121 bool
Chris@94 122 AudioPortAudioTarget::isOK() const
Chris@94 123 {
Chris@94 124 return (m_stream != 0);
Chris@94 125 }
Chris@94 126
Chris@96 127 #ifdef HAVE_PORTAUDIO_V18
Chris@94 128 int
Chris@94 129 AudioPortAudioTarget::processStatic(void *input, void *output,
Chris@94 130 unsigned long nframes,
Chris@94 131 PaTimestamp outTime, void *data)
Chris@94 132 {
Chris@94 133 return ((AudioPortAudioTarget *)data)->process(input, output,
Chris@94 134 nframes, outTime);
Chris@94 135 }
Chris@94 136 #else
Chris@94 137 int
Chris@94 138 AudioPortAudioTarget::processStatic(const void *input, void *output,
Chris@94 139 unsigned long nframes,
Chris@94 140 const PaStreamCallbackTimeInfo *timeInfo,
Chris@94 141 PaStreamCallbackFlags flags, void *data)
Chris@94 142 {
Chris@94 143 return ((AudioPortAudioTarget *)data)->process(input, output,
Chris@94 144 nframes, timeInfo,
Chris@94 145 flags);
Chris@94 146 }
Chris@94 147 #endif
Chris@94 148
Chris@94 149 void
Chris@94 150 AudioPortAudioTarget::sourceModelReplaced()
Chris@94 151 {
Chris@94 152 m_source->setTargetSampleRate(m_sampleRate);
Chris@94 153 }
Chris@94 154
Chris@96 155 #ifdef HAVE_PORTAUDIO_V18
Chris@94 156 int
Chris@94 157 AudioPortAudioTarget::process(void *inputBuffer, void *outputBuffer,
Chris@94 158 unsigned long nframes,
Chris@94 159 PaTimestamp)
Chris@94 160 #else
Chris@94 161 int
Chris@137 162 AudioPortAudioTarget::process(const void *, void *outputBuffer,
Chris@94 163 unsigned long nframes,
Chris@94 164 const PaStreamCallbackTimeInfo *,
Chris@94 165 PaStreamCallbackFlags)
Chris@94 166 #endif
Chris@94 167 {
Chris@94 168 #ifdef DEBUG_AUDIO_PORT_AUDIO_TARGET
Chris@94 169 std::cout << "AudioPortAudioTarget::process(" << nframes << ")" << std::endl;
Chris@94 170 #endif
Chris@94 171
Chris@94 172 if (!m_source) return 0;
Chris@94 173
Chris@94 174 float *output = (float *)outputBuffer;
Chris@94 175
Chris@94 176 assert(nframes <= m_bufferSize);
Chris@94 177
Chris@94 178 static float **tmpbuf = 0;
Chris@94 179 static size_t tmpbufch = 0;
Chris@94 180 static size_t tmpbufsz = 0;
Chris@94 181
Chris@94 182 size_t sourceChannels = m_source->getSourceChannelCount();
Chris@94 183
Chris@94 184 // Because we offer pan, we always want at least 2 channels
Chris@94 185 if (sourceChannels < 2) sourceChannels = 2;
Chris@94 186
Chris@137 187 if (!tmpbuf || tmpbufch != sourceChannels || int(tmpbufsz) < m_bufferSize) {
Chris@94 188
Chris@94 189 if (tmpbuf) {
Chris@94 190 for (size_t i = 0; i < tmpbufch; ++i) {
Chris@94 191 delete[] tmpbuf[i];
Chris@94 192 }
Chris@94 193 delete[] tmpbuf;
Chris@94 194 }
Chris@94 195
Chris@94 196 tmpbufch = sourceChannels;
Chris@94 197 tmpbufsz = m_bufferSize;
Chris@94 198 tmpbuf = new float *[tmpbufch];
Chris@94 199
Chris@94 200 for (size_t i = 0; i < tmpbufch; ++i) {
Chris@94 201 tmpbuf[i] = new float[tmpbufsz];
Chris@94 202 }
Chris@94 203 }
Chris@94 204
Chris@106 205 size_t received = m_source->getSourceSamples(nframes, tmpbuf);
Chris@94 206
Chris@94 207 float peakLeft = 0.0, peakRight = 0.0;
Chris@94 208
Chris@94 209 for (size_t ch = 0; ch < 2; ++ch) {
Chris@94 210
Chris@94 211 float peak = 0.0;
Chris@94 212
Chris@94 213 if (ch < sourceChannels) {
Chris@94 214
Chris@94 215 // PortAudio samples are interleaved
Chris@94 216 for (size_t i = 0; i < nframes; ++i) {
Chris@106 217 if (i < received) {
Chris@106 218 output[i * 2 + ch] = tmpbuf[ch][i] * m_outputGain;
Chris@106 219 float sample = fabsf(output[i * 2 + ch]);
Chris@106 220 if (sample > peak) peak = sample;
Chris@106 221 } else {
Chris@106 222 output[i * 2 + ch] = 0;
Chris@106 223 }
Chris@94 224 }
Chris@94 225
Chris@94 226 } else if (ch == 1 && sourceChannels == 1) {
Chris@94 227
Chris@94 228 for (size_t i = 0; i < nframes; ++i) {
Chris@106 229 if (i < received) {
Chris@106 230 output[i * 2 + ch] = tmpbuf[0][i] * m_outputGain;
Chris@106 231 float sample = fabsf(output[i * 2 + ch]);
Chris@106 232 if (sample > peak) peak = sample;
Chris@106 233 } else {
Chris@106 234 output[i * 2 + ch] = 0;
Chris@106 235 }
Chris@94 236 }
Chris@94 237
Chris@94 238 } else {
Chris@94 239 for (size_t i = 0; i < nframes; ++i) {
Chris@94 240 output[i * 2 + ch] = 0;
Chris@94 241 }
Chris@94 242 }
Chris@94 243
Chris@94 244 if (ch == 0) peakLeft = peak;
Chris@94 245 if (ch > 0 || sourceChannels == 1) peakRight = peak;
Chris@94 246 }
Chris@94 247
Chris@94 248 m_source->setOutputLevels(peakLeft, peakRight);
Chris@94 249
Chris@94 250 return 0;
Chris@94 251 }
Chris@94 252
Chris@94 253 #endif /* HAVE_PORTAUDIO */
Chris@94 254