Advertisement
Kitomas

using kit_w32 audio callbacks

Mar 8th, 2024
573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <kit/audio.hpp>
  2.  
  3. using namespace kit;
  4.  
  5.  
  6. #define FORMULA t*(42&t>>10) //the elusive 42 melody~
  7. #define TARGET_SAMPLE_RATE 8000
  8. u64 _t = KIT_U64_MAX;
  9.  
  10. s32 audio_callback(const void* _input, void* _output,
  11.                    const AudioStreamInfo* info, void* userdata)
  12. {
  13.   (void)_input;
  14.   u8* output = (u8*)_output;
  15.  
  16.   u32 numSamples = info->sampleFrames;
  17.   f64 sampleRate = info->sampleRate;
  18.   f64 rateRatio  = (f64)TARGET_SAMPLE_RATE/sampleRate;
  19.  
  20.  
  21.   for(u32 i=0; i<numSamples; ++i){
  22.     u64 t = (u64)((++_t)*rateRatio);
  23.     output[i] = (u8)( FORMULA );
  24.   }
  25.  
  26.   return ASTREAM_RTN_CONTINUE;
  27. }
  28.  
  29.  
  30.  
  31.  
  32. int main(int argc, char** argv){
  33.  
  34.   AudioStreamParams params;
  35.   params.callback       = audio_callback;
  36.   params.outputDeviceID = audio::getDefOutputDevice();
  37.   params.outputFormat   = ASTREAM_FMT_U8;
  38.   params.outputChannels = 1;
  39.  
  40.   AudioStream stream(&params);
  41.  
  42.   stream.start();
  43.   time::sleep(8300); //plays 1 loop of 42 melody bytebeat
  44.   stream.stop();
  45.  
  46.   return 0;
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement