Chris@59: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@59: Chris@59: /* Chris@59: Sonic Visualiser Chris@59: An audio file viewer and annotation editor. Chris@59: Centre for Digital Music, Queen Mary, University of London. Chris@59: This file copyright 2006 Chris Cannam. Chris@59: Chris@59: This program is free software; you can redistribute it and/or Chris@59: modify it under the terms of the GNU General Public License as Chris@59: published by the Free Software Foundation; either version 2 of the Chris@59: License, or (at your option) any later version. See the file Chris@59: COPYING included with this distribution for more information. Chris@59: */ Chris@59: Chris@114: #ifdef HAVE_PORTAUDIO_2_0 Chris@59: Chris@59: #include "AudioPortAudioTarget.h" Chris@59: #include "AudioCallbackPlaySource.h" Chris@59: Chris@59: #include Chris@59: #include Chris@59: #include Chris@59: Chris@59: //#define DEBUG_AUDIO_PORT_AUDIO_TARGET 1 Chris@59: Chris@59: AudioPortAudioTarget::AudioPortAudioTarget(AudioCallbackPlaySource *source) : Chris@59: AudioCallbackPlayTarget(source), Chris@59: m_stream(0), Chris@59: m_bufferSize(0), Chris@59: m_sampleRate(0), Chris@70: m_latency(0), Chris@70: m_done(false) Chris@59: { Chris@59: PaError err; Chris@59: Chris@59: #ifdef DEBUG_AUDIO_PORT_AUDIO_TARGET Chris@59: std::cerr << "AudioPortAudioTarget: Initialising for PortAudio v19" << std::endl; Chris@59: #endif Chris@59: Chris@59: err = Pa_Initialize(); Chris@59: if (err != paNoError) { Chris@59: std::cerr << "ERROR: AudioPortAudioTarget: Failed to initialize PortAudio: " << Pa_GetErrorText(err) << std::endl; Chris@59: return; Chris@59: } Chris@59: Chris@80: m_bufferSize = 2048; Chris@59: m_sampleRate = 44100; Chris@59: if (m_source && (m_source->getSourceSampleRate() != 0)) { Chris@59: m_sampleRate = m_source->getSourceSampleRate(); Chris@59: } Chris@59: Chris@81: PaStreamParameters op; Chris@96: op.device = Pa_GetDefaultOutputDevice(); Chris@81: op.channelCount = 2; Chris@81: op.sampleFormat = paFloat32; Chris@81: op.suggestedLatency = 0.2; Chris@81: op.hostApiSpecificStreamInfo = 0; Chris@91: err = Pa_OpenStream(&m_stream, 0, &op, m_sampleRate, Chris@91: paFramesPerBufferUnspecified, Chris@81: paNoFlag, processStatic, this); Chris@59: Chris@95: if (err != paNoError) { Chris@95: Chris@95: std::cerr << "WARNING: AudioPortAudioTarget: Failed to open PortAudio stream with default frames per buffer, trying again with fixed frames per buffer..." << std::endl; Chris@95: Chris@95: err = Pa_OpenStream(&m_stream, 0, &op, m_sampleRate, Chris@95: 1024, Chris@95: paNoFlag, processStatic, this); Chris@96: m_bufferSize = 1024; Chris@95: } Chris@95: Chris@59: if (err != paNoError) { Chris@59: std::cerr << "ERROR: AudioPortAudioTarget: Failed to open PortAudio stream: " << Pa_GetErrorText(err) << std::endl; Chris@59: m_stream = 0; Chris@59: Pa_Terminate(); Chris@59: return; Chris@59: } Chris@59: Chris@59: const PaStreamInfo *info = Pa_GetStreamInfo(m_stream); Chris@59: m_latency = int(info->outputLatency * m_sampleRate + 0.001); Chris@96: if (m_bufferSize < m_latency) m_bufferSize = m_latency; Chris@59: Chris@59: std::cerr << "PortAudio latency = " << m_latency << " frames" << std::endl; Chris@59: Chris@59: err = Pa_StartStream(m_stream); Chris@59: Chris@59: if (err != paNoError) { Chris@59: std::cerr << "ERROR: AudioPortAudioTarget: Failed to start PortAudio stream: " << Pa_GetErrorText(err) << std::endl; Chris@59: Pa_CloseStream(m_stream); Chris@59: m_stream = 0; Chris@59: Pa_Terminate(); Chris@59: return; Chris@59: } Chris@59: Chris@59: if (m_source) { Chris@59: std::cerr << "AudioPortAudioTarget: block size " << m_bufferSize << std::endl; Chris@91: m_source->setTarget(this, m_bufferSize); Chris@59: m_source->setTargetSampleRate(m_sampleRate); Chris@59: m_source->setTargetPlayLatency(m_latency); Chris@59: } Chris@59: Chris@59: #ifdef DEBUG_PORT_AUDIO_TARGET Chris@59: std::cerr << "AudioPortAudioTarget: initialised OK" << std::endl; Chris@59: #endif Chris@59: } Chris@59: Chris@59: AudioPortAudioTarget::~AudioPortAudioTarget() Chris@59: { Chris@70: std::cerr << "AudioPortAudioTarget::~AudioPortAudioTarget()" << std::endl; Chris@70: Chris@91: if (m_source) { Chris@91: m_source->setTarget(0, m_bufferSize); Chris@91: } Chris@91: Chris@70: shutdown(); Chris@70: Chris@59: if (m_stream) { Chris@70: Chris@70: std::cerr << "closing stream" << std::endl; Chris@70: Chris@59: PaError err; Chris@59: err = Pa_CloseStream(m_stream); Chris@59: if (err != paNoError) { Chris@59: std::cerr << "ERROR: AudioPortAudioTarget: Failed to close PortAudio stream: " << Pa_GetErrorText(err) << std::endl; Chris@59: } Chris@70: Chris@70: std::cerr << "terminating" << std::endl; Chris@70: Chris@59: err = Pa_Terminate(); Chris@59: if (err != paNoError) { Chris@59: std::cerr << "ERROR: AudioPortAudioTarget: Failed to terminate PortAudio: " << Pa_GetErrorText(err) << std::endl; Chris@59: } Chris@59: } Chris@70: Chris@70: m_stream = 0; Chris@70: Chris@70: std::cerr << "AudioPortAudioTarget::~AudioPortAudioTarget() done" << std::endl; Chris@70: } Chris@70: Chris@70: void Chris@70: AudioPortAudioTarget::shutdown() Chris@70: { Chris@177: #ifdef DEBUG_PORT_AUDIO_TARGET Chris@177: std::cerr << "AudioPortAudioTarget::shutdown" << std::endl; Chris@177: #endif Chris@70: m_done = true; Chris@59: } Chris@59: Chris@59: bool Chris@59: AudioPortAudioTarget::isOK() const Chris@59: { Chris@59: return (m_stream != 0); Chris@59: } Chris@59: Chris@91: double Chris@91: AudioPortAudioTarget::getCurrentTime() const Chris@91: { Chris@91: if (!m_stream) return 0.0; Chris@91: else return Pa_GetStreamTime(m_stream); Chris@91: } Chris@91: Chris@59: int Chris@59: AudioPortAudioTarget::processStatic(const void *input, void *output, Chris@59: unsigned long nframes, Chris@59: const PaStreamCallbackTimeInfo *timeInfo, Chris@59: PaStreamCallbackFlags flags, void *data) Chris@59: { Chris@59: return ((AudioPortAudioTarget *)data)->process(input, output, Chris@59: nframes, timeInfo, Chris@59: flags); Chris@59: } Chris@59: Chris@59: void Chris@59: AudioPortAudioTarget::sourceModelReplaced() Chris@59: { Chris@59: m_source->setTargetSampleRate(m_sampleRate); Chris@59: } Chris@59: Chris@59: int Chris@59: AudioPortAudioTarget::process(const void *, void *outputBuffer, Chris@59: unsigned long nframes, Chris@59: const PaStreamCallbackTimeInfo *, Chris@59: PaStreamCallbackFlags) Chris@59: { Chris@59: #ifdef DEBUG_AUDIO_PORT_AUDIO_TARGET Chris@177: std::cerr << "AudioPortAudioTarget::process(" << nframes << ")" << std::endl; Chris@59: #endif Chris@59: Chris@177: if (!m_source || m_done) { Chris@177: #ifdef DEBUG_AUDIO_PORT_AUDIO_TARGET Chris@177: std::cerr << "AudioPortAudioTarget::process: Doing nothing, no source or application done" << std::endl; Chris@177: #endif Chris@177: return 0; Chris@177: } Chris@59: Chris@59: float *output = (float *)outputBuffer; Chris@59: Chris@59: assert(nframes <= m_bufferSize); Chris@59: Chris@59: static float **tmpbuf = 0; Chris@59: static size_t tmpbufch = 0; Chris@59: static size_t tmpbufsz = 0; Chris@59: Chris@59: size_t sourceChannels = m_source->getSourceChannelCount(); Chris@59: Chris@59: // Because we offer pan, we always want at least 2 channels Chris@59: if (sourceChannels < 2) sourceChannels = 2; Chris@59: Chris@59: if (!tmpbuf || tmpbufch != sourceChannels || int(tmpbufsz) < m_bufferSize) { Chris@59: Chris@59: if (tmpbuf) { Chris@59: for (size_t i = 0; i < tmpbufch; ++i) { Chris@59: delete[] tmpbuf[i]; Chris@59: } Chris@59: delete[] tmpbuf; Chris@59: } Chris@59: Chris@59: tmpbufch = sourceChannels; Chris@59: tmpbufsz = m_bufferSize; Chris@59: tmpbuf = new float *[tmpbufch]; Chris@59: Chris@59: for (size_t i = 0; i < tmpbufch; ++i) { Chris@59: tmpbuf[i] = new float[tmpbufsz]; Chris@59: } Chris@59: } Chris@59: Chris@59: size_t received = m_source->getSourceSamples(nframes, tmpbuf); Chris@59: Chris@59: float peakLeft = 0.0, peakRight = 0.0; Chris@59: Chris@59: for (size_t ch = 0; ch < 2; ++ch) { Chris@59: Chris@59: float peak = 0.0; Chris@59: Chris@59: if (ch < sourceChannels) { Chris@59: Chris@59: // PortAudio samples are interleaved Chris@59: for (size_t i = 0; i < nframes; ++i) { Chris@59: if (i < received) { Chris@59: output[i * 2 + ch] = tmpbuf[ch][i] * m_outputGain; Chris@59: float sample = fabsf(output[i * 2 + ch]); Chris@59: if (sample > peak) peak = sample; Chris@59: } else { Chris@59: output[i * 2 + ch] = 0; Chris@59: } Chris@59: } Chris@59: Chris@59: } else if (ch == 1 && sourceChannels == 1) { Chris@59: Chris@59: for (size_t i = 0; i < nframes; ++i) { Chris@59: if (i < received) { Chris@59: output[i * 2 + ch] = tmpbuf[0][i] * m_outputGain; Chris@59: float sample = fabsf(output[i * 2 + ch]); Chris@59: if (sample > peak) peak = sample; Chris@59: } else { Chris@59: output[i * 2 + ch] = 0; Chris@59: } Chris@59: } Chris@59: Chris@59: } else { Chris@59: for (size_t i = 0; i < nframes; ++i) { Chris@59: output[i * 2 + ch] = 0; Chris@59: } Chris@59: } Chris@59: Chris@59: if (ch == 0) peakLeft = peak; Chris@59: if (ch > 0 || sourceChannels == 1) peakRight = peak; Chris@59: } Chris@59: Chris@59: m_source->setOutputLevels(peakLeft, peakRight); Chris@59: Chris@130: if (Pa_GetStreamCpuLoad(m_stream) > 0.7) { Chris@130: if (m_source) m_source->audioProcessingOverload(); Chris@130: } Chris@130: Chris@59: return 0; Chris@59: } Chris@59: Chris@59: #endif /* HAVE_PORTAUDIO */ Chris@59: