audio_raw_stream.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*******************************************************************************************
  2. *
  3. * raylib [audio] example - Raw audio streaming
  4. *
  5. * NOTE: This example requires OpenAL Soft library installed
  6. *
  7. * This example has been created using raylib 1.6 (www.raylib.com)
  8. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  9. *
  10. * Copyright (c) 2015 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #if defined(PLATFORM_WEB)
  15. #include <emscripten/emscripten.h>
  16. #endif
  17. #include <stdlib.h> // Required for: malloc(), free()
  18. #include <math.h> // Required for: sinf()
  19. #define MAX_SAMPLES 22050
  20. #define MAX_SAMPLES_PER_UPDATE 4096
  21. //----------------------------------------------------------------------------------
  22. // Global Variables Definition
  23. //----------------------------------------------------------------------------------
  24. const int screenWidth = 800;
  25. const int screenHeight = 450;
  26. AudioStream stream;
  27. short *data;
  28. int totalSamples = MAX_SAMPLES;
  29. int samplesLeft = MAX_SAMPLES;
  30. Vector2 position = { 0, 0 };
  31. //----------------------------------------------------------------------------------
  32. // Module Functions Declaration
  33. //----------------------------------------------------------------------------------
  34. void UpdateDrawFrame(void); // Update and Draw one frame
  35. //----------------------------------------------------------------------------------
  36. // Main Enry Point
  37. //----------------------------------------------------------------------------------
  38. int main()
  39. {
  40. // Initialization
  41. //--------------------------------------------------------------------------------------
  42. InitWindow(screenWidth, screenHeight, "raylib [audio] example - raw audio streaming");
  43. InitAudioDevice(); // Initialize audio device
  44. // Init raw audio stream (sample rate: 22050, sample size: 16bit-short, channels: 1-mono)
  45. stream = InitAudioStream(22050, 16, 1);
  46. // Generate samples data from sine wave
  47. data = (short *)malloc(sizeof(short)*MAX_SAMPLES);
  48. // TODO: Review data generation, it seems data is discontinued for loop,
  49. // for that reason, there is a clip everytime audio stream is looped...
  50. for (int i = 0; i < MAX_SAMPLES; i++)
  51. {
  52. data[i] = (short)(sinf(((2*PI*(float)i)/2)*DEG2RAD)*32000);
  53. }
  54. PlayAudioStream(stream); // Start processing stream buffer (no data loaded currently)
  55. #if defined(PLATFORM_WEB)
  56. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  57. #else
  58. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  59. //--------------------------------------------------------------------------------------
  60. // Main game loop
  61. while (!WindowShouldClose()) // Detect window close button or ESC key
  62. {
  63. UpdateDrawFrame();
  64. }
  65. #endif
  66. // De-Initialization
  67. //--------------------------------------------------------------------------------------
  68. free(data); // Unload sine wave data
  69. CloseAudioStream(stream); // Close raw audio stream and delete buffers from RAM
  70. CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
  71. CloseWindow(); // Close window and OpenGL context
  72. //--------------------------------------------------------------------------------------
  73. return 0;
  74. }
  75. //----------------------------------------------------------------------------------
  76. // Module Functions Definition
  77. //----------------------------------------------------------------------------------
  78. void UpdateDrawFrame(void)
  79. {
  80. // Update
  81. //----------------------------------------------------------------------------------
  82. // Refill audio stream if required
  83. // NOTE: Every update we check if stream data has been already consumed and we update
  84. // buffer with new data from the generated samples, we upload data at a rate (MAX_SAMPLES_PER_UPDATE),
  85. // but notice that at some point we update < MAX_SAMPLES_PER_UPDATE data...
  86. if (IsAudioBufferProcessed(stream))
  87. {
  88. int numSamples = 0;
  89. if (samplesLeft >= MAX_SAMPLES_PER_UPDATE) numSamples = MAX_SAMPLES_PER_UPDATE;
  90. else numSamples = samplesLeft;
  91. UpdateAudioStream(stream, data + (totalSamples - samplesLeft), numSamples);
  92. samplesLeft -= numSamples;
  93. // Reset samples feeding (loop audio)
  94. if (samplesLeft <= 0) samplesLeft = totalSamples;
  95. }
  96. //----------------------------------------------------------------------------------
  97. // Draw
  98. //----------------------------------------------------------------------------------
  99. BeginDrawing();
  100. ClearBackground(RAYWHITE);
  101. DrawText("SINE WAVE SHOULD BE PLAYING!", 240, 140, 20, LIGHTGRAY);
  102. // NOTE: Draw a part of the sine wave (only screen width, proportional values)
  103. for (int i = 0; i < GetScreenWidth(); i++)
  104. {
  105. position.x = i;
  106. position.y = 250 + 50*data[i]/32000;
  107. DrawPixelV(position, RED);
  108. }
  109. EndDrawing();
  110. //----------------------------------------------------------------------------------
  111. }