-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
[raudio] Audio recording #5941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jn-jairo
wants to merge
2
commits into
raysan5:master
Choose a base branch
from
jn-jairo:audio-recording
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[raudio] Audio recording #5941
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| /******************************************************************************************* | ||
| * | ||
| * raylib [audio] example - stream recording | ||
| * | ||
| * Example complexity rating: [★★★☆] 3/4 | ||
| * | ||
| * Example originally created with raylib 6.0, last time updated with raylib 6.0 | ||
| * | ||
| * Example created by Jairo Correa (@jn-jairo) and reviewed by Ramon Santamaria (@raysan5) | ||
| * | ||
| * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, | ||
| * BSD-like license that allows static linking with closed source software | ||
| * | ||
| * Copyright (c) 2026 Jairo Correa (@jn-jairo) | ||
| * | ||
| ********************************************************************************************/ | ||
|
|
||
| #include "raylib.h" | ||
|
|
||
| #include <stdlib.h> // Required for: NULL | ||
| #include <string.h> // Required for: memcpy() | ||
|
|
||
| //---------------------------------------------------------------------------------- | ||
| // Global Variables Definition | ||
| //---------------------------------------------------------------------------------- | ||
| static unsigned int maxFrameCount = 0; | ||
| static Wave wave = { 0 }; | ||
|
|
||
| //------------------------------------------------------------------------------------ | ||
| // Module Functions Declaration | ||
| //------------------------------------------------------------------------------------ | ||
| static void RecordingCallback(const void *framesIn, unsigned int frameCount); | ||
|
|
||
| //------------------------------------------------------------------------------------ | ||
| // Program main entry point | ||
| //------------------------------------------------------------------------------------ | ||
| int main(void) | ||
| { | ||
| // Initialization | ||
| //-------------------------------------------------------------------------------------- | ||
| const int screenWidth = 800; | ||
| const int screenHeight = 450; | ||
|
|
||
| InitWindow(screenWidth, screenHeight, "raylib [audio] example - stream recording"); | ||
|
|
||
| InitAudioDevice(); | ||
| InitAudioRecordingDevice(); | ||
| // InitAudioRecordingDeviceEx(24000, 16, 1); | ||
Check noticeCode scanning / CodeQL Commented-out code Note
This comment appears to contain commented-out code.
|
||
|
|
||
| // Configure it so that RecordingCallback is called whenever new samples are read from the microphone | ||
| SetAudioRecordingCallback(RecordingCallback); | ||
|
|
||
| // Configure the wave to match the recording | ||
| wave.sampleRate = GetAudioRecordingSampleRate(); | ||
| wave.sampleSize = GetAudioRecordingSampleSize(); | ||
| wave.channels = GetAudioRecordingChannels(); | ||
|
|
||
| // Allocate buffer for the audio recording | ||
| maxFrameCount = wave.sampleRate*5; // 5 seconds | ||
| wave.data = RL_CALLOC(maxFrameCount*wave.channels, wave.sampleSize/8); | ||
|
|
||
| Sound sound = { 0 }; | ||
|
|
||
| float timeRecorded = 0.0f; | ||
|
|
||
| SetTargetFPS(60); // Set our game to run at 60 frames-per-second | ||
| //-------------------------------------------------------------------------------------- | ||
|
|
||
| // Main game loop | ||
| while (!WindowShouldClose()) // Detect window close button or ESC key | ||
| { | ||
| // Update | ||
| //---------------------------------------------------------------------------------- | ||
|
|
||
| // Space key pressed or the recording buffer is full | ||
| if (IsKeyPressed(KEY_SPACE) || (IsAudioRecording() && (wave.frameCount == maxFrameCount))) | ||
| { | ||
| if (IsAudioRecording()) | ||
| { | ||
| StopAudioRecording(); | ||
|
|
||
| // Reload and play the sound | ||
| UnloadSound(sound); | ||
| sound = LoadSoundFromWave(wave); | ||
| PlaySound(sound); | ||
| } | ||
| else | ||
| { | ||
| // Stop the sound and reset the recording frameCount | ||
| StopSound(sound); | ||
| wave.frameCount = 0; | ||
|
|
||
| StartAudioRecording(); | ||
| } | ||
| } | ||
|
|
||
| // Get timeRecorded scaled to bar dimensions | ||
| timeRecorded = ((float)wave.frameCount/(float)maxFrameCount)*(screenWidth - 40); | ||
| //---------------------------------------------------------------------------------- | ||
|
|
||
| // Draw | ||
| //---------------------------------------------------------------------------------- | ||
| BeginDrawing(); | ||
|
|
||
| ClearBackground(RAYWHITE); | ||
|
|
||
| // Draw time bar | ||
| DrawRectangle(20, screenHeight - 20 - 12, screenWidth - 40, 12, LIGHTGRAY); | ||
| DrawRectangle(20, screenHeight - 20 - 12, (int)timeRecorded, 12, MAROON); | ||
| DrawRectangleLines(20, screenHeight - 20 - 12, screenWidth - 40, 12, GRAY); | ||
|
|
||
| // Draw help instructions | ||
| DrawText("Press SPACE to START/STOP recording the sound!", 130, 180, 20, LIGHTGRAY); | ||
|
|
||
| EndDrawing(); | ||
| //---------------------------------------------------------------------------------- | ||
| } | ||
|
|
||
| // De-Initialization | ||
| //-------------------------------------------------------------------------------------- | ||
| CloseAudioRecordingDevice(); // Close audio recording device (recording is automatically stopped) | ||
| CloseAudioDevice(); // Close audio device (music streaming is automatically stopped) | ||
|
|
||
| UnloadSound(sound); | ||
| UnloadWave(wave); | ||
|
|
||
| CloseWindow(); // Close window and OpenGL context | ||
| //-------------------------------------------------------------------------------------- | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| //------------------------------------------------------------------------------------ | ||
| // Module Functions Definition | ||
| //------------------------------------------------------------------------------------ | ||
| static void RecordingCallback(const void *framesIn, unsigned int frameCount) | ||
| { | ||
| // Check how many frames left in the buffer | ||
| unsigned int framesLeft = maxFrameCount - wave.frameCount; | ||
|
|
||
| // Do not overflow the buffer | ||
| if (framesLeft == 0) return; | ||
| unsigned int framesToCopy = (frameCount < framesLeft)? frameCount : framesLeft; | ||
|
|
||
| // Append the new data | ||
| memcpy((unsigned char *)wave.data + wave.frameCount*wave.channels*wave.sampleSize/8, framesIn, framesToCopy*wave.channels*wave.sampleSize/8); | ||
| wave.frameCount += framesToCopy; | ||
| } | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.