[Stk] Trouble with samples

Jeff Farr Jeff Farr <moogah@gmail.com>
Tue, 29 Mar 2005 19:10:07 -0500


------=_Part_2139_23701617.1112141407731
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

I'm having trouble wrapping all thats needed to play a sample into a
class.  In particular I'm having trouble moving some of the steps into
seperate functions.  In the attached file if the function loadFile()
is used the .exe crashes (windows console) but if the code in the main
function is used it works fine.  The same is true of the initization
of the RtAudio object.  In a function it causes a crash (windows
reports that the .exe has encountered a problem and must shut down.)
while in the main() it works fine.  Perhaps I'm not handling my
pointers correctly?  I'm very sure of my project settings and included
files, but I won't rule that out completely.  Any  help is
appreciated.

------=_Part_2139_23701617.1112141407731
Content-Type: text/x-cpp; name="Main.cpp"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment; filename="Main.cpp"



#include <iostream>
#include "WvIn.h"
#include "RtAudio.h"
#include <signal.h>
using namespace std;

     =20
void initSystem(RtAudio *audio);
void loadFile(WvIn *waveIn);
void probeDevices(RtAudio *audio);
void initStream(RtAudio *audio, WvIn *waveIn);
void startStream(RtAudio *audio, WvIn *waveIn);
void createOutput(int numPoints, int startPoint, RtAudio *audio, WvIn *wave=
In);
void closeStream(RtAudio *audio);
void setCallback(RtAudio *audio, WvIn *waveIn);
int sawtooth(char *buffer, int bufferSize, void *data);
int tick(char *buffer, int bufferSize, void *dataPointer);

bool done;
static void finish(int ignore){ done =3D true; }


StkFrames sampleData;
float *streamBuffer;
double data[2];
char input;
int channels;
int sampleRate;
int bufferSize; =20
int nBuffers;   =20
int device;
double rate; =20
RtAudioFormat format;



int main(){// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ main()
  RtAudio *audio =3D 0;
  WvIn    *waveIn =3D 0;
 =20
  initSystem(audio);
 =20
  try{
=09  waveIn =3D new WvIn("r909cr.wav");
  }catch(RtError &error){
      cout << "File Error " << endl;
=09  error.printMessage();
=09exit(0);
  }
 =20
  double rate =3D 1.0;
  rate =3D waveIn->getFileRate() / Stk::sampleRate();
 =20
  waveIn->setRate( rate );
 =20
  int channels =3D waveIn->getChannels();
   =20
  //loadFile(waveIn);
  initStream(audio, waveIn);
 =20
  sampleData.resize( bufferSize, channels );
     =20
  try {
    audio =3D new RtAudio(device, channels, 0, 0, format, sampleRate, &buff=
erSize, nBuffers);
  }
  catch (RtError &error) {
    error.printMessage();
    goto cleanup;
  }

  probeDevices(audio);
  cout << " 1 ";
 =20
  startStream(audio, waveIn);
  cout << " 2 ";
   =20
  createOutput(100000, 0, audio, waveIn);
  cout << " 3 ";

  //setCallback(audio, waveIn);

  //while ( !done )
    //Stk::sleep( 100 );

  cleanup:
  cout << " cleanup " << endl;
  //closeStream(audio);
  delete audio;

  return 0;
}// ____________________________________________________________ >>>> end m=
ain()






void initSystem(RtAudio *audio){
=09Stk::setSampleRate( (StkFloat) 44100 );
}



void loadFile(WvIn *waveIn){
=09try{
=09  waveIn =3D new WvIn("r909cr.wav");
=09}catch(RtError &error){
      cout << "File Error " << endl;
=09  error.printMessage();
=09  exit(0);
=09}
 =20
=09rate =3D 1.0;
=09rate =3D waveIn->getFileRate() / Stk::sampleRate();
 =20
=09waveIn->setRate( rate );

=09channels =3D waveIn->getChannels();

=09//sampleData.resize( bufferSize, channels );
}



void probeDevices(RtAudio *audio){
 =20
  int devices =3D audio->getDeviceCount();
=20
  RtAudioDeviceInfo info;
  for (int i=3D1; i<=3Ddevices; i++) {

    try {
      info =3D audio->getDeviceInfo(i);
    }
    catch (RtError &error) {
=09=09cout << "probeDevices";
      error.printMessage();
      break;
    }
  =20
    std::cout << "device =3D " << i << " " << info.name;
    std::cout << ": maximum output channels =3D " << info.outputChannels <<=
 " Default? " << info.isDefault << "\n";
  }

}



void initStream(RtAudio *audio, WvIn *waveIn){
=09channels =3D waveIn->getChannels();
=09sampleRate =3D (int) Stk::sampleRate();
=09device =3D 0;
=09bufferSize =3D RT_BUFFER_SIZE;
=09nBuffers =3D 8;

=09format =3D ( sizeof(StkFloat) =3D=3D 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLO=
AT32;
    /*
=09try {
=09=09audio =3D new RtAudio(device, channels, 0, 0, format, sampleRate, &bu=
fferSize, nBuffers);
=09}
=09catch (RtError &error) {
=09=09error.printMessage();
=09=09exit(0);
=09}
=09*/
=09cout << " Stream Info: " << "Channels: " << channels << " SampleRate: " =
<< sampleRate << " Buffer Size: " << bufferSize << endl;
}



void startStream(RtAudio *audio, WvIn *waveIn){
=09try{
=09=09audio->startStream();
=09=09streamBuffer =3D  (float *) audio->getStreamBuffer();
=09}catch(RtError &error){
=09=09error.printMessage();
=09}
}





void createOutput(int numPoints, int startPoint, RtAudio *audio, WvIn *wave=
In){
=09int count =3D 0;
=09
=09while (count < numPoints){
=09=09
=09=09int i;
=09=09register StkFloat *samples =3D (StkFloat *) streamBuffer;
=09=09waveIn->tickFrame(sampleData);
=09=09
=09=09
=09=09
=09=09for (i=3D0; i<bufferSize; i++) {
=09=09=09
=09=09=09*samples++ =3D sampleData[i + startPoint];
=09=09
=09=09}
=09=09
=09=09try{
=09=09=09audio->tickStream();
=09=09}catch(RtError &error){
=09=09=09error.printMessage();
=09=09=09cout << "createOutput()";
=09=09}

=09=09count +=3D bufferSize;
=09}
=09
=09closeStream(audio);
}



void closeStream(RtAudio *audio){
  try {
    audio->stopStream();
    audio->closeStream();
  }
  catch (RtError &error) {
    error.printMessage();
  }
}

















int tick(char *buffer, int bufferSize, void *dataPointer)
{
  WvIn *input =3D (WvIn *) dataPointer;
  register StkFloat *samples =3D (StkFloat *) buffer;

  input->tickFrame( sampleData );
  for ( unsigned int i=3D0; i<sampleData.size(); i++ )
    *samples++ =3D sampleData[i];

  if ( input->isFinished() ) {
    done =3D true;
    return 1;
  }
  else
    return 0;
}




void setCallback(RtAudio *audio, WvIn *waveIn){
=09/*
=09try{
=09=09audio->startStream(device, channels, 0, 0, RTAUDIO_FLOAT64, sampleRat=
e,=20
=09=09=09=09=09=09=09=09=09=09=09=09&bufferSize, nBuffers);
=09}catch(RtError &error){
=09=09cout << "setCallback()";
=09=09error.printMessage();
=09}*/

=09(void) signal(SIGINT, finish);

=09try{
=09=09audio->setStreamCallback(&tick, (void*)waveIn);
=09=09audio->startStream();
=09}catch(RtError &error){
=09=09cout << "setCallback() 2";
=09=09error.printMessage();
=09}=09
}



int sawtooth(char *buffer, int bufferSize, void *data){
  int i, j;
  double *my_buffer =3D (double *) buffer;
  double *my_data =3D (double *) data;

  // Write interleaved audio data.
  for (i=3D0; i<bufferSize; i++) {
    for (j=3D0; j<2; j++) {
      *my_buffer++ =3D my_data[j];

      my_data[j] +=3D 0.005 * (j+1+(j*0.1));
      if (my_data[j] >=3D 1.0) my_data[j] -=3D 2.0;
    }
  }

  return 0;
}
------=_Part_2139_23701617.1112141407731--