Exemple #1
0
int main()
{
  // Set the global sample rate before creating class instances.
  Stk::setSampleRate( 44100.0 );

  int i;
  FileWvOut output;
  SineWave inputs[4];

  // Set the sine wave frequencies.
  for ( i=0; i<4; i++ )
    inputs[i].setFrequency( 220.0 * (i+1) );

  // Define and open a 16-bit, four-channel AIFF formatted output file
  try {
    output.openFile( "foursine.aif", 4, FileWrite::FILE_AIF, Stk::STK_SINT16 );
  }
  catch (StkError &) {
    exit( 1 );
  }

  // Write two seconds of four sines to the output file
  StkFrames frames( 88200, 4 );
  for ( i=0; i<4; i++ )
    inputs[i].tick( frames, i );

  output.tick( frames );

  // Now write the first sine to all four channels for two seconds
  for ( i=0; i<88200; i++ ) {
    output.tick( inputs[0].tick() );
  }

  return 0;
}
Exemple #2
0
int main( int argc, char* argv[] )
{
	cout << "*** Arguments ***" << endl;
	for ( int a = 0; a < argc; a++ )
		cout <<  a << " : " << argv[a] << endl;
	if ( argc == 1 ) return 1;
	string ifname = string( argv[1] );
	FileWvIn filewvin = FileWvIn( ifname, false, true );
	//size_t pos = ifname.rfind("/");
	//string dirname = ifname.substr(0, pos);
	size_t extpos = ifname.rfind(".wav");
	string basename = ifname.substr(0, extpos);
	for ( unsigned int i=0; i<filewvin.channelsOut(); i++ )
	{
		string ofname = basename +  "-" + to_string(i) + ".wav";
		FileWvOut filewvout = FileWvOut( ofname );
		cout << "channel " << i << endl;
		vector<StkFloat> data;
		unsigned int sn = 0;
		int mod = 0;
		float delta = 0.0f;
		float max = 0.0f;
		float corrected;
		filewvin.reset();
		StkFloat previous;
		StkFloat value = filewvin.tick(i);
		//~ filewvout.tick( previous );
		while ( not( filewvin.isFinished() ) )
		{
			//~ cout << "value : " << value << endl;
			corrected = value + mod;
			data.push_back( corrected );
			if ( abs( corrected ) > max ) max = abs( corrected );
			previous = value;
			sn++;
			value = filewvin.tick(i);
			delta = value - previous;
			if ( delta > 1.7 )
			{
				if ( mod == 0 ) mod = -2;
				else mod = 0;
				cout << sn <<  " | value : " << value << " | delta : " << delta << endl;
			}
			else if ( delta < -1.7 )
			{
				if ( mod == 0 ) mod = 2;
				else mod = 0;
				cout << sn <<  " | value : " << value << " | delta : " << delta << endl;
			}
			
		}
		cout << "maximum amp : " << max << endl;
		for ( vector<StkFloat>::iterator f=data.begin(); f != data.end(); f++ )
			filewvout.tick( *f / max );
		filewvout.closeFile();
	}
	filewvin.closeFile();
	return 0;
}
Exemple #3
0
int main( int argc, char *argv[] )
{
  float base_freq = 220.0;
  int i;

  // Minimal command-line checking.
  if ( argc != 5 ) usage();

  int channels = (int) atoi( argv[1] );
  double time = atof( argv[3] );
  double srate = atof( argv[4] );

  // Create our object instances.
  FileWvOut output;
  SineWave **oscs = (SineWave **) malloc( channels * sizeof(SineWave *) );
  for ( i=0; i<channels; i++ ) oscs[i] = 0;

  // If you want to change the default sample rate (set in Stk.h), do
  // it before instantiating any objects!!
  Stk::setSampleRate( srate );

  // Define the sinewaves.
  for ( i=0; i<channels; i++ )
    oscs[i] = new SineWave;

  // Set oscillator frequency(ies) here ... somewhat random.
  for ( i=0; i<channels; i++ )
    oscs[i]->setFrequency( base_freq + i*(45.0) );

  long nFrames = (long) ( time * Stk::sampleRate() );
  StkFrames frames( nFrames, channels );

  // Open the soundfile for output.  Other file format options
  // include: FILE_SND, FILE_AIF, FILE_MAT, and FILE_RAW.  Other data
  // type options include: STK_SINT8, STK_SINT32, StkFloat32, and
  // StkFloat64.
  try {
    output.openFile( argv[2], channels, FileWrite::FILE_WAV, Stk::STK_SINT16 );
  }
  catch ( StkError & ) {
    goto cleanup;
  }

  // Here's the runtime code ... no loop
  for ( i=0; i<channels; i++ )
    oscs[i]->tick( frames, i );

  output.tick( frames );

 cleanup:
  for ( i=0; i<channels; i++ )
    delete oscs[i];
  free( oscs );

  return 0;
}
Exemple #4
0
int main()
{
  // Set the global sample rate before creating class instances.
  Stk::setSampleRate( 44100.0 );

  int nFrames = 100000;
  FileLoop input;
  FileWvOut output;

  try {
    // Load the sine wave file.
    input.openFile( "rawwaves/sinewave.raw", true );

    // Open a 16-bit, one-channel WAV formatted output file
    output.openFile( "hellosine.wav", 1, FileWrite::FILE_WAV, Stk::STK_SINT16 );
  }
  catch ( StkError & ) {
    exit( 1 );
  }

  input.setFrequency( 440.0 );

  // Option 1: Use StkFrames
  /*
  StkFrames frames( nFrames, 1 );
  try {
    output.tick( input.tick( frames ) );
  }
  catch ( StkError & ) {
    exit( 1 );
  }
  */

  // Option 2: Single-sample computations
  for ( int i=0; i<nFrames; i++ ) {
    try {
      output.tick( input.tick() );
    }
    catch ( StkError & ) {
      exit( 1 );
    }
  }

  return 0;
}
Exemple #5
0
int main(int argc, char *argv[])
{
    double velSource;
    double velReciver;
    double distX;
    double distY;
    double velRelativeX;
    double totalTime;
    unsigned long numSamples;

    if ( argc < 7 || argc > 8 ) {
        std::cout<<"\n\n\n****Doppler shift example*****\n\n";
        std::cout<<"Will produce output assuming source is travelling towards\n";
        std::cout<<"and then past receiver in straight line\n\n";
        std::cout<<"Will start simulation at d0=-D and end at d1=+D\n";
        std::cout<<"Simulation time = 2D/(relative velocity)\n";
        std::cout<<"input will be looped for duration of simulation\n\n";
        std::cout<< "usage: doppler [spd src (m/s)] [spd recv (m/s)] [distX (m)] [distY (m)] [input file (wav)] [output file (wav)] [0/1: no mix/mix]\n";
        std::cout<< "NOTE: if source and receiver are diverging, fix simulation duration to 5s\n";
        return 0;
    }
    else {
        std::cout<<"INPUT OK...\n parsed values:";
        velSource = atof(argv[1]);
        velReciver = atof(argv[2]);
        distX = atof(argv[3]);
        distY = atof(argv[4]);
        //visual check to make sure input was parsed properly
        std::cout<<"velSource=" << velSource << " velReciver=" << velReciver << " distX=" << distX << " distY=" <<distY << " if=" << argv[5] << "\n\n";

        velRelativeX = velSource-velReciver;

        double distAbs = sqrt(distX*distX + distY*distY);

        totalTime = 2*distX/velRelativeX; //total simulation time, if not diverging/constant

        //do some basic input checking:
        if ((velSource - velReciver) <= 0) {
            //this is for the "not so interesting" case where
            //the source and receiver will never "cross"
            // will just have a boring, constant frequency...
            //so we force a fixed simulation time here.
            std::cout<<"DIVERGING! clamping time to 5 seconds\n";
            totalTime = 5.0;
        }
        numSamples = totalTime * SAMPLE_RATE;
        std::cout<<"relative speedX = " << velRelativeX<<"; simulation time = " << totalTime<< "s; numSamples = "<< numSamples<<"\n";
    }

    Stk::setSampleRate( SAMPLE_RATE );

    //file i/o
    FileWvOut outputFile;
    FileLoop inputLoop;

    try {
        outputFile.openFile( argv[6], 1, FileWrite::FILE_WAV, Stk::STK_SINT16 );
        inputLoop.openFile(argv[5]);
    }
    catch (StkError &) {
        exit( 1 );
    }

    //the doppler effect's frequency shift is implemented using a delay line whose write
    // pointer is incremented sample by sample and read using a variable delay.
    DelayL delay(1.0, DELAY_SIZE);

    //initialize: since read pointer can increment faster than write pointer, we should
    // fill up the delay line with initial values for continuity's sake...
    // since our input is a loop, it doesn't matter too much where the "starting" point
    // in the audio sample is...
    for (int i=0; i<DELAY_SIZE; i++) {
        delay.tick(inputLoop.tick());
    }

    stk::StkFrames frames( numSamples, 1);
    double readPtr = 0;
    unsigned int writePtr = 0;
    double g=0;

    // set initial locations
    double xSrcCurr = 0, xSrcPrev = 0;
    double xRcvCurr = distX, xRcvPrev = distX;
    double dT = totalTime/numSamples;

    for (int i=0; i<numSamples; i++) {

        //basic kinematic model for estimating current relative speed:
        double timeElapsed = (double)totalTime*i/numSamples;
        xSrcCurr+=dT*velSource;
        xRcvCurr+=dT*velReciver;

        //distY is constant!

        //velRelative = ( sqrt((xSrcCurr-xSrcPrev)*(xSrcCurr-xSrcPrev)+distY*distY) +
        //               sqrt((xRcvPrev-xRcvCurr)*(-xRcvCurr)+distY*distY) ) / dT;

        //double velRelative = ( (xRcvCurr-xSrcCurr) - (xRcvPrev-xSrcPrev) ) / dT;


        //bit of a hack to get the right behaviour, as my quickly cobbled together model
        // was bit sloppy when it came to managing the sign
        double velRelative = -fabs( sqrt((xRcvCurr-xSrcCurr)*(xRcvCurr-xSrcCurr)+distY*distY)
                                    - sqrt((xRcvPrev-xSrcPrev)*(xRcvPrev-xSrcPrev)+distY*distY) ) / dT;

        if (xRcvCurr < xSrcCurr)
            velRelative = -velRelative;

        //std::cout <<xSrcCurr<<"  "<<xRcvCurr<< " "<< velRelative <<"\n";

        xSrcPrev = xSrcCurr;
        xRcvPrev = xRcvCurr;

        //if (i < numSamples/2)
        //  g = - velRelative/SPEED_OF_SOUND; // "growth parameter"
        //else
        g = + velRelative/SPEED_OF_SOUND;
        readPtr+=g; //update read pointer with variable delay

        //wraparound checks:
        if (readPtr < 0)
            readPtr+=DELAY_SIZE;
        if (readPtr>DELAY_SIZE-1)
            readPtr-=DELAY_SIZE;

        //write pointer simply increments...
        writePtr++;
        if (writePtr>DELAY_SIZE)
            writePtr-=DELAY_SIZE;

        //where the magic happens: the variable delay creates the frequency shift
        delay.setDelay(1+readPtr);

        //User Selectable switching of the "Mixing": gets rid of discontinuities
        // when the read pointer crosses the write location by
        // linearly mixing between half a delay line away.
        // maximum amplitude is respected, but at the cost
        // of additional artifacts. better mixing techniques could reduce these...
        double mix = ( (int)(readPtr-writePtr) % (DELAY_SIZE) ) / (double) DELAY_SIZE ;
        if (atoi(argv[7]) == 0)
            mix = 1; // overrides the mixing calculation. i.e. no mixing.

        //update output sample
        float outsample = mix*delay.tick(inputLoop.tick());

        //apply mixing (if any)
        outsample += (1-mix)*delay.tapOut(DELAY_SIZE/2);

        //put into frames
        frames[i] = outsample;
        //std::cout<<"tE = " << timeElapsed<<"\n";
    }


    //dump frames into outputFile file. (could have incrementally done it directly above...)
    outputFile.tick(frames);

    return 0;
}
Exemple #6
0
int main()
{
	StkFloat d;						// sample data
	unsigned long inputSize;		// size of the input file in sample frames
	double inputDuration;			// duration of the input file loop
	unsigned int channels = 1;
	//set global sample rate
	Stk::setSampleRate(44100.0);
	
	int nFrames=100000;
	FileLoop input;
	FileWvOut output;
	
	// open the input and output files, with error checking
	try {
		// load the input file
		input.openFile("rawwaves/Noisy_Miner_chirp_stereo.wav", false);
	
		// open a 16 bit, one channel WAV formatted output
		output.openFile("many_chirps.wav", 1, FileWrite::FILE_WAV, Stk::STK_SINT16);
	}
	catch (StkError &) {
			exit(1);
	}
	
	  // Set input read rate based on the default STK sample rate.
	double rate;
	double inputRate;
	inputRate =  input.getFileRate();
	rate = inputRate / Stk::sampleRate();
	input.setRate( rate );  
  
	// Find out the length of the input file
	inputSize=input.getSize();
	inputDuration = inputSize/inputRate;
	std::cout << "input file is " << inputSize << " samples long with a duration of "  << inputDuration << " sec." << std::endl;
  
  
	// Find out how many channels we have.
	channels = input.channelsOut();
	std::cout << "input file has " << channels << " channels at "<< inputRate <<  "Hz  sample rate " << std::endl;

	std::cout << " we want to run a rate of " << Stk::sampleRate() << " and will sample the file by a factor of "  << rate << std::endl;
	
	
	// we want the chirps to be at the same frequency as they were recorded
	// so we adjust the looping rate of recorded loop
	// if you make the frequency negative, it reverses the loop!
	input.setFrequency(1.0/inputDuration);
	
	// run the oscillator for 40000 samples, writing to the output file
	for (int i=0; i<nFrames; i++){
		try {
			d=input.tick();		// read 1 sample from the input file
			output.tick(d);		// write 1 sample to the output file
		}
		catch (StkError &) {
			exit(1);
		}
		
		// print the first 100 samples to the std output for debugging
		if (i<10){
			std::cout << d << std::endl;
		}	
	}	
		
	return 0;
	
}