Advertisement
Guest User

Untitled

a guest
Aug 29th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.70 KB | None | 0 0
  1. #define MA_NO_DECODING
  2. #define MA_NO_ENCODING
  3. #define MINIAUDIO_IMPLEMENTATION
  4.      #include "miniaudio.h"
  5. #define DEVICE_FORMAT       ma_format_f32
  6. #define DEVICE_CHANNELS     1
  7. #define DEVICE_SAMPLE_RATE  48000
  8. #include "miniaudio.h"
  9.  
  10.  
  11. void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount){
  12. /*In playback mode copy data to pOutput. In capture mode read data from pInput. In full-duplex mode, both
  13. pOutput and pInput will be valid and you can move data from pInput into pOutput. Never process more than
  14. frameCount frames.*/
  15.  
  16.      static float saw = 0;
  17.      float* Samples = pOutput; /* "Samples" is just a (castless in this case) cast of the "pOutput" float pointer */
  18.      ma_uint32 SampleIndex;
  19.      float freq = 50;
  20.      float amp = 0.2;
  21.  
  22.      MA_ASSERT(pDevice->playback.channels == DEVICE_CHANNELS);
  23.  
  24.      for(SampleIndex = 0; SampleIndex < frameCount; SampleIndex++){
  25.           saw=saw*(saw<(DEVICE_SAMPLE_RATE/freq)); /* zeroes out sq every (1/freq) */
  26.           *Samples = saw/(DEVICE_SAMPLE_RATE/freq)*amp;
  27.           saw++; /* increases sq >> so I have a rising sawthoot */
  28.           Samples++;}
  29.  
  30.      (void)pInput;}
  31.  
  32. int main(){
  33.  
  34.      ma_device_config deviceConfig;
  35.      ma_device device;
  36.  
  37.  
  38.      /* audio output device configuration */
  39.      deviceConfig = ma_device_config_init(ma_device_type_playback); /* initialize for playback (not capture) */
  40.      deviceConfig.playback.format   = DEVICE_FORMAT;
  41.      deviceConfig.playback.channels = DEVICE_CHANNELS;
  42.      deviceConfig.sampleRate        = DEVICE_SAMPLE_RATE;
  43.      deviceConfig.dataCallback      = data_callback; /* name of the callback function */
  44.      /*deviceConfig.pUserData         = &sineWave;*/ /* this data is fed to data_callback (Can be accessed from the device object (device.pUserData) inside the callback) */
  45.      /*************************************/
  46.  
  47.      /* audio output device initialization */
  48.      if(ma_device_init(NULL, &deviceConfig, &device) != MA_SUCCESS){
  49.           printf("Failed to open playback device.\n");
  50.           return -4;}
  51.      /**************************************/
  52.  
  53.      printf("== Device Name: %s\n", device.playback.name);
  54.      printf("== Press Enter to start...");
  55.      getchar();
  56.  
  57.      /* this is the actual sound start */
  58.      if (ma_device_start(&device) != MA_SUCCESS) {
  59.           printf("== Failed to start playback device.\n");
  60.           ma_device_uninit(&device);
  61.           return -5;}
  62.  
  63.      /* Do something here. Probably your program's main loop. */
  64.  
  65.      printf("~~~ You should hear sound now ~~~\n");
  66.      printf("== Press Enter to quit...");
  67.      getchar();
  68.  
  69.      ma_device_uninit(&device); /* turn off sound */
  70.  
  71.     return 0;}
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement