Advertisement
Kaelygon

ukaelAudio.c

Jul 8th, 2023
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.15 KB | None | 0 0
  1. //ukaelAudio program
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <portaudio.h>
  6.  
  7. #include "ukaelH/ukaelTypedefC.h"
  8. #include "ukaelH/ukaelWaveC.h"
  9. #include "ukaelH/ukaelToneC.h"
  10.  
  11. static const uint8_t PLAYAUDIO = 1;
  12.  
  13. int main( int argc, char *argv[] )  {
  14.     ENTROPY^=rand();
  15.  
  16.     PaError err;
  17.     PaStream* stream;
  18.     AudioData sample[MAX_CHANNELS];
  19.  
  20.     WaveArg wargs = {
  21.         .time = DEFAULT_AMPLITUDE,
  22.         .freq = (Frac){127,1},
  23.         .u8arg = {0,0,0,0},
  24.         .u16arg = {0,0,0,0}
  25.     };
  26.  
  27.     //allocate
  28.     sample[0].size = BUFFER_SIZE;
  29.     sampleAlloc(&sample[0]);
  30.  
  31.     // Generate the wave
  32.     wargs.u8arg[0]=6;
  33.     wargs.u8arg[1]=DEFAULT_AMPLITUDE;
  34.     generateTone(&sample[0], 255, "testing", &wargs );
  35.    
  36.  
  37.     AudioData PAudioData;
  38.     PAudioData.size = BUFFER_SIZE;
  39.     PAudioData.position = 0;
  40.     sampleAlloc(&PAudioData);
  41.     sampleCopy(&PAudioData,&sample[0]);
  42.  
  43.     audioDataToBin("audio.bin", &PAudioData);
  44.  
  45.     if(PLAYAUDIO && PAudioData.size){
  46.         // Initialize PortAudio
  47.         err = Pa_Initialize();
  48.         if (err != paNoError) {
  49.             printf("Failed to initialize PortAudio: %s\n", Pa_GetErrorText(err));
  50.             return 1;
  51.         }
  52.        
  53.         // Open the default output stream
  54.         err = Pa_OpenDefaultStream(&stream, 0, 1, paUInt8, SAMPLE_RATE,
  55.                                 FRAMES_PER_BUFFER, audioCallback, &PAudioData);
  56.  
  57.         if (err != paNoError) {
  58.             printf("Failed to open stream: %s\n", Pa_GetErrorText(err));
  59.             Pa_Terminate();
  60.             return 1;
  61.         }
  62.        
  63.         // Start the stream
  64.         err = Pa_StartStream(stream);
  65.         if (err != paNoError) {
  66.             printf("Failed to start stream: %s\n", Pa_GetErrorText(err));
  67.             Pa_CloseStream(stream);
  68.             Pa_Terminate();
  69.             return 1;
  70.         }
  71.        
  72.         // Wait for the stream to finish
  73.         printf("Playing audio...\n");
  74.  
  75.         Pa_Sleep((PAudioData.size)/SAMPLE_RATE*1000);
  76.        
  77.         // Stop and close the stream
  78.         err = Pa_StopStream(stream);
  79.         if (err != paNoError) {
  80.             printf("Failed to stop stream: %s\n", Pa_GetErrorText(err));
  81.         }
  82.         err = Pa_CloseStream(stream);
  83.         if (err != paNoError) {
  84.             printf("Failed to close stream:%s\n", Pa_GetErrorText(err));
  85.         }
  86.        
  87.         // Terminate PortAudio
  88.         Pa_Terminate();
  89.     }
  90.  
  91.     sampleFree(&sample[0]);
  92.     sampleFree(&PAudioData);
  93.    
  94.     return 0;
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement