audio_raw_stream.lua 3.3 KB

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