audio_raw_stream.c 4.3 KB

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