2016-08-02 18:32:24 +03:00
|
|
|
/*******************************************************************************************
|
|
|
|
*
|
|
|
|
* raylib [audio] example - Raw audio streaming
|
|
|
|
*
|
|
|
|
* 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)
|
|
|
|
*
|
2019-06-29 12:26:31 +03:00
|
|
|
* Example created by Ramon Santamaria (@raysan5) and reviewed by James Hofmann (@triplefox)
|
|
|
|
*
|
2019-04-07 18:49:12 +03:00
|
|
|
* Copyright (c) 2015-2019 Ramon Santamaria (@raysan5) and James Hofmann (@triplefox)
|
2016-08-02 18:32:24 +03:00
|
|
|
*
|
|
|
|
********************************************************************************************/
|
|
|
|
|
|
|
|
#include "raylib.h"
|
|
|
|
|
|
|
|
#include <stdlib.h> // Required for: malloc(), free()
|
|
|
|
#include <math.h> // Required for: sinf()
|
2018-07-25 13:01:48 +03:00
|
|
|
#include <string.h> // Required for: memcpy()
|
2016-08-02 18:32:24 +03:00
|
|
|
|
2018-07-25 15:38:12 +03:00
|
|
|
#define MAX_SAMPLES 512
|
2016-12-26 13:46:07 +03:00
|
|
|
#define MAX_SAMPLES_PER_UPDATE 4096
|
2016-08-03 10:26:30 +03:00
|
|
|
|
2019-05-20 17:36:42 +03:00
|
|
|
int main(void)
|
2016-08-02 18:32:24 +03:00
|
|
|
{
|
|
|
|
// Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
2019-05-20 17:36:42 +03:00
|
|
|
const int screenWidth = 800;
|
|
|
|
const int screenHeight = 450;
|
2016-08-02 18:32:24 +03:00
|
|
|
|
|
|
|
InitWindow(screenWidth, screenHeight, "raylib [audio] example - raw audio streaming");
|
|
|
|
|
|
|
|
InitAudioDevice(); // Initialize audio device
|
|
|
|
|
2016-12-25 03:58:56 +03:00
|
|
|
// Init raw audio stream (sample rate: 22050, sample size: 16bit-short, channels: 1-mono)
|
|
|
|
AudioStream stream = InitAudioStream(22050, 16, 1);
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-25 13:01:48 +03:00
|
|
|
// Buffer for the single cycle waveform we are synthesizing
|
2016-12-25 03:58:56 +03:00
|
|
|
short *data = (short *)malloc(sizeof(short)*MAX_SAMPLES);
|
2018-07-25 13:01:48 +03:00
|
|
|
|
|
|
|
// Frame buffer, describing the waveform when repeated over the course of a frame
|
|
|
|
short *writeBuf = (short *)malloc(sizeof(short)*MAX_SAMPLES_PER_UPDATE);
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2016-12-26 13:46:07 +03:00
|
|
|
PlayAudioStream(stream); // Start processing stream buffer (no data loaded currently)
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-25 13:01:48 +03:00
|
|
|
// Position read in to determine next frequency
|
|
|
|
Vector2 mousePosition = { -100.0f, -100.0f };
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-25 13:01:48 +03:00
|
|
|
// Cycles per second (hz)
|
|
|
|
float frequency = 440.0f;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-25 13:01:48 +03:00
|
|
|
// Previous value, used to test if sine needs to be rewritten, and to smoothly modulate frequency
|
|
|
|
float oldFrequency = 1.0f;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-25 13:01:48 +03:00
|
|
|
// Cursor to read and copy the samples of the sine wave buffer
|
|
|
|
int readCursor = 0;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-25 13:01:48 +03:00
|
|
|
// Computed size in samples of the sine wave
|
|
|
|
int waveLength = 1;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2016-08-02 18:32:24 +03:00
|
|
|
Vector2 position = { 0, 0 };
|
|
|
|
|
|
|
|
SetTargetFPS(30); // Set our game to run at 30 frames-per-second
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Main game loop
|
|
|
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
|
|
|
{
|
|
|
|
// Update
|
|
|
|
//----------------------------------------------------------------------------------
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-25 13:01:48 +03:00
|
|
|
// Sample mouse input.
|
|
|
|
mousePosition = GetMousePosition();
|
2019-05-20 17:36:42 +03:00
|
|
|
|
|
|
|
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
|
2018-07-25 15:38:12 +03:00
|
|
|
{
|
2018-07-25 13:01:48 +03:00
|
|
|
float fp = (float)(mousePosition.y);
|
|
|
|
frequency = 40.0f + (float)(fp);
|
|
|
|
}
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-25 13:01:48 +03:00
|
|
|
// Rewrite the sine wave.
|
2018-07-25 15:38:12 +03:00
|
|
|
// Compute two cycles to allow the buffer padding, simplifying any modulation, resampling, etc.
|
2019-05-20 17:36:42 +03:00
|
|
|
if (frequency != oldFrequency)
|
2018-07-25 15:38:12 +03:00
|
|
|
{
|
2018-07-25 13:01:48 +03:00
|
|
|
// Compute wavelength. Limit size in both directions.
|
|
|
|
int oldWavelength = waveLength;
|
|
|
|
waveLength = (int)(22050/frequency);
|
|
|
|
if (waveLength > MAX_SAMPLES/2) waveLength = MAX_SAMPLES/2;
|
|
|
|
if (waveLength < 1) waveLength = 1;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-25 13:01:48 +03:00
|
|
|
// Write sine wave.
|
|
|
|
for (int i = 0; i < waveLength*2; i++)
|
|
|
|
{
|
|
|
|
data[i] = (short)(sinf(((2*PI*(float)i/waveLength)))*32000);
|
|
|
|
}
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-25 13:01:48 +03:00
|
|
|
// Scale read cursor's position to minimize transition artifacts
|
|
|
|
readCursor = (int)(readCursor * ((float)waveLength / (float)oldWavelength));
|
|
|
|
oldFrequency = frequency;
|
|
|
|
}
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2016-08-02 18:32:24 +03:00
|
|
|
// Refill audio stream if required
|
2019-05-20 17:36:42 +03:00
|
|
|
if (IsAudioBufferProcessed(stream))
|
2016-08-02 18:32:24 +03:00
|
|
|
{
|
2018-07-25 13:01:48 +03:00
|
|
|
// Synthesize a buffer that is exactly the requested size
|
|
|
|
int writeCursor = 0;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
|
|
|
while (writeCursor < MAX_SAMPLES_PER_UPDATE)
|
2018-07-25 15:38:12 +03:00
|
|
|
{
|
2018-07-25 13:01:48 +03:00
|
|
|
// Start by trying to write the whole chunk at once
|
|
|
|
int writeLength = MAX_SAMPLES_PER_UPDATE-writeCursor;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-25 13:01:48 +03:00
|
|
|
// Limit to the maximum readable size
|
|
|
|
int readLength = waveLength-readCursor;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-25 15:38:12 +03:00
|
|
|
if (writeLength > readLength) writeLength = readLength;
|
2018-07-25 13:01:48 +03:00
|
|
|
|
|
|
|
// Write the slice
|
|
|
|
memcpy(writeBuf + writeCursor, data + readCursor, writeLength*sizeof(short));
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-25 13:01:48 +03:00
|
|
|
// Update cursors and loop audio
|
|
|
|
readCursor = (readCursor + writeLength) % waveLength;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-25 13:01:48 +03:00
|
|
|
writeCursor += writeLength;
|
|
|
|
}
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-25 15:38:12 +03:00
|
|
|
// Copy finished frame to audio stream
|
2018-07-25 13:01:48 +03:00
|
|
|
UpdateAudioStream(stream, writeBuf, MAX_SAMPLES_PER_UPDATE);
|
2016-08-02 18:32:24 +03:00
|
|
|
}
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Draw
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
BeginDrawing();
|
|
|
|
|
|
|
|
ClearBackground(RAYWHITE);
|
|
|
|
|
2018-07-25 15:38:12 +03:00
|
|
|
DrawText(FormatText("sine frequency: %i",(int)frequency), GetScreenWidth() - 220, 10, 20, RED);
|
2018-07-25 13:01:48 +03:00
|
|
|
DrawText("click mouse button to change frequency", 10, 10, 20, DARKGRAY);
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2018-07-25 13:01:48 +03:00
|
|
|
// Draw the current buffer state proportionate to the screen
|
|
|
|
for (int i = 0; i < screenWidth; i++)
|
2016-08-02 18:32:24 +03:00
|
|
|
{
|
|
|
|
position.x = i;
|
2018-07-25 13:01:48 +03:00
|
|
|
position.y = 250 + 50*data[i*MAX_SAMPLES/screenWidth]/32000;
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2016-08-02 18:32:24 +03:00
|
|
|
DrawPixelV(position, RED);
|
|
|
|
}
|
|
|
|
|
|
|
|
EndDrawing();
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
}
|
|
|
|
|
|
|
|
// De-Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
free(data); // Unload sine wave data
|
2018-07-25 13:01:48 +03:00
|
|
|
free(writeBuf); // Unload write buffer
|
2019-05-20 17:36:42 +03:00
|
|
|
|
2016-08-02 18:32:24 +03:00
|
|
|
CloseAudioStream(stream); // Close raw audio stream and delete buffers from RAM
|
|
|
|
CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
|
|
|
|
|
|
|
|
CloseWindow(); // Close window and OpenGL context
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
return 0;
|
2018-07-25 13:01:48 +03:00
|
|
|
}
|