mirror of
https://github.com/thestk/stk
synced 2026-01-16 06:21:51 +00:00
Version 4.2.0
This commit is contained in:
committed by
Stephen Sinclair
parent
cf06b7598b
commit
a6381b9d38
144
src/RtWvOut.cpp
144
src/RtWvOut.cpp
@@ -1,6 +1,6 @@
|
||||
/***************************************************/
|
||||
/*! \class RtWvOut
|
||||
\brief STK realtime audio output class.
|
||||
\brief STK realtime audio (blocking) output class.
|
||||
|
||||
This class provides a simplified interface to
|
||||
RtAudio for realtime audio output. It is a
|
||||
@@ -14,19 +14,19 @@
|
||||
takes a pointer to multi-channel sample
|
||||
frame data.
|
||||
|
||||
by Perry R. Cook and Gary P. Scavone, 1995 - 2002.
|
||||
by Perry R. Cook and Gary P. Scavone, 1995 - 2004.
|
||||
*/
|
||||
/***************************************************/
|
||||
|
||||
#include "RtWvOut.h"
|
||||
#include <iostream>
|
||||
|
||||
RtWvOut :: RtWvOut( unsigned int nChannels, MY_FLOAT sampleRate, int device, int bufferFrames, int nBuffers )
|
||||
RtWvOut :: RtWvOut( unsigned int nChannels, StkFloat sampleRate, int device, int bufferFrames, int nBuffers )
|
||||
{
|
||||
// We'll let RtAudio deal with channel and srate limitations.
|
||||
channels = nChannels;
|
||||
channels_ = nChannels;
|
||||
bufferSize_ = bufferFrames;
|
||||
RtAudioFormat format = ( sizeof(MY_FLOAT) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32;
|
||||
RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32;
|
||||
|
||||
audio_ = 0;
|
||||
try {
|
||||
@@ -36,11 +36,11 @@ RtWvOut :: RtWvOut( unsigned int nChannels, MY_FLOAT sampleRate, int device, int
|
||||
handleError( error.getMessageString(), StkError::AUDIO_SYSTEM );
|
||||
}
|
||||
|
||||
// Now open a stream and set the callback function.
|
||||
// Now open a stream and get the buffer pointer.
|
||||
try {
|
||||
audio_->openStream(device, (int)channels, 0, 0, format,
|
||||
audio_->openStream(device, (int)channels_, 0, 0, format,
|
||||
(int)sampleRate, &bufferSize_, nBuffers);
|
||||
data = (MY_FLOAT *) audio_->getStreamBuffer();
|
||||
dataPtr_ = (StkFloat *) audio_->getStreamBuffer();
|
||||
}
|
||||
catch (RtError &error) {
|
||||
handleError( error.getMessageString(), StkError::AUDIO_SYSTEM );
|
||||
@@ -54,7 +54,6 @@ RtWvOut :: ~RtWvOut()
|
||||
if ( !stopped_ )
|
||||
audio_->stopStream();
|
||||
delete audio_;
|
||||
data = 0;
|
||||
}
|
||||
|
||||
void RtWvOut :: start()
|
||||
@@ -75,62 +74,155 @@ void RtWvOut :: stop()
|
||||
|
||||
unsigned long RtWvOut :: getFrames( void ) const
|
||||
{
|
||||
return totalCount;
|
||||
return totalCount_;
|
||||
}
|
||||
|
||||
MY_FLOAT RtWvOut :: getTime( void ) const
|
||||
StkFloat RtWvOut :: getTime( void ) const
|
||||
{
|
||||
return (MY_FLOAT) totalCount / Stk::sampleRate();
|
||||
return (StkFloat) totalCount_ / Stk::sampleRate();
|
||||
}
|
||||
|
||||
void RtWvOut :: tick(const MY_FLOAT sample)
|
||||
void RtWvOut :: tick( const StkFloat sample )
|
||||
{
|
||||
if ( stopped_ )
|
||||
start();
|
||||
|
||||
for ( unsigned int j=0; j<channels; j++ )
|
||||
data[counter*channels+j] = sample;
|
||||
StkFloat input = sample;
|
||||
this->clipTest( input );
|
||||
for ( unsigned int j=0; j<channels_; j++ )
|
||||
dataPtr_[counter_*channels_+j] = input;
|
||||
|
||||
counter++;
|
||||
totalCount++;
|
||||
counter_++;
|
||||
totalCount_++;
|
||||
|
||||
if ( counter >= (unsigned int )bufferSize_ ) {
|
||||
if ( counter_ >= (unsigned int )bufferSize_ ) {
|
||||
try {
|
||||
audio_->tickStream();
|
||||
}
|
||||
catch (RtError &error) {
|
||||
handleError( error.getMessageString(), StkError::AUDIO_SYSTEM );
|
||||
}
|
||||
counter = 0;
|
||||
counter_ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void RtWvOut :: tick(const MY_FLOAT *vector, unsigned int vectorSize)
|
||||
void RtWvOut :: tick( const StkFloat *vector, unsigned int vectorSize )
|
||||
{
|
||||
for (unsigned int i=0; i<vectorSize; i++)
|
||||
tick( vector[i] );
|
||||
}
|
||||
|
||||
void RtWvOut :: tickFrame(const MY_FLOAT *frameVector, unsigned int frames)
|
||||
void RtWvOut :: tick( const StkFrames& frames, unsigned int channel )
|
||||
{
|
||||
if ( channel == 0 || frames.channels() < channel ) {
|
||||
errorString_ << "RtWvOut::tick(): channel argument (" << channel << ") is zero or > channels in StkFrames argument!";
|
||||
handleError( StkError::FUNCTION_ARGUMENT );
|
||||
}
|
||||
|
||||
if ( stopped_ )
|
||||
start();
|
||||
|
||||
if ( frames.channels() == 1 ) {
|
||||
for ( unsigned int i=0; i<frames.frames(); i++ )
|
||||
tick( frames[i] );
|
||||
}
|
||||
else if ( frames.interleaved() ) {
|
||||
unsigned int hop = frames.channels();
|
||||
unsigned int index = channel - 1;
|
||||
for ( unsigned int i=0; i<frames.frames(); i++ ) {
|
||||
tick( frames[index] );
|
||||
index += hop;
|
||||
}
|
||||
}
|
||||
else {
|
||||
unsigned int iStart = (channel - 1) * frames.frames();
|
||||
for ( unsigned int i=0; i<frames.frames(); i++ )
|
||||
tick( frames[iStart + i] );
|
||||
}
|
||||
}
|
||||
|
||||
void RtWvOut :: tickFrame( const StkFloat *frameVector, unsigned int frames )
|
||||
{
|
||||
if ( stopped_ )
|
||||
start();
|
||||
|
||||
StkFloat sample;
|
||||
for ( unsigned int i=0; i<frames; i++ ) {
|
||||
for ( unsigned int j=0; j<channels; j++ ) {
|
||||
data[counter*channels+j] = frameVector[i*channels+j];
|
||||
for ( unsigned int j=0; j<channels_; j++ ) {
|
||||
sample = frameVector[i*channels_+j];
|
||||
this->clipTest( sample );
|
||||
dataPtr_[counter_*channels_+j] = sample;
|
||||
}
|
||||
counter++;
|
||||
counter_++;
|
||||
totalCount_++;
|
||||
|
||||
if ( counter >= (unsigned int)bufferSize_ ) {
|
||||
if ( counter_ >= (unsigned int)bufferSize_ ) {
|
||||
try {
|
||||
audio_->tickStream();
|
||||
}
|
||||
catch (RtError &error) {
|
||||
handleError( error.getMessageString(), StkError::AUDIO_SYSTEM );
|
||||
}
|
||||
counter = 0;
|
||||
counter_ = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void RtWvOut :: tickFrame( const StkFrames& frames )
|
||||
{
|
||||
if ( channels_ != frames.channels() ) {
|
||||
errorString_ << "RtWvOut::tickFrame(): incompatible channel value in StkFrames argument!";
|
||||
handleError( StkError::FUNCTION_ARGUMENT );
|
||||
}
|
||||
|
||||
if ( stopped_ )
|
||||
start();
|
||||
|
||||
unsigned int j;
|
||||
StkFloat sample;
|
||||
if ( channels_ == 1 || frames.interleaved() ) {
|
||||
unsigned long iFrames = 0, iData = counter_;
|
||||
for ( unsigned int i=0; i<frames.frames(); i++ ) {
|
||||
for ( j=0; j<channels_; j++ ) {
|
||||
sample = frames[iFrames++];
|
||||
this->clipTest( sample );
|
||||
dataPtr_[iData++] = sample;
|
||||
}
|
||||
counter_++;
|
||||
totalCount_++;
|
||||
|
||||
if ( counter_ >= (unsigned int)bufferSize_ ) {
|
||||
try {
|
||||
audio_->tickStream();
|
||||
}
|
||||
catch (RtError &error) {
|
||||
handleError( error.getMessageString(), StkError::AUDIO_SYSTEM );
|
||||
}
|
||||
counter_ = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
unsigned int hop = frames.frames();
|
||||
unsigned long iData = counter_;
|
||||
for ( unsigned int i=0; i<frames.frames(); i++ ) {
|
||||
for ( j=0; j<channels_; j++ ) {
|
||||
sample = frames[i + j*hop];
|
||||
this->clipTest( sample );
|
||||
dataPtr_[iData++] = sample;
|
||||
}
|
||||
counter_++;
|
||||
totalCount_++;
|
||||
|
||||
if ( counter_ >= (unsigned int)bufferSize_ ) {
|
||||
try {
|
||||
audio_->tickStream();
|
||||
}
|
||||
catch (RtError &error) {
|
||||
handleError( error.getMessageString(), StkError::AUDIO_SYSTEM );
|
||||
}
|
||||
counter_ = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user