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@182: #ifndef _WIN32 Chris@182: #include Chris@182: #endif Chris@182: 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@182: m_prioritySet(false), Chris@70: m_done(false) Chris@59: { Chris@59: PaError err; Chris@59: Chris@59: #ifdef DEBUG_AUDIO_PORT_AUDIO_TARGET Chris@293: cerr << "AudioPortAudioTarget: Initialising for PortAudio v19" << endl; Chris@59: #endif Chris@59: Chris@59: err = Pa_Initialize(); Chris@59: if (err != paNoError) { Chris@293: cerr << "ERROR: AudioPortAudioTarget: Failed to initialize PortAudio: " << Pa_GetErrorText(err) << 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@436: m_sampleRate = int(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@245: 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@293: cerr << "WARNING: AudioPortAudioTarget: Failed to open PortAudio stream with default frames per buffer, trying again with fixed frames per buffer..." << 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@293: cerr << "ERROR: AudioPortAudioTarget: Failed to open PortAudio stream: " << Pa_GetErrorText(err) << endl; Chris@293: cerr << "Note: device ID was " << op.device << 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@293: cerr << "PortAudio latency = " << m_latency << " frames" << endl; Chris@59: Chris@59: err = Pa_StartStream(m_stream); Chris@59: Chris@59: if (err != paNoError) { Chris@293: cerr << "ERROR: AudioPortAudioTarget: Failed to start PortAudio stream: " << Pa_GetErrorText(err) << 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@293: cerr << "AudioPortAudioTarget: block size " << m_bufferSize << 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@293: cerr << "AudioPortAudioTarget: initialised OK" << endl; Chris@59: #endif Chris@59: } Chris@59: Chris@59: AudioPortAudioTarget::~AudioPortAudioTarget() Chris@59: { Chris@233: SVDEBUG << "AudioPortAudioTarget::~AudioPortAudioTarget()" << 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@233: SVDEBUG << "closing stream" << endl; Chris@70: Chris@59: PaError err; Chris@59: err = Pa_CloseStream(m_stream); Chris@59: if (err != paNoError) { Chris@293: cerr << "ERROR: AudioPortAudioTarget: Failed to close PortAudio stream: " << Pa_GetErrorText(err) << endl; Chris@59: } Chris@70: Chris@293: cerr << "terminating" << endl; Chris@70: Chris@59: err = Pa_Terminate(); Chris@59: if (err != paNoError) { Chris@293: cerr << "ERROR: AudioPortAudioTarget: Failed to terminate PortAudio: " << Pa_GetErrorText(err) << endl; Chris@59: } Chris@59: } Chris@70: Chris@70: m_stream = 0; Chris@70: Chris@233: SVDEBUG << "AudioPortAudioTarget::~AudioPortAudioTarget() done" << endl; Chris@70: } Chris@70: Chris@70: void Chris@70: AudioPortAudioTarget::shutdown() Chris@70: { Chris@177: #ifdef DEBUG_PORT_AUDIO_TARGET Chris@233: SVDEBUG << "AudioPortAudioTarget::shutdown" << 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@436: sv_frame_t nframes, Chris@59: const PaStreamCallbackTimeInfo *, Chris@59: PaStreamCallbackFlags) Chris@59: { Chris@59: #ifdef DEBUG_AUDIO_PORT_AUDIO_TARGET Chris@233: SVDEBUG << "AudioPortAudioTarget::process(" << nframes << ")" << endl; Chris@59: #endif Chris@59: Chris@177: if (!m_source || m_done) { Chris@177: #ifdef DEBUG_AUDIO_PORT_AUDIO_TARGET Chris@233: SVDEBUG << "AudioPortAudioTarget::process: Doing nothing, no source or application done" << endl; Chris@177: #endif Chris@177: return 0; Chris@177: } Chris@59: Chris@182: if (!m_prioritySet) { Chris@182: #ifndef _WIN32 Chris@182: sched_param param; Chris@182: param.sched_priority = 20; Chris@182: if (pthread_setschedparam(pthread_self(), SCHED_RR, ¶m)) { Chris@233: SVDEBUG << "AudioPortAudioTarget: NOTE: couldn't set RT scheduling class" << endl; Chris@182: } else { Chris@233: SVDEBUG << "AudioPortAudioTarget: NOTE: successfully set RT scheduling class" << endl; Chris@182: } Chris@182: #endif Chris@182: m_prioritySet = true; Chris@182: } Chris@182: Chris@59: float *output = (float *)outputBuffer; Chris@59: Chris@59: assert(nframes <= m_bufferSize); Chris@59: Chris@59: static float **tmpbuf = 0; Chris@366: static int tmpbufch = 0; Chris@366: static int tmpbufsz = 0; Chris@59: Chris@366: int 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@366: for (int 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@366: for (int i = 0; i < tmpbufch; ++i) { Chris@59: tmpbuf[i] = new float[tmpbufsz]; Chris@59: } Chris@59: } Chris@59: Chris@436: sv_frame_t received = m_source->getSourceSamples(nframes, tmpbuf); Chris@59: Chris@59: float peakLeft = 0.0, peakRight = 0.0; Chris@59: Chris@366: for (int 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@366: for (int 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@366: for (int 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@366: for (int 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: