Chris@0: /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@0: A waveform viewer and audio annotation editor. Chris@2: Chris Cannam, Queen Mary University of London, 2005-2006 Chris@0: Chris@0: This is experimental software. Not for distribution. 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@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: