Created
September 29, 2018 18:55
-
-
Save jpcima/32f0a77067927079cc79531c6fd075ad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/AudioFileSet.cpp b/AudioFileSet.cpp | |
index efa93c9..b08ca4e 100644 | |
--- a/AudioFileSet.cpp | |
+++ b/AudioFileSet.cpp | |
@@ -29,6 +29,8 @@ | |
#include "AudioFileSet.h" | |
+extern unsigned int samp_rate; | |
+ | |
//--------------------------------------------------------------------------- | |
// Destructor | |
//--------------------------------------------------------------------------- | |
@@ -125,8 +127,8 @@ int AudioFileSet::loadFileSet(string localPath) | |
cout << " Format: " << sfinfo.format << endl; | |
//warn about sampling rate incompatibility | |
- if (sfinfo.samplerate != MY_SRATE){ | |
- printf("\nWARNING: '%s' is sampled at a different rate from the current sample rate of %i\n",theFileName.c_str(),MY_SRATE); | |
+ if (sfinfo.samplerate != ::samp_rate){ | |
+ printf("\nWARNING: '%s' is sampled at a different rate from the current sample rate of %i\n",theFileName.c_str(),::samp_rate); | |
} | |
//MONO CONVERSION SET ASIDE FOR NOW... number of channels for each file is dealt with | |
@@ -192,6 +194,8 @@ int AudioFileSet::loadFileSet(string localPath) | |
perror (""); | |
return 1; | |
} | |
+ | |
+ return 0; | |
} | |
diff --git a/Borderlands.cpp b/Borderlands.cpp | |
index c8d5f77..be04f4e 100644 | |
--- a/Borderlands.cpp | |
+++ b/Borderlands.cpp | |
@@ -114,9 +114,12 @@ vector<GrainClusterVis *> * grainCloudVis; | |
//cloud counter | |
unsigned int numClouds = 0; | |
+//sample rate - Hz | |
+unsigned int samp_rate = 0; | |
+ | |
//global time increment - samples per second | |
//global time is incremented in audio callback | |
-const double samp_time_sec = (double) 1.0 / (double)MY_SRATE; | |
+double samp_time_sec = 0; | |
//Initial camera movement vars | |
@@ -1566,6 +1569,36 @@ int main (int argc, char ** argv) | |
//init random number generator | |
srand(time(NULL)); | |
//start time | |
+ | |
+ //-------------Audio Configuration-----------// | |
+ | |
+ //configure RtAudio | |
+ //create the object | |
+ try { | |
+ theAudio = new MyRtAudio(1,MY_CHANNELS, &g_buffSize, MY_FORMAT,true); | |
+ } catch (RtError & err) { | |
+ err.printMessage(); | |
+ exit(1); | |
+ } | |
+ try | |
+ { | |
+ unsigned sampleRate = theAudio->getSampleRate(); | |
+ ::samp_rate = sampleRate; | |
+ ::samp_time_sec = 1.0 / sampleRate; | |
+ Stk::setSampleRate(sampleRate); | |
+ //open audio stream/assign callback | |
+ theAudio->openStream(&audioCallback); | |
+ //get new buffer size | |
+ g_buffSize = theAudio->getBufferSize(); | |
+ //report latency | |
+ theAudio->reportStreamLatency(); | |
+ | |
+ }catch (RtError & err ) | |
+ { | |
+ err.printMessage(); | |
+ cleaningFunction(); | |
+ exit(1); | |
+ } | |
//-------------Graphics Initialization--------// | |
@@ -1621,32 +1654,9 @@ int main (int argc, char ** argv) | |
- //-------------Audio Configuration-----------// | |
- | |
- //configure RtAudio | |
- //create the object | |
- try { | |
- theAudio = new MyRtAudio(1,MY_CHANNELS, MY_SRATE, &g_buffSize, MY_FORMAT,true); | |
- } catch (RtError & err) { | |
- err.printMessage(); | |
- exit(1); | |
- } | |
- try | |
- { | |
- //open audio stream/assign callback | |
- theAudio->openStream(&audioCallback); | |
- //get new buffer size | |
- g_buffSize = theAudio->getBufferSize(); | |
- //start audio stream | |
- theAudio->startStream(); | |
- //report latency | |
- theAudio->reportStreamLatency(); | |
- | |
- }catch (RtError & err ) | |
- { | |
- err.printMessage(); | |
- goto cleanup; | |
- } | |
+ | |
+ //start audio stream | |
+ theAudio->startStream(); | |
diff --git a/GrainCluster.cpp b/GrainCluster.cpp | |
index 02509c9..e1a9a3a 100644 | |
--- a/GrainCluster.cpp | |
+++ b/GrainCluster.cpp | |
@@ -28,7 +28,7 @@ | |
#include "GrainCluster.h" | |
- | |
+extern unsigned int samp_rate; | |
//Destructor | |
@@ -128,7 +128,7 @@ GrainCluster::GrainCluster(vector<AudioFile*> * soundSet, float theNumVoices) | |
// setDirection(RANDOM_DIR); | |
//initialize trigger time (samples) | |
- bang_time = duration * MY_SRATE * (double) 0.001 / overlap; | |
+ bang_time = duration * ::samp_rate * (double) 0.001 / overlap; | |
//load grains | |
for (int i = 0; i < myGrains->size(); i++){ | |
@@ -244,7 +244,7 @@ void GrainCluster::setDurationMs(float theDur) | |
//update internal grain trigger time | |
void GrainCluster::updateBangTime(){ | |
- bang_time = duration * MY_SRATE * (double) 0.001 / overlap; | |
+ bang_time = duration * ::samp_rate * (double) 0.001 / overlap; | |
//cout << "duration: " << duration << ", new bang time " << bang_time << endl; | |
} | |
diff --git a/GrainVoice.cpp b/GrainVoice.cpp | |
index e0e3def..2b6d15e 100644 | |
--- a/GrainVoice.cpp | |
+++ b/GrainVoice.cpp | |
@@ -30,6 +30,8 @@ | |
#include "GrainVoice.h" | |
+extern unsigned int samp_rate; | |
+ | |
//-------------------AUDIO----------------------------------------------------// | |
//----------------------------------------------------------------------------- | |
@@ -143,7 +145,7 @@ GrainVoice::GrainVoice(vector<AudioFile *> * soundSet,float durationMs,float the | |
winReader = 0.0; //0 idx | |
//get duration in samples (fractional) | |
- winDurationSamps = ceil(duration * MY_SRATE * (double) 0.001); | |
+ winDurationSamps = ceil(duration * ::samp_rate * (double) 0.001); | |
winInc = (double)WINDOW_LEN / winDurationSamps; | |
} | |
@@ -292,7 +294,7 @@ void GrainVoice::updateParams() | |
window = Window::Instance().getWindow(windowType); | |
//double value, but eliminate fractional component - | |
- winDurationSamps = ceil(duration * MY_SRATE * (double) 0.001); | |
+ winDurationSamps = ceil(duration * ::samp_rate * (double) 0.001); | |
//how far should we advance through windowing function each sample | |
winInc = (double)WINDOW_LEN / winDurationSamps; | |
diff --git a/MyRtAudio.cpp b/MyRtAudio.cpp | |
index ef60ac2..1a3e525 100644 | |
--- a/MyRtAudio.cpp | |
+++ b/MyRtAudio.cpp | |
@@ -40,7 +40,7 @@ MyRtAudio::~MyRtAudio() | |
//cerr << "rtaudio cleanup reached " << endl; | |
} | |
-MyRtAudio::MyRtAudio(unsigned int numIns, unsigned int numOuts, unsigned int srate, unsigned int * bufferSize, RtAudioFormat format,bool showWarnings) | |
+MyRtAudio::MyRtAudio(unsigned int numIns, unsigned int numOuts, unsigned int * bufferSize, RtAudioFormat format,bool showWarnings) | |
{ | |
//configure RtAudio | |
//create pointer to RtAudio object | |
@@ -64,6 +64,8 @@ MyRtAudio::MyRtAudio(unsigned int numIns, unsigned int numOuts, unsigned int sra | |
cout << "No Audio Devices Found!" << endl; | |
exit( 1 ); | |
} | |
+ const RtAudio::DeviceInfo &info = audio->getDeviceInfo( audio->getDefaultOutputDevice() ); | |
+ | |
//allow RtAudio to print msgs to stderr | |
audio->showWarnings( showWarnings ); | |
@@ -72,7 +74,7 @@ MyRtAudio::MyRtAudio(unsigned int numIns, unsigned int numOuts, unsigned int sra | |
myBufferSize = bufferSize; | |
//set sample rate; | |
- mySRate = srate; | |
+ mySRate = info.preferredSampleRate; | |
//set format | |
myFormat = format; | |
@@ -86,6 +88,7 @@ void MyRtAudio::openStream( RtAudioCallback callback){ | |
//create stream options | |
RtAudio::StreamOptions options; | |
+ options.streamName = "Borderlands"; | |
RtAudio::StreamParameters iParams, oParams; | |
//i/o params | |
@@ -102,6 +105,12 @@ void MyRtAudio::openStream( RtAudioCallback callback){ | |
} | |
+//report the current sample rate | |
+unsigned int MyRtAudio::getSampleRate() | |
+{ | |
+ return mySRate; | |
+} | |
+ | |
//report the current buffer size | |
unsigned int MyRtAudio::getBufferSize(){ | |
diff --git a/MyRtAudio.h b/MyRtAudio.h | |
index 01756a7..4ea58fa 100644 | |
--- a/MyRtAudio.h | |
+++ b/MyRtAudio.h | |
@@ -48,12 +48,15 @@ public: | |
virtual ~MyRtAudio(); | |
//constructor - args = | |
- MyRtAudio(unsigned int numIns, unsigned int numOuts, unsigned int srate, unsigned int * bufferSize, RtAudioFormat format,bool showWarnings); | |
+ MyRtAudio(unsigned int numIns, unsigned int numOuts, unsigned int * bufferSize, RtAudioFormat format,bool showWarnings); | |
//set the audio callback and start the audio stream | |
void openStream( RtAudioCallback callback); | |
+ //report the current sample rate | |
+ unsigned int getSampleRate(); | |
+ | |
//report the current buffer size | |
unsigned int getBufferSize(); | |
diff --git a/theglobals.h b/theglobals.h | |
index e846b0e..1b8ebef 100644 | |
--- a/theglobals.h | |
+++ b/theglobals.h | |
@@ -40,8 +40,6 @@ | |
#define SAMPLE double | |
//create rtaudio format | |
#define MY_FORMAT RTAUDIO_FLOAT64 | |
-//set the sample rate | |
-#define MY_SRATE 44100 | |
//number of output channels | |
#define MY_CHANNELS 2 | |
@@ -81,4 +79,4 @@ static const double globalAtten = 0.5; | |
//}; | |
-#endif | |
\ No newline at end of file | |
+#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/AudioFileSet.cpp b/AudioFileSet.cpp | |
index efa93c9..b08ca4e 100644 | |
--- a/AudioFileSet.cpp | |
+++ b/AudioFileSet.cpp | |
@@ -29,6 +29,8 @@ | |
#include "AudioFileSet.h" | |
+extern unsigned int samp_rate; | |
+ | |
//--------------------------------------------------------------------------- | |
// Destructor | |
//--------------------------------------------------------------------------- | |
@@ -125,8 +127,8 @@ int AudioFileSet::loadFileSet(string localPath) | |
cout << " Format: " << sfinfo.format << endl; | |
//warn about sampling rate incompatibility | |
- if (sfinfo.samplerate != MY_SRATE){ | |
- printf("\nWARNING: '%s' is sampled at a different rate from the current sample rate of %i\n",theFileName.c_str(),MY_SRATE); | |
+ if (sfinfo.samplerate != ::samp_rate){ | |
+ printf("\nWARNING: '%s' is sampled at a different rate from the current sample rate of %i\n",theFileName.c_str(),::samp_rate); | |
} | |
//MONO CONVERSION SET ASIDE FOR NOW... number of channels for each file is dealt with | |
@@ -192,6 +194,8 @@ int AudioFileSet::loadFileSet(string localPath) | |
perror (""); | |
return 1; | |
} | |
+ | |
+ return 0; | |
} | |
diff --git a/Borderlands.cpp b/Borderlands.cpp | |
index c8d5f77..be04f4e 100644 | |
--- a/Borderlands.cpp | |
+++ b/Borderlands.cpp | |
@@ -114,9 +114,12 @@ vector<GrainClusterVis *> * grainCloudVis; | |
//cloud counter | |
unsigned int numClouds = 0; | |
+//sample rate - Hz | |
+unsigned int samp_rate = 0; | |
+ | |
//global time increment - samples per second | |
//global time is incremented in audio callback | |
-const double samp_time_sec = (double) 1.0 / (double)MY_SRATE; | |
+double samp_time_sec = 0; | |
//Initial camera movement vars | |
@@ -1566,6 +1569,36 @@ int main (int argc, char ** argv) | |
//init random number generator | |
srand(time(NULL)); | |
//start time | |
+ | |
+ //-------------Audio Configuration-----------// | |
+ | |
+ //configure RtAudio | |
+ //create the object | |
+ try { | |
+ theAudio = new MyRtAudio(1,MY_CHANNELS, &g_buffSize, MY_FORMAT,true); | |
+ } catch (RtError & err) { | |
+ err.printMessage(); | |
+ exit(1); | |
+ } | |
+ try | |
+ { | |
+ unsigned sampleRate = theAudio->getSampleRate(); | |
+ ::samp_rate = sampleRate; | |
+ ::samp_time_sec = 1.0 / sampleRate; | |
+ Stk::setSampleRate(sampleRate); | |
+ //open audio stream/assign callback | |
+ theAudio->openStream(&audioCallback); | |
+ //get new buffer size | |
+ g_buffSize = theAudio->getBufferSize(); | |
+ //report latency | |
+ theAudio->reportStreamLatency(); | |
+ | |
+ }catch (RtError & err ) | |
+ { | |
+ err.printMessage(); | |
+ cleaningFunction(); | |
+ exit(1); | |
+ } | |
//-------------Graphics Initialization--------// | |
@@ -1621,32 +1654,9 @@ int main (int argc, char ** argv) | |
- //-------------Audio Configuration-----------// | |
- | |
- //configure RtAudio | |
- //create the object | |
- try { | |
- theAudio = new MyRtAudio(1,MY_CHANNELS, MY_SRATE, &g_buffSize, MY_FORMAT,true); | |
- } catch (RtError & err) { | |
- err.printMessage(); | |
- exit(1); | |
- } | |
- try | |
- { | |
- //open audio stream/assign callback | |
- theAudio->openStream(&audioCallback); | |
- //get new buffer size | |
- g_buffSize = theAudio->getBufferSize(); | |
- //start audio stream | |
- theAudio->startStream(); | |
- //report latency | |
- theAudio->reportStreamLatency(); | |
- | |
- }catch (RtError & err ) | |
- { | |
- err.printMessage(); | |
- goto cleanup; | |
- } | |
+ | |
+ //start audio stream | |
+ theAudio->startStream(); | |
diff --git a/GrainCluster.cpp b/GrainCluster.cpp | |
index 02509c9..e1a9a3a 100644 | |
--- a/GrainCluster.cpp | |
+++ b/GrainCluster.cpp | |
@@ -28,7 +28,7 @@ | |
#include "GrainCluster.h" | |
- | |
+extern unsigned int samp_rate; | |
//Destructor | |
@@ -128,7 +128,7 @@ GrainCluster::GrainCluster(vector<AudioFile*> * soundSet, float theNumVoices) | |
// setDirection(RANDOM_DIR); | |
//initialize trigger time (samples) | |
- bang_time = duration * MY_SRATE * (double) 0.001 / overlap; | |
+ bang_time = duration * ::samp_rate * (double) 0.001 / overlap; | |
//load grains | |
for (int i = 0; i < myGrains->size(); i++){ | |
@@ -244,7 +244,7 @@ void GrainCluster::setDurationMs(float theDur) | |
//update internal grain trigger time | |
void GrainCluster::updateBangTime(){ | |
- bang_time = duration * MY_SRATE * (double) 0.001 / overlap; | |
+ bang_time = duration * ::samp_rate * (double) 0.001 / overlap; | |
//cout << "duration: " << duration << ", new bang time " << bang_time << endl; | |
} | |
diff --git a/GrainVoice.cpp b/GrainVoice.cpp | |
index e0e3def..2b6d15e 100644 | |
--- a/GrainVoice.cpp | |
+++ b/GrainVoice.cpp | |
@@ -30,6 +30,8 @@ | |
#include "GrainVoice.h" | |
+extern unsigned int samp_rate; | |
+ | |
//-------------------AUDIO----------------------------------------------------// | |
//----------------------------------------------------------------------------- | |
@@ -143,7 +145,7 @@ GrainVoice::GrainVoice(vector<AudioFile *> * soundSet,float durationMs,float the | |
winReader = 0.0; //0 idx | |
//get duration in samples (fractional) | |
- winDurationSamps = ceil(duration * MY_SRATE * (double) 0.001); | |
+ winDurationSamps = ceil(duration * ::samp_rate * (double) 0.001); | |
winInc = (double)WINDOW_LEN / winDurationSamps; | |
} | |
@@ -292,7 +294,7 @@ void GrainVoice::updateParams() | |
window = Window::Instance().getWindow(windowType); | |
//double value, but eliminate fractional component - | |
- winDurationSamps = ceil(duration * MY_SRATE * (double) 0.001); | |
+ winDurationSamps = ceil(duration * ::samp_rate * (double) 0.001); | |
//how far should we advance through windowing function each sample | |
winInc = (double)WINDOW_LEN / winDurationSamps; | |
diff --git a/MyRtAudio.cpp b/MyRtAudio.cpp | |
index ef60ac2..1a3e525 100644 | |
--- a/MyRtAudio.cpp | |
+++ b/MyRtAudio.cpp | |
@@ -40,7 +40,7 @@ MyRtAudio::~MyRtAudio() | |
//cerr << "rtaudio cleanup reached " << endl; | |
} | |
-MyRtAudio::MyRtAudio(unsigned int numIns, unsigned int numOuts, unsigned int srate, unsigned int * bufferSize, RtAudioFormat format,bool showWarnings) | |
+MyRtAudio::MyRtAudio(unsigned int numIns, unsigned int numOuts, unsigned int * bufferSize, RtAudioFormat format,bool showWarnings) | |
{ | |
//configure RtAudio | |
//create pointer to RtAudio object | |
@@ -64,6 +64,8 @@ MyRtAudio::MyRtAudio(unsigned int numIns, unsigned int numOuts, unsigned int sra | |
cout << "No Audio Devices Found!" << endl; | |
exit( 1 ); | |
} | |
+ const RtAudio::DeviceInfo &info = audio->getDeviceInfo( audio->getDefaultOutputDevice() ); | |
+ | |
//allow RtAudio to print msgs to stderr | |
audio->showWarnings( showWarnings ); | |
@@ -72,7 +74,7 @@ MyRtAudio::MyRtAudio(unsigned int numIns, unsigned int numOuts, unsigned int sra | |
myBufferSize = bufferSize; | |
//set sample rate; | |
- mySRate = srate; | |
+ mySRate = info.preferredSampleRate; | |
//set format | |
myFormat = format; | |
@@ -86,6 +88,7 @@ void MyRtAudio::openStream( RtAudioCallback callback){ | |
//create stream options | |
RtAudio::StreamOptions options; | |
+ options.streamName = "Borderlands"; | |
RtAudio::StreamParameters iParams, oParams; | |
//i/o params | |
@@ -102,6 +105,12 @@ void MyRtAudio::openStream( RtAudioCallback callback){ | |
} | |
+//report the current sample rate | |
+unsigned int MyRtAudio::getSampleRate() | |
+{ | |
+ return mySRate; | |
+} | |
+ | |
//report the current buffer size | |
unsigned int MyRtAudio::getBufferSize(){ | |
diff --git a/MyRtAudio.h b/MyRtAudio.h | |
index 01756a7..4ea58fa 100644 | |
--- a/MyRtAudio.h | |
+++ b/MyRtAudio.h | |
@@ -48,12 +48,15 @@ public: | |
virtual ~MyRtAudio(); | |
//constructor - args = | |
- MyRtAudio(unsigned int numIns, unsigned int numOuts, unsigned int srate, unsigned int * bufferSize, RtAudioFormat format,bool showWarnings); | |
+ MyRtAudio(unsigned int numIns, unsigned int numOuts, unsigned int * bufferSize, RtAudioFormat format,bool showWarnings); | |
//set the audio callback and start the audio stream | |
void openStream( RtAudioCallback callback); | |
+ //report the current sample rate | |
+ unsigned int getSampleRate(); | |
+ | |
//report the current buffer size | |
unsigned int getBufferSize(); | |
diff --git a/theglobals.h b/theglobals.h | |
index e846b0e..1b8ebef 100644 | |
--- a/theglobals.h | |
+++ b/theglobals.h | |
@@ -40,8 +40,6 @@ | |
#define SAMPLE double | |
//create rtaudio format | |
#define MY_FORMAT RTAUDIO_FLOAT64 | |
-//set the sample rate | |
-#define MY_SRATE 44100 | |
//number of output channels | |
#define MY_CHANNELS 2 | |
@@ -81,4 +79,4 @@ static const double globalAtten = 0.5; | |
//}; | |
-#endif | |
\ No newline at end of file | |
+#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment