jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
5.1KB

  1. /** @file patest_sine.c
  2. @brief Play a sine wave for several seconds.
  3. @author Ross Bencina <rossb@audiomulch.com>
  4. @author Phil Burk <philburk@softsynth.com>
  5. */
  6. /*
  7. * $Id: patest_sine.c,v 1.1.2.1 2006/06/06 19:37:56 letz Exp $
  8. *
  9. * This program uses the PortAudio Portable Audio Library.
  10. * For more information see: http://www.portaudio.com/
  11. * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining
  14. * a copy of this software and associated documentation files
  15. * (the "Software"), to deal in the Software without restriction,
  16. * including without limitation the rights to use, copy, modify, merge,
  17. * publish, distribute, sublicense, and/or sell copies of the Software,
  18. * and to permit persons to whom the Software is furnished to do so,
  19. * subject to the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be
  22. * included in all copies or substantial portions of the Software.
  23. *
  24. * Any person wishing to distribute modifications to the Software is
  25. * requested to send the modifications to the original developer so that
  26. * they can be incorporated into the canonical version.
  27. *
  28. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  29. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  31. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  32. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  33. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  34. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  35. *
  36. */
  37. #include <stdio.h>
  38. #include <math.h>
  39. #include "portaudio.h"
  40. #define NUM_SECONDS (5)
  41. #define SAMPLE_RATE (44100)
  42. #define FRAMES_PER_BUFFER (64)
  43. #ifndef M_PI
  44. #define M_PI (3.14159265)
  45. #endif
  46. #define TABLE_SIZE (200)
  47. typedef struct
  48. {
  49. float sine[TABLE_SIZE];
  50. int left_phase;
  51. int right_phase;
  52. }
  53. paTestData;
  54. /* This routine will be called by the PortAudio engine when audio is needed.
  55. ** It may called at interrupt level on some machines so don't do anything
  56. ** that could mess up the system like calling malloc() or free().
  57. */
  58. static int patestCallback( const void *inputBuffer, void *outputBuffer,
  59. unsigned long framesPerBuffer,
  60. const PaStreamCallbackTimeInfo* timeInfo,
  61. PaStreamCallbackFlags statusFlags,
  62. void *userData )
  63. {
  64. paTestData *data = (paTestData*)userData;
  65. float *out = (float*)outputBuffer;
  66. unsigned long i;
  67. (void) timeInfo; /* Prevent unused variable warnings. */
  68. (void) statusFlags;
  69. (void) inputBuffer;
  70. for( i=0; i<framesPerBuffer; i++ )
  71. {
  72. *out++ = data->sine[data->left_phase]; /* left */
  73. *out++ = data->sine[data->right_phase]; /* right */
  74. data->left_phase += 1;
  75. if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
  76. data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
  77. if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
  78. }
  79. return paContinue;
  80. }
  81. /*******************************************************************/
  82. int main(void);
  83. int main(void)
  84. {
  85. PaStreamParameters outputParameters;
  86. PaStream *stream;
  87. PaError err;
  88. paTestData data;
  89. int i;
  90. printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
  91. /* initialise sinusoidal wavetable */
  92. for( i=0; i<TABLE_SIZE; i++ )
  93. {
  94. data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
  95. }
  96. data.left_phase = data.right_phase = 0;
  97. err = Pa_Initialize();
  98. if( err != paNoError ) goto error;
  99. outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
  100. outputParameters.channelCount = 2; /* stereo output */
  101. outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
  102. outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
  103. outputParameters.hostApiSpecificStreamInfo = NULL;
  104. err = Pa_OpenStream(
  105. &stream,
  106. NULL, /* no input */
  107. &outputParameters,
  108. SAMPLE_RATE,
  109. FRAMES_PER_BUFFER,
  110. paClipOff, /* we won't output out of range samples so don't bother clipping them */
  111. patestCallback,
  112. &data );
  113. if( err != paNoError ) goto error;
  114. err = Pa_StartStream( stream );
  115. if( err != paNoError ) goto error;
  116. printf("Play for %d seconds.\n", NUM_SECONDS );
  117. Pa_Sleep( NUM_SECONDS * 1000 );
  118. err = Pa_StopStream( stream );
  119. if( err != paNoError ) goto error;
  120. err = Pa_CloseStream( stream );
  121. if( err != paNoError ) goto error;
  122. Pa_Terminate();
  123. printf("Test finished.\n");
  124. return err;
  125. error:
  126. Pa_Terminate();
  127. fprintf( stderr, "An error occured while using the portaudio stream\n" );
  128. fprintf( stderr, "Error number: %d\n", err );
  129. fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
  130. return err;
  131. }