Reviewed audio example
This commit is contained in:
parent
4d45173a77
commit
64e9d72c07
1 changed files with 18 additions and 17 deletions
|
@ -7,7 +7,7 @@
|
||||||
* This example has been created using raylib 1.6 (www.raylib.com)
|
* This example has been created using raylib 1.6 (www.raylib.com)
|
||||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||||
*
|
*
|
||||||
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
|
* Copyright (c) 2015-2018 Ramon Santamaria (@raysan5) and James Hofmann (@triplefox)
|
||||||
*
|
*
|
||||||
********************************************************************************************/
|
********************************************************************************************/
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@
|
||||||
#include <math.h> // Required for: sinf()
|
#include <math.h> // Required for: sinf()
|
||||||
#include <string.h> // Required for: memcpy()
|
#include <string.h> // Required for: memcpy()
|
||||||
|
|
||||||
#define MAX_SAMPLES 512
|
#define MAX_SAMPLES 512
|
||||||
#define MAX_SAMPLES_PER_UPDATE 4096
|
#define MAX_SAMPLES_PER_UPDATE 4096
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
|
@ -70,16 +70,17 @@ int main()
|
||||||
|
|
||||||
// Sample mouse input.
|
// Sample mouse input.
|
||||||
mousePosition = GetMousePosition();
|
mousePosition = GetMousePosition();
|
||||||
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) {
|
|
||||||
|
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
|
||||||
|
{
|
||||||
float fp = (float)(mousePosition.y);
|
float fp = (float)(mousePosition.y);
|
||||||
frequency = 40.0f + (float)(fp);
|
frequency = 40.0f + (float)(fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rewrite the sine wave.
|
// Rewrite the sine wave.
|
||||||
// Compute two cycles to allow the buffer padding, simplifying
|
// Compute two cycles to allow the buffer padding, simplifying any modulation, resampling, etc.
|
||||||
// any modulation, resampling, etc.
|
if (frequency != oldFrequency)
|
||||||
if (frequency != oldFrequency) {
|
{
|
||||||
|
|
||||||
// Compute wavelength. Limit size in both directions.
|
// Compute wavelength. Limit size in both directions.
|
||||||
int oldWavelength = waveLength;
|
int oldWavelength = waveLength;
|
||||||
waveLength = (int)(22050/frequency);
|
waveLength = (int)(22050/frequency);
|
||||||
|
@ -100,28 +101,29 @@ int main()
|
||||||
// Refill audio stream if required
|
// Refill audio stream if required
|
||||||
if (IsAudioBufferProcessed(stream))
|
if (IsAudioBufferProcessed(stream))
|
||||||
{
|
{
|
||||||
|
|
||||||
// Synthesize a buffer that is exactly the requested size
|
// Synthesize a buffer that is exactly the requested size
|
||||||
int writeCursor = 0;
|
int writeCursor = 0;
|
||||||
while(writeCursor < MAX_SAMPLES_PER_UPDATE) {
|
|
||||||
|
while (writeCursor < MAX_SAMPLES_PER_UPDATE)
|
||||||
|
{
|
||||||
// Start by trying to write the whole chunk at once
|
// Start by trying to write the whole chunk at once
|
||||||
int writeLength = MAX_SAMPLES_PER_UPDATE-writeCursor;
|
int writeLength = MAX_SAMPLES_PER_UPDATE-writeCursor;
|
||||||
|
|
||||||
// Limit to the maximum readable size
|
// Limit to the maximum readable size
|
||||||
int readLength = waveLength-readCursor;
|
int readLength = waveLength-readCursor;
|
||||||
if (writeLength > readLength)
|
|
||||||
{
|
if (writeLength > readLength) writeLength = readLength;
|
||||||
writeLength = readLength;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write the slice
|
// Write the slice
|
||||||
memcpy(writeBuf + writeCursor, data + readCursor, writeLength*sizeof(short));
|
memcpy(writeBuf + writeCursor, data + readCursor, writeLength*sizeof(short));
|
||||||
|
|
||||||
// Update cursors and loop audio
|
// Update cursors and loop audio
|
||||||
readCursor = (readCursor + writeLength) % waveLength;
|
readCursor = (readCursor + writeLength) % waveLength;
|
||||||
|
|
||||||
writeCursor += writeLength;
|
writeCursor += writeLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
// copy finished frame to audio stream
|
// Copy finished frame to audio stream
|
||||||
UpdateAudioStream(stream, writeBuf, MAX_SAMPLES_PER_UPDATE);
|
UpdateAudioStream(stream, writeBuf, MAX_SAMPLES_PER_UPDATE);
|
||||||
}
|
}
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
@ -132,7 +134,7 @@ int main()
|
||||||
|
|
||||||
ClearBackground(RAYWHITE);
|
ClearBackground(RAYWHITE);
|
||||||
|
|
||||||
DrawText(FormatText("Sine frequency: %i",(int)frequency), 240, 140, 20, LIGHTGRAY);
|
DrawText(FormatText("sine frequency: %i",(int)frequency), GetScreenWidth() - 220, 10, 20, RED);
|
||||||
DrawText("click mouse button to change frequency", 10, 10, 20, DARKGRAY);
|
DrawText("click mouse button to change frequency", 10, 10, 20, DARKGRAY);
|
||||||
|
|
||||||
// Draw the current buffer state proportionate to the screen
|
// Draw the current buffer state proportionate to the screen
|
||||||
|
@ -154,7 +156,6 @@ int main()
|
||||||
free(writeBuf); // Unload write buffer
|
free(writeBuf); // Unload write buffer
|
||||||
|
|
||||||
CloseAudioStream(stream); // Close raw audio stream and delete buffers from RAM
|
CloseAudioStream(stream); // Close raw audio stream and delete buffers from RAM
|
||||||
|
|
||||||
CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
|
CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
|
||||||
|
|
||||||
CloseWindow(); // Close window and OpenGL context
|
CloseWindow(); // Close window and OpenGL context
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue