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