Guest User

Untitled

a guest
Jan 19th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. #include <SDL2/SDL.h>
  2.  
  3. #include <cmath>
  4.  
  5. // An audio driver. This audio driver will define a callback that will
  6. // generate a waveform which is passed to SDL. SDL passes this waveform to
  7. // the OS, where it is output to a physical device.
  8.  
  9. struct audio_driver
  10. {
  11. SDL_AudioSpec audio_spec;
  12.  
  13. // Default constructor.
  14.  
  15. audio_driver()
  16. {
  17. // The 'freq' parameter specifies amount of samples to take per
  18. // second. A higher frequency corresponds to a higher quality of
  19. // sound.
  20.  
  21. audio_spec.freq = 48000;
  22.  
  23. // The 'format' parameter specifies the precision of each sample. A
  24. // higher format size corresponds to a broader range of possible
  25. // sounds.
  26.  
  27. audio_spec.format = AUDIO_S16SYS;
  28.  
  29. // The 'channels' parameter specifies the amount of output channels. 2
  30. // is stereo, and 1 is mono.
  31.  
  32. audio_spec.channels = 1;
  33.  
  34. // The 'samples' parameter specifies the size of the stream buffer.
  35. // The value of 4096 is suggested by SDL.
  36.  
  37. audio_spec.samples = 4096;
  38.  
  39. // The 'userdata' parameter specifies the arbitrary data to pass to
  40. // the audio callback function. The pointer to this object is used to
  41. // allow access to the contents of this object.
  42.  
  43. audio_spec.userdata = this;
  44.  
  45. // The 'callback' parameter specifies the callback function to use to
  46. // generate audio samples. This callback function will invoke the
  47. // waveform generator from it's given userdata, which is set above to
  48. // the 'this' pointer.
  49.  
  50. audio_spec.callback = [](void* userdata, Uint8* stream, int len) -> void
  51. {
  52. ((audio_driver*)(userdata))->waveform((short*)stream, len / 2);
  53. };
  54.  
  55. // Open the audio device.
  56.  
  57. SDL_AudioDeviceID audio_device = SDL_OpenAudioDevice(NULL, 0, &audio_spec, &audio_spec, 0);
  58.  
  59. // Pause the audio device.
  60.  
  61. SDL_PauseAudioDevice(audio_device, 0);
  62. }
  63.  
  64. // Waveform generator (plays a song).
  65.  
  66. int row = 63;
  67.  
  68. int freq1;
  69. int freq2;
  70.  
  71. int freq2b;
  72.  
  73. int count = 0;
  74.  
  75. void waveform(short* target, int num_samples)
  76. {
  77. static const char song[] = "057+5420+%7%+%7%5%4%2%457%0%0%754%2%+%%%5%542%457%0%0%042%2#+%!#0%+%$%%%";
  78.  
  79. for (int position = 0; position < num_samples; --count)
  80. {
  81. if (!count)
  82. {
  83. row = (row + 1) % 64;
  84.  
  85. freq1 = 17 + std::exp2(song[row + 8] / 12.0);
  86.  
  87. if (song[row + 8] == '%')
  88. {
  89. freq1 = 0;
  90. }
  91.  
  92. freq2 = 17 * std::exp2(song[row / 8] / 12.0);
  93.  
  94. if (row & 2)
  95. {
  96. freq2b = freq2 / 2;
  97. }
  98. else
  99. {
  100. freq2b = 0;
  101. }
  102.  
  103. count = 5400 + 540 * (row & 2);
  104. }
  105.  
  106. target[position++] = (8 * (((count + 85) * freq1 & 32767) - 16384) + 1 * (((count + 4) * freq1 * 8 & 32767) - 16384) + 3 * (((count + 57) * freq2 / 4 & 32767) - 16384) + 8 * (((count + 23) * freq2b & 32767) - 16384)) * count >> 16;
  107. }
  108. }
  109. };
  110.  
  111. int main(int argc, char** argv)
  112. {
  113. SDL_InitSubSystem(SDL_INIT_AUDIO);
  114.  
  115. audio_driver driver;
  116.  
  117. while (true)
  118. {
  119. SDL_Delay(500);
  120. }
  121.  
  122. return 0;
  123. }
Add Comment
Please, Sign In to add comment