Chris@94: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@94: Chris@94: /* Chris@94: Sonic Visualiser Chris@94: An audio file viewer and annotation editor. Chris@94: Centre for Digital Music, Queen Mary, University of London. Chris@94: This file copyright 2006 Chris Cannam. Chris@94: Chris@94: This program is free software; you can redistribute it and/or Chris@94: modify it under the terms of the GNU General Public License as Chris@94: published by the Free Software Foundation; either version 2 of the Chris@94: License, or (at your option) any later version. See the file Chris@94: COPYING included with this distribution for more information. Chris@94: */ Chris@94: Chris@94: #ifdef HAVE_PORTAUDIO Chris@94: Chris@94: #include "AudioPortAudioTarget.h" Chris@94: #include "AudioCallbackPlaySource.h" Chris@94: Chris@94: #include Chris@94: #include Chris@94: #include Chris@94: Chris@99: //#define DEBUG_AUDIO_PORT_AUDIO_TARGET 1 Chris@94: Chris@94: AudioPortAudioTarget::AudioPortAudioTarget(AudioCallbackPlaySource *source) : Chris@94: AudioCallbackPlayTarget(source), Chris@94: m_stream(0), Chris@94: m_bufferSize(0), Chris@94: m_sampleRate(0), Chris@94: m_latency(0) Chris@94: { Chris@94: PaError err; Chris@94: Chris@94: #ifdef DEBUG_AUDIO_PORT_AUDIO_TARGET Chris@96: #ifdef HAVE_PORTAUDIO_V18 Chris@94: std::cerr << "AudioPortAudioTarget: Initialising for PortAudio v18" << std::endl; Chris@94: #else Chris@94: std::cerr << "AudioPortAudioTarget: Initialising for PortAudio v19" << std::endl; Chris@94: #endif Chris@94: #endif Chris@94: Chris@94: err = Pa_Initialize(); Chris@94: if (err != paNoError) { Chris@94: std::cerr << "ERROR: AudioPortAudioTarget: Failed to initialize PortAudio: " << Pa_GetErrorText(err) << std::endl; Chris@94: return; Chris@94: } Chris@94: Chris@94: m_bufferSize = 1024; Chris@94: m_sampleRate = 44100; Chris@94: if (m_source && (m_source->getSourceSampleRate() != 0)) { Chris@94: m_sampleRate = m_source->getSourceSampleRate(); Chris@94: } Chris@94: Chris@96: #ifdef HAVE_PORTAUDIO_V18 Chris@94: m_latency = Pa_GetMinNumBuffers(m_bufferSize, m_sampleRate) * m_bufferSize; Chris@94: #endif Chris@94: Chris@96: #ifdef HAVE_PORTAUDIO_V18 Chris@94: err = Pa_OpenDefaultStream(&m_stream, 0, 2, paFloat32, Chris@94: m_sampleRate, m_bufferSize, 0, Chris@94: processStatic, this); Chris@94: #else Chris@94: err = Pa_OpenDefaultStream(&m_stream, 0, 2, paFloat32, Chris@94: m_sampleRate, m_bufferSize, Chris@94: processStatic, this); Chris@94: #endif Chris@94: Chris@94: if (err != paNoError) { Chris@94: std::cerr << "ERROR: AudioPortAudioTarget: Failed to open PortAudio stream: " << Pa_GetErrorText(err) << std::endl; Chris@94: m_stream = 0; Chris@94: Pa_Terminate(); Chris@94: return; Chris@94: } Chris@94: Chris@96: #ifndef HAVE_PORTAUDIO_V18 Chris@94: const PaStreamInfo *info = Pa_GetStreamInfo(m_stream); Chris@94: m_latency = int(info->outputLatency * m_sampleRate + 0.001); Chris@94: #endif Chris@94: Chris@94: std::cerr << "PortAudio latency = " << m_latency << " frames" << std::endl; Chris@94: Chris@94: err = Pa_StartStream(m_stream); Chris@94: Chris@94: if (err != paNoError) { Chris@94: std::cerr << "ERROR: AudioPortAudioTarget: Failed to start PortAudio stream: " << Pa_GetErrorText(err) << std::endl; Chris@94: Pa_CloseStream(m_stream); Chris@94: m_stream = 0; Chris@94: Pa_Terminate(); Chris@94: return; Chris@94: } Chris@94: Chris@94: if (m_source) { Chris@94: std::cerr << "AudioPortAudioTarget: block size " << m_bufferSize << std::endl; Chris@94: m_source->setTargetBlockSize(m_bufferSize); Chris@94: m_source->setTargetSampleRate(m_sampleRate); Chris@94: m_source->setTargetPlayLatency(m_latency); Chris@94: } Chris@94: Chris@94: #ifdef DEBUG_PORT_AUDIO_TARGET Chris@94: std::cerr << "AudioPortAudioTarget: initialised OK" << std::endl; Chris@94: #endif Chris@94: } Chris@94: Chris@94: AudioPortAudioTarget::~AudioPortAudioTarget() Chris@94: { Chris@94: if (m_stream) { Chris@94: PaError err; Chris@94: err = Pa_CloseStream(m_stream); Chris@94: if (err != paNoError) { Chris@94: std::cerr << "ERROR: AudioPortAudioTarget: Failed to close PortAudio stream: " << Pa_GetErrorText(err) << std::endl; Chris@94: } Chris@94: err = Pa_Terminate(); Chris@94: if (err != paNoError) { Chris@94: std::cerr << "ERROR: AudioPortAudioTarget: Failed to terminate PortAudio: " << Pa_GetErrorText(err) << std::endl; Chris@94: } Chris@94: } Chris@94: } Chris@94: Chris@94: bool Chris@94: AudioPortAudioTarget::isOK() const Chris@94: { Chris@94: return (m_stream != 0); Chris@94: } Chris@94: Chris@96: #ifdef HAVE_PORTAUDIO_V18 Chris@94: int Chris@94: AudioPortAudioTarget::processStatic(void *input, void *output, Chris@94: unsigned long nframes, Chris@94: PaTimestamp outTime, void *data) Chris@94: { Chris@94: return ((AudioPortAudioTarget *)data)->process(input, output, Chris@94: nframes, outTime); Chris@94: } Chris@94: #else Chris@94: int Chris@94: AudioPortAudioTarget::processStatic(const void *input, void *output, Chris@94: unsigned long nframes, Chris@94: const PaStreamCallbackTimeInfo *timeInfo, Chris@94: PaStreamCallbackFlags flags, void *data) Chris@94: { Chris@94: return ((AudioPortAudioTarget *)data)->process(input, output, Chris@94: nframes, timeInfo, Chris@94: flags); Chris@94: } Chris@94: #endif Chris@94: Chris@94: void Chris@94: AudioPortAudioTarget::sourceModelReplaced() Chris@94: { Chris@94: m_source->setTargetSampleRate(m_sampleRate); Chris@94: } Chris@94: Chris@96: #ifdef HAVE_PORTAUDIO_V18 Chris@94: int Chris@94: AudioPortAudioTarget::process(void *inputBuffer, void *outputBuffer, Chris@94: unsigned long nframes, Chris@94: PaTimestamp) Chris@94: #else Chris@94: int Chris@94: AudioPortAudioTarget::process(const void *inputBuffer, void *outputBuffer, Chris@94: unsigned long nframes, Chris@94: const PaStreamCallbackTimeInfo *, Chris@94: PaStreamCallbackFlags) Chris@94: #endif Chris@94: { Chris@94: #ifdef DEBUG_AUDIO_PORT_AUDIO_TARGET Chris@94: std::cout << "AudioPortAudioTarget::process(" << nframes << ")" << std::endl; Chris@94: #endif Chris@94: Chris@94: if (!m_source) return 0; Chris@94: Chris@94: float *output = (float *)outputBuffer; Chris@94: Chris@94: assert(nframes <= m_bufferSize); Chris@94: Chris@94: static float **tmpbuf = 0; Chris@94: static size_t tmpbufch = 0; Chris@94: static size_t tmpbufsz = 0; Chris@94: Chris@94: size_t sourceChannels = m_source->getSourceChannelCount(); Chris@94: Chris@94: // Because we offer pan, we always want at least 2 channels Chris@94: if (sourceChannels < 2) sourceChannels = 2; Chris@94: Chris@94: if (!tmpbuf || tmpbufch != sourceChannels || tmpbufsz < m_bufferSize) { Chris@94: Chris@94: if (tmpbuf) { Chris@94: for (size_t i = 0; i < tmpbufch; ++i) { Chris@94: delete[] tmpbuf[i]; Chris@94: } Chris@94: delete[] tmpbuf; Chris@94: } Chris@94: Chris@94: tmpbufch = sourceChannels; Chris@94: tmpbufsz = m_bufferSize; Chris@94: tmpbuf = new float *[tmpbufch]; Chris@94: Chris@94: for (size_t i = 0; i < tmpbufch; ++i) { Chris@94: tmpbuf[i] = new float[tmpbufsz]; Chris@94: } Chris@94: } Chris@94: Chris@94: m_source->getSourceSamples(nframes, tmpbuf); Chris@94: Chris@94: float peakLeft = 0.0, peakRight = 0.0; Chris@94: Chris@94: for (size_t ch = 0; ch < 2; ++ch) { Chris@94: Chris@94: float peak = 0.0; Chris@94: Chris@94: if (ch < sourceChannels) { Chris@94: Chris@94: // PortAudio samples are interleaved Chris@94: for (size_t i = 0; i < nframes; ++i) { Chris@94: output[i * 2 + ch] = tmpbuf[ch][i] * m_outputGain; Chris@94: float sample = fabsf(output[i * 2 + ch]); Chris@94: if (sample > peak) peak = sample; Chris@94: } Chris@94: Chris@94: } else if (ch == 1 && sourceChannels == 1) { Chris@94: Chris@94: for (size_t i = 0; i < nframes; ++i) { Chris@94: output[i * 2 + ch] = tmpbuf[0][i] * m_outputGain; Chris@94: float sample = fabsf(output[i * 2 + ch]); Chris@94: if (sample > peak) peak = sample; Chris@94: } Chris@94: Chris@94: } else { Chris@94: for (size_t i = 0; i < nframes; ++i) { Chris@94: output[i * 2 + ch] = 0; Chris@94: } Chris@94: } Chris@94: Chris@94: if (ch == 0) peakLeft = peak; Chris@94: if (ch > 0 || sourceChannels == 1) peakRight = peak; Chris@94: } Chris@94: Chris@94: m_source->setOutputLevels(peakLeft, peakRight); Chris@94: Chris@94: return 0; Chris@94: } Chris@94: Chris@94: #ifdef INCLUDE_MOCFILES Chris@94: #include "AudioPortAudioTarget.moc.cpp" Chris@94: #endif Chris@94: Chris@94: #endif /* HAVE_PORTAUDIO */ Chris@94: