audio_raw_stream.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #include <stdlib.h> // Required for: malloc(), free()
  15. #include <math.h> // Required for: sinf()
  16. #define MAX_SAMPLES 20000
  17. int main()
  18. {
  19. // Initialization
  20. //--------------------------------------------------------------------------------------
  21. int screenWidth = 800;
  22. int screenHeight = 450;
  23. InitWindow(screenWidth, screenHeight, "raylib [audio] example - raw audio streaming");
  24. InitAudioDevice(); // Initialize audio device
  25. // Init raw audio stream (sample rate: 22050, sample size: 32bit-float, channels: 1-mono)
  26. AudioStream stream = InitAudioStream(22050, 32, 1);
  27. // Fill audio stream with some samples (sine wave)
  28. float *data = (float *)malloc(sizeof(float)*MAX_SAMPLES);
  29. for (int i = 0; i < MAX_SAMPLES; i++)
  30. {
  31. data[i] = sinf(((2*PI*(float)i)/2)*DEG2RAD);
  32. }
  33. // NOTE: The generated MAX_SAMPLES do not fit to close a perfect loop
  34. // for that reason, there is a clip everytime audio stream is looped
  35. PlayAudioStream(stream);
  36. int totalSamples = MAX_SAMPLES;
  37. int samplesLeft = totalSamples;
  38. Vector2 position = { 0, 0 };
  39. SetTargetFPS(30); // Set our game to run at 30 frames-per-second
  40. //--------------------------------------------------------------------------------------
  41. // Main game loop
  42. while (!WindowShouldClose()) // Detect window close button or ESC key
  43. {
  44. // Update
  45. //----------------------------------------------------------------------------------
  46. // Refill audio stream if required
  47. if (IsAudioBufferProcessed(stream))
  48. {
  49. int numSamples = 0;
  50. if (samplesLeft >= 4096) numSamples = 4096;
  51. else numSamples = samplesLeft;
  52. UpdateAudioStream(stream, data + (totalSamples - samplesLeft), numSamples);
  53. samplesLeft -= numSamples;
  54. // Reset samples feeding (loop audio)
  55. if (samplesLeft <= 0) samplesLeft = totalSamples;
  56. }
  57. //----------------------------------------------------------------------------------
  58. // Draw
  59. //----------------------------------------------------------------------------------
  60. BeginDrawing();
  61. ClearBackground(RAYWHITE);
  62. DrawText("SINE WAVE SHOULD BE PLAYING!", 240, 140, 20, LIGHTGRAY);
  63. // NOTE: Draw a part of the sine wave (only screen width)
  64. for (int i = 0; i < GetScreenWidth(); i++)
  65. {
  66. position.x = i;
  67. position.y = 250 + 50*data[i];
  68. DrawPixelV(position, RED);
  69. }
  70. EndDrawing();
  71. //----------------------------------------------------------------------------------
  72. }
  73. // De-Initialization
  74. //--------------------------------------------------------------------------------------
  75. free(data); // Unload sine wave data
  76. CloseAudioStream(stream); // Close raw audio stream and delete buffers from RAM
  77. CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
  78. CloseWindow(); // Close window and OpenGL context
  79. //--------------------------------------------------------------------------------------
  80. return 0;
  81. }