Exemplo n.º 1
0
int main()
{
	//set the global sample rate
	Stk::setSampleRate(44100.0);
	Stk::showWarnings(true);
	
	int nFrames=100000;
	SineWave sine;
	RtWvOut *dac=0;
	
	try {
		// define and open the default realtime output device for one channel playback
		dac= new RtWvOut(1);
	}
	catch (StkError &) {
		exit(1);
	}
	
	sine.setFrequency(441.0);
	
	for (int i=0; i<nFrames; i++) {
		try {
			dac->tick(sine.tick());
		}
		catch (StkError &) {
			goto cleanup;
		}
	}
	
	cleanup:
	delete dac;
	
	return 0;
}
Exemplo n.º 2
0
Arquivo: main.cpp Projeto: OlafW/SOGM
int main(int argc, char *argv[]) {

  SineWave sine;
  float* tempSize;

  enum{ARG_NAME = 0, ARG_FREQ, ARG_AMP, ARG_DUR, NUM_ARGS};

  if (argc != NUM_ARGS || atoi(argv[ARG_FREQ]) < 1 ||
      atof(argv[ARG_AMP]) > 1 || atof(argv[ARG_DUR]) <= 0 ) {
    cout << "Give: \n"
          "- frequency(> 1) \n" <<
          "- amplitude(<= 1) \n" <<
          "- duration(> 0) \n" <<
          "  default sampleRate = 44100" << endl;
  }

  else {

  sine.setFrequency(atoi(argv[ARG_FREQ]));
  sine.setAmplitude(atof(argv[ARG_AMP]));
  sine.setSampleRate();
  sine.setDuration(atof(argv[ARG_DUR]));

  tempSize = sine.generate();
  for (int i = 0; i < sine.setSampleRate() *
    sine.setDuration(atof(argv[ARG_DUR])); i++) {
    cout << tempSize[i] << endl;
    }

  delete[] tempSize;
  }
  return 0;
}
Exemplo n.º 3
0
int main()
{
  // Set the global sample rate before creating class instances.
  Stk::setSampleRate( 44100.0 );

  SineWave sine;
  RtAudio dac;

  // Figure out how many bytes in an StkFloat and setup the RtAudio stream.
  RtAudio::StreamParameters parameters;
  parameters.deviceId = dac.getDefaultOutputDevice();
  parameters.deviceId = 3;
  parameters.nChannels = 1;
  
  RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32;
  unsigned int bufferFrames = RT_BUFFER_SIZE;

  try {
    dac.openStream( &parameters, NULL, format, (unsigned int)Stk::sampleRate(),
		    &bufferFrames, &tick, (void *)&sine );
  }
  catch ( RtAudioError &error ) {
    error.printMessage();
    goto cleanup;
  }

  // configuration of oscilator
  sine.setFrequency(440.0);

  

  // start the main real time loop
  try {
    dac.startStream();
  }
  catch ( RtAudioError &error ) {
    error.printMessage();
    goto cleanup;
  }


  // USER interface

  // Block waiting here.
  char keyhit;
  std::cout << "\nPlaying ... press <enter> to quit.\n";
  std::cin.get( keyhit );

  // SYSTEM shutdown
  // Shut down the output stream.
  try {
    dac.closeStream();
  }
  catch ( RtAudioError &error ) {
    error.printMessage();
  }

 cleanup:
  return 0;
}
Exemplo n.º 4
0
int main( int argc, char **argv ) {

    StkFloat lengthseconds = 2;
    StkFloat lofreq = 10;
    StkFloat hifreq = 22000;
    StkFloat partialsperoctave = 1500.0;
    unsigned long numpartials = 0;
    unsigned int noisetype = WHITENOISE;
    std::string outfilename( "outfile.wav" );

    // -(option parsing)--------------------------------------------------------

    int optionfound = -1;
    unsigned char ppospecified = 1;
    while( 1 ) {

        int option_index = 0; // getopt_long stores the option index here.

        optionfound = getopt_long (argc, argv, "l:s:e:p:n:wko:", long_options, &option_index);

        // end of options
        if (optionfound == -1)
            break;

        switch (optionfound) {

            case 'l':
                lengthseconds = atof( optarg );
                break;

            case 's':
                lofreq = atof( optarg );
                break;

            case 'e':
                hifreq = atof( optarg );
                break;

            case 'p':
                partialsperoctave = atof(optarg);
                ppospecified = 1;
                break;

            case 'n':
                numpartials = atol(optarg);
                ppospecified = 0;
                break;

            case 'w':
                noisetype = WHITENOISE;
                break;

            case 'k':
                noisetype = PINKNOISE;
                break;

            case 'o':
                outfilename = optarg;
                break;

            default:
                abort();

        }

    };

    unsigned long numsamples = static_cast<unsigned long>(lengthseconds * Stk::sampleRate());

    // need to do this at the end since it depends on lofreq/hifreq and can be
    // overridden by --numpartials
    if (ppospecified)
        numpartials = static_cast<unsigned long>(partialsperoctave * numoctaves( lofreq, hifreq ));

    if ( lofreq >= hifreq ) {
        std::cout << "ERROR: low freq (" << lofreq
            << ") is not lower than high freq (" << hifreq
            << ")" << std::endl;
        exit(0);
    };

    // -(main routine)----------------------------------------------------------

    // final output
    StkFrames output(0.0, numsamples, 1);

    // sine ugen
    SineWave thissine;

    std::cout << "computing " << numpartials << " partials" << std::endl;

    for ( unsigned long i = 0; i < numpartials; i++ ) {

        // progress indicator
        std::cout << "." << std::flush;

        if ( i % 20 == 0 )
            std::cout << " " << i << " " << std::flush;

        if ( noisetype == WHITENOISE ) {
            thissine.setFrequency( getlinrandfreq( lofreq, hifreq ) );
        } else { // PINKNOISE
            thissine.setFrequency( getlograndfreq( lofreq, hifreq ) );
        }

        // NB just changing the frequency in this way but using the same
        // SineWave ugen effectively randomises the starting phase.  we don't
        // want all of the partials phase aligned at the start since that leads
        // to a big amplitude spike (throwing off the normalisation step).

        for ( unsigned long j = 0; j < numsamples; j++ ) {
            // NB we're assuming here that we're not going to overflow the
            // doubles.  max I've seen is ~200.0, so we should be safe.
            output[j] += thissine.tick();
        }

    }

    std::cout << "done computing" << std::endl;

    normalize( output );

    std::cout << "done normalising" << std::endl;

    // write to file
    FileWrite outfile( outfilename, 1, FileWrite::FILE_WAV, Stk::STK_SINT16 );
    outfile.write( output );
    outfile.close();

    std::cout << "wrote to " << outfilename << std::endl;

    std::cout << "all done" << std::endl;

}
Exemplo n.º 5
0
int main()
{
  // Set the global sample rate before creating class instances.
  Stk::setSampleRate( 44100.0 );

  SineWave sine;
  RtAudio dac;

  // Figure out how many bytes in an StkFloat and setup the RtAudio stream.
  RtAudio::StreamParameters parameters;
  parameters.deviceId = dac.getDefaultOutputDevice();
  parameters.nChannels = 1;
  RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32;
  unsigned int bufferFrames = RT_BUFFER_SIZE;
  try {
    dac.openStream( &parameters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&sine );
  }
  catch ( RtError &error ) {
    error.printMessage();
    goto cleanup;
  }
  double f = 440.0;
  double twelveRoot2 = 1.0594630943592952645618252949463;

  sine.setFrequency(f);
  try {
    dac.startStream();
  }
  catch ( RtError &error ) {
    error.printMessage();
    goto cleanup;
  }

  // Block waiting here.
  int keyhit = 0;
  std::cout << "\nPlaying ... press <esc> to quit.\n";
  while (keyhit != 32 && keyhit != 27)
  {
	  keyhit = _getch();
	  if (tolower(keyhit) == 'a')
	  {
		  f = 220.0;
		  sine.setFrequency(f);
	  }
	  else if (tolower(keyhit) == 'g')
	  {
		  f /= twelveRoot2;
		  sine.setFrequency(f);
	  }
	  else if (tolower(keyhit) == 'h')
	  {
		  f *= twelveRoot2;
		  sine.setFrequency(f);
	  }
	  else if (tolower(keyhit) == 'f')
	  {
		  for (int i = 0; i < 2; ++i)
			f /= twelveRoot2;
		  sine.setFrequency(f);
	  }
	  else if (tolower(keyhit) == 'j')
	  {
		  for (int i = 0; i < 2; ++i)
			  f *= twelveRoot2;
		  sine.setFrequency(f);
	  }
	  else if (tolower(keyhit) == 'd')
	  {
		  for (int i = 0; i < 3; ++i)
			  f /= twelveRoot2;
		  sine.setFrequency(f);
	  }
	  else if (tolower(keyhit) == 'k')
	  {
		  for (int i = 0; i < 3; ++i)
			  f *= twelveRoot2;
		  sine.setFrequency(f);
	  }
	  else if (tolower(keyhit) == 's')
	  {
		  for (int i = 0; i < 4; ++i)
			  f /= twelveRoot2;
		  sine.setFrequency(f);
	  }
	  else if (tolower(keyhit) == 'l')
	  {
		  for (int i = 0; i < 4; ++i)
			  f *= twelveRoot2;
		  sine.setFrequency(f);
	  }
	  else
	  {
		  std::cout << "Freq: " << f << std::endl;
	  }
  }

  // Shut down the output stream.
  try {
    dac.closeStream();
  }
  catch ( RtError &error ) {
    error.printMessage();
  }

 cleanup:

  return 0;
}