| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
#if defined(RAUDIO_STANDALONE) |
| 73 |
#include "raudio.h" |
| 74 |
#else |
| 75 |
#include "raylib.h" |
| 76 |
|
| 77 |
#include "config.h" |
| 78 |
#endif |
| 79 |
|
| 80 |
#if SUPPORT_MODULE_RAUDIO || defined(RAUDIO_STANDALONE) |
| 81 |
|
| 82 |
#if defined(_WIN32) |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
#define NOGDICAPMASKS |
| 90 |
#define NOVIRTUALKEYCODES |
| 91 |
#define NOWINMESSAGES |
| 92 |
#define NOWINSTYLES |
| 93 |
#define NOSYSMETRICS |
| 94 |
#define NOMENUS |
| 95 |
#define NOICONS |
| 96 |
#define NOKEYSTATES |
| 97 |
#define NOSYSCOMMANDS |
| 98 |
#define NORASTEROPS |
| 99 |
#define NOSHOWWINDOW |
| 100 |
#define OEMRESOURCE |
| 101 |
#define NOATOM |
| 102 |
#define NOCLIPBOARD |
| 103 |
#define NOCOLOR |
| 104 |
#define NOCTLMGR |
| 105 |
#define NODRAWTEXT |
| 106 |
#define NOGDI |
| 107 |
#define NOKERNEL |
| 108 |
#define NOUSER |
| 109 |
|
| 110 |
#define NOMB |
| 111 |
#define NOMEMMGR |
| 112 |
#define NOMETAFILE |
| 113 |
#define NOMINMAX |
| 114 |
#define NOMSG |
| 115 |
#define NOOPENFILE |
| 116 |
#define NOSCROLL |
| 117 |
#define NOSERVICE |
| 118 |
#define NOSOUND |
| 119 |
#define NOTEXTMETRIC |
| 120 |
#define NOWH |
| 121 |
#define NOWINOFFSETS |
| 122 |
#define NOCOMM |
| 123 |
#define NOKANJI |
| 124 |
#define NOHELP |
| 125 |
#define NOPROFILER |
| 126 |
#define NODEFERWINDOWPOS |
| 127 |
#define NOMCX |
| 128 |
|
| 129 |
|
| 130 |
typedef struct tagMSG *LPMSG; |
| 131 |
|
| 132 |
#include <windows.h> |
| 133 |
|
| 134 |
|
| 135 |
typedef struct tagBITMAPINFOHEADER { |
| 136 |
DWORD biSize; |
| 137 |
LONG biWidth; |
| 138 |
LONG biHeight; |
| 139 |
WORD biPlanes; |
| 140 |
WORD biBitCount; |
| 141 |
DWORD biCompression; |
| 142 |
DWORD biSizeImage; |
| 143 |
LONG biXPelsPerMeter; |
| 144 |
LONG biYPelsPerMeter; |
| 145 |
DWORD biClrUsed; |
| 146 |
DWORD biClrImportant; |
| 147 |
} BITMAPINFOHEADER, *PBITMAPINFOHEADER; |
| 148 |
|
| 149 |
#include <objbase.h> |
| 150 |
#include <mmreg.h> |
| 151 |
#include <mmsystem.h> |
| 152 |
|
| 153 |
|
| 154 |
#if defined(_MSC_VER) || defined(__TINYC__) |
| 155 |
#include "propidl.h" |
| 156 |
#endif |
| 157 |
#endif |
| 158 |
|
| 159 |
#define MA_MALLOC RL_MALLOC |
| 160 |
#define MA_FREE RL_FREE |
| 161 |
|
| 162 |
#define MA_NO_JACK |
| 163 |
#define MA_NO_WAV |
| 164 |
#define MA_NO_FLAC |
| 165 |
#define MA_NO_MP3 |
| 166 |
#define MA_NO_RESOURCE_MANAGER |
| 167 |
#define MA_NO_NODE_GRAPH |
| 168 |
#define MA_NO_ENGINE |
| 169 |
#define MA_NO_GENERATION |
| 170 |
|
| 171 |
|
| 172 |
#define MA_COINIT_VALUE 2 // [2] COINIT_APARTMENTTHREADED: Each object has its own thread (apartment model) |
| 173 |
|
| 174 |
#define MINIAUDIO_IMPLEMENTATION |
| 175 |
|
| 176 |
#include "external/miniaudio.h" |
| 177 |
#undef PlaySound // Win32 API: windows.h > mmsystem.h defines PlaySound macro |
| 178 |
|
| 179 |
#include <stdlib.h> |
| 180 |
#include <stdio.h> |
| 181 |
#include <string.h> |
| 182 |
|
| 183 |
#if defined(RAUDIO_STANDALONE) |
| 184 |
#ifndef TRACELOG |
| 185 |
#define TRACELOG(level, ...) printf(__VA_ARGS__) |
| 186 |
#endif |
| 187 |
|
| 188 |
|
| 189 |
#ifndef RL_MALLOC |
| 190 |
#define RL_MALLOC(sz) malloc(sz) |
| 191 |
#endif |
| 192 |
#ifndef RL_CALLOC |
| 193 |
#define RL_CALLOC(n,sz) calloc(n,sz) |
| 194 |
#endif |
| 195 |
#ifndef RL_REALLOC |
| 196 |
#define RL_REALLOC(ptr,sz) realloc(ptr,sz) |
| 197 |
#endif |
| 198 |
#ifndef RL_FREE |
| 199 |
#define RL_FREE(ptr) free(ptr) |
| 200 |
#endif |
| 201 |
#endif |
| 202 |
|
| 203 |
#if SUPPORT_FILEFORMAT_WAV |
| 204 |
#define DRWAV_MALLOC RL_MALLOC |
| 205 |
#define DRWAV_REALLOC RL_REALLOC |
| 206 |
#define DRWAV_FREE RL_FREE |
| 207 |
|
| 208 |
#define DR_WAV_IMPLEMENTATION |
| 209 |
#include "external/dr_wav.h" |
| 210 |
#endif |
| 211 |
|
| 212 |
#if SUPPORT_FILEFORMAT_OGG |
| 213 |
|
| 214 |
#include "external/stb_vorbis.c" |
| 215 |
#endif |
| 216 |
|
| 217 |
#if SUPPORT_FILEFORMAT_MP3 |
| 218 |
#define DRMP3_MALLOC RL_MALLOC |
| 219 |
#define DRMP3_REALLOC RL_REALLOC |
| 220 |
#define DRMP3_FREE RL_FREE |
| 221 |
|
| 222 |
#define DR_MP3_IMPLEMENTATION |
| 223 |
#include "external/dr_mp3.h" |
| 224 |
#endif |
| 225 |
|
| 226 |
#if SUPPORT_FILEFORMAT_QOA |
| 227 |
#define QOA_MALLOC RL_MALLOC |
| 228 |
#define QOA_FREE RL_FREE |
| 229 |
|
| 230 |
#if defined(_MSC_VER) |
| 231 |
#pragma warning(push) |
| 232 |
#pragma warning(disable : 4018) |
| 233 |
#pragma warning(disable : 4267) |
| 234 |
#pragma warning(disable : 4244) |
| 235 |
#endif |
| 236 |
|
| 237 |
#define QOA_IMPLEMENTATION |
| 238 |
#include "external/qoa.h" |
| 239 |
#include "external/qoaplay.c" |
| 240 |
|
| 241 |
#if defined(_MSC_VER) |
| 242 |
#pragma warning(pop) // Disable MSVC warning suppression |
| 243 |
#endif |
| 244 |
#endif |
| 245 |
|
| 246 |
#if SUPPORT_FILEFORMAT_FLAC |
| 247 |
#define DRFLAC_MALLOC RL_MALLOC |
| 248 |
#define DRFLAC_REALLOC RL_REALLOC |
| 249 |
#define DRFLAC_FREE RL_FREE |
| 250 |
|
| 251 |
#define DR_FLAC_IMPLEMENTATION |
| 252 |
#define DR_FLAC_NO_WIN32_IO |
| 253 |
#include "external/dr_flac.h" |
| 254 |
#endif |
| 255 |
|
| 256 |
#if SUPPORT_FILEFORMAT_XM |
| 257 |
#define JARXM_MALLOC RL_MALLOC |
| 258 |
#define JARXM_FREE RL_FREE |
| 259 |
|
| 260 |
#if defined(_MSC_VER) |
| 261 |
#pragma warning(push) |
| 262 |
#pragma warning(disable : 4244) |
| 263 |
#endif |
| 264 |
|
| 265 |
#define JAR_XM_IMPLEMENTATION |
| 266 |
#include "external/jar_xm.h" |
| 267 |
|
| 268 |
#if defined(_MSC_VER) |
| 269 |
#pragma warning(pop) // Disable MSVC warning suppression |
| 270 |
#endif |
| 271 |
#endif |
| 272 |
|
| 273 |
#if SUPPORT_FILEFORMAT_MOD |
| 274 |
#define JARMOD_MALLOC RL_MALLOC |
| 275 |
#define JARMOD_FREE RL_FREE |
| 276 |
|
| 277 |
#define JAR_MOD_IMPLEMENTATION |
| 278 |
#include "external/jar_mod.h" |
| 279 |
#endif |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
#ifndef AUDIO_DEVICE_FORMAT |
| 285 |
#define AUDIO_DEVICE_FORMAT ma_format_f32 // Device output format (float-32bit) |
| 286 |
#endif |
| 287 |
#ifndef AUDIO_DEVICE_CHANNELS |
| 288 |
#define AUDIO_DEVICE_CHANNELS 2 // Device output channels: stereo |
| 289 |
#endif |
| 290 |
#ifndef AUDIO_DEVICE_SAMPLE_RATE |
| 291 |
#define AUDIO_DEVICE_SAMPLE_RATE 0 // Device output sample rate |
| 292 |
#endif |
| 293 |
#ifndef AUDIO_DEVICE_PERIOD_SIZE_IN_FRAMES |
| 294 |
#define AUDIO_DEVICE_PERIOD_SIZE_IN_FRAMES 0 // Device latency. 0 defaults to 10ms |
| 295 |
#endif |
| 296 |
|
| 297 |
#ifndef MAX_AUDIO_BUFFER_POOL_CHANNELS |
| 298 |
#define MAX_AUDIO_BUFFER_POOL_CHANNELS 16 // Audio pool channels |
| 299 |
#endif |
| 300 |
|
| 301 |
#ifndef AUDIO_BUFFER_RESIDUAL_CAPACITY |
| 302 |
#define AUDIO_BUFFER_RESIDUAL_CAPACITY 8 // In PCM frames, for resampling and pitch shifting |
| 303 |
#endif |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
#if defined(RAUDIO_STANDALONE) |
| 309 |
|
| 310 |
|
| 311 |
typedef enum { |
| 312 |
LOG_ALL = 0, |
| 313 |
LOG_TRACE, |
| 314 |
LOG_DEBUG, |
| 315 |
LOG_INFO, |
| 316 |
LOG_WARNING, |
| 317 |
LOG_ERROR, |
| 318 |
LOG_FATAL, |
| 319 |
LOG_NONE |
| 320 |
} TraceLogLevel; |
| 321 |
#endif |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
typedef enum { |
| 327 |
MUSIC_AUDIO_NONE = 0, |
| 328 |
MUSIC_AUDIO_WAV, |
| 329 |
MUSIC_AUDIO_OGG, |
| 330 |
MUSIC_AUDIO_FLAC, |
| 331 |
MUSIC_AUDIO_MP3, |
| 332 |
MUSIC_AUDIO_QOA, |
| 333 |
MUSIC_MODULE_XM, |
| 334 |
MUSIC_MODULE_MOD |
| 335 |
} MusicContextType; |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
typedef enum { |
| 340 |
AUDIO_BUFFER_USAGE_STATIC = 0, |
| 341 |
AUDIO_BUFFER_USAGE_STREAM |
| 342 |
} AudioBufferUsage; |
| 343 |
|
| 344 |
|
| 345 |
struct rAudioBuffer { |
| 346 |
ma_data_converter converter; |
| 347 |
unsigned char* converterResidual; |
| 348 |
unsigned int converterResidualCount; |
| 349 |
|
| 350 |
AudioCallback callback; |
| 351 |
rAudioProcessor *processor; |
| 352 |
|
| 353 |
float volume; |
| 354 |
float pitch; |
| 355 |
float pan; |
| 356 |
|
| 357 |
bool playing; |
| 358 |
bool paused; |
| 359 |
bool looping; |
| 360 |
int usage; |
| 361 |
|
| 362 |
bool isSubBufferProcessed[2]; |
| 363 |
unsigned int sizeInFrames; |
| 364 |
unsigned int frameCursorPos; |
| 365 |
unsigned int framesProcessed; |
| 366 |
|
| 367 |
unsigned char *data; |
| 368 |
|
| 369 |
rAudioBuffer *next; |
| 370 |
rAudioBuffer *prev; |
| 371 |
}; |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
struct rAudioProcessor { |
| 376 |
AudioCallback process; |
| 377 |
rAudioProcessor *next; |
| 378 |
rAudioProcessor *prev; |
| 379 |
}; |
| 380 |
|
| 381 |
#define AudioBuffer rAudioBuffer // HACK: To avoid CoreAudio (macOS) symbol collision |
| 382 |
|
| 383 |
|
| 384 |
typedef struct AudioData { |
| 385 |
struct { |
| 386 |
ma_context context; |
| 387 |
ma_device device; |
| 388 |
ma_mutex lock; |
| 389 |
bool isReady; |
| 390 |
size_t pcmBufferSize; |
| 391 |
void *pcmBuffer; |
| 392 |
} System; |
| 393 |
struct { |
| 394 |
AudioBuffer *first; |
| 395 |
AudioBuffer *last; |
| 396 |
int defaultSize; |
| 397 |
} Buffer; |
| 398 |
rAudioProcessor *mixedProcessor; |
| 399 |
} AudioData; |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
static AudioData AUDIO = { |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
.Buffer.defaultSize = 0, |
| 411 |
.mixedProcessor = NULL |
| 412 |
}; |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
static void OnLog(void *pUserData, ma_uint32 level, const char *pMessage); |
| 418 |
|
| 419 |
|
| 420 |
static ma_uint32 ReadAudioBufferFramesInInternalFormat(AudioBuffer *audioBuffer, void *framesOut, ma_uint32 frameCount); |
| 421 |
static ma_uint32 ReadAudioBufferFramesInMixingFormat(AudioBuffer *audioBuffer, float *framesOut, ma_uint32 frameCount); |
| 422 |
|
| 423 |
static void OnSendAudioDataToDevice(ma_device *pDevice, void *pFramesOut, const void *pFramesInput, ma_uint32 frameCount); |
| 424 |
static void MixAudioFrames(float *framesOut, const float *framesIn, ma_uint32 frameCount, AudioBuffer *buffer); |
| 425 |
|
| 426 |
static bool IsAudioBufferPlayingInLockedState(AudioBuffer *buffer); |
| 427 |
static void StopAudioBufferInLockedState(AudioBuffer *buffer); |
| 428 |
static void UpdateAudioStreamInLockedState(AudioStream stream, const void *data, int frameCount); |
| 429 |
|
| 430 |
#if defined(RAUDIO_STANDALONE) |
| 431 |
static bool IsFileExtension(const char *fileName, const char *ext); |
| 432 |
static const char *GetFileExtension(const char *fileName); |
| 433 |
static const char *GetFileName(const char *filePath); |
| 434 |
static const char *GetFileNameWithoutExt(const char *filePath); |
| 435 |
|
| 436 |
static unsigned char *LoadFileData(const char *fileName, int *dataSize); |
| 437 |
static bool SaveFileData(const char *fileName, void *data, int dataSize); |
| 438 |
static bool SaveFileText(const char *fileName, char *text); |
| 439 |
#endif |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
AudioBuffer *LoadAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sampleRate, ma_uint32 sizeInFrames, int usage); |
| 446 |
void UnloadAudioBuffer(AudioBuffer *buffer); |
| 447 |
|
| 448 |
bool IsAudioBufferPlaying(AudioBuffer *buffer); |
| 449 |
void PlayAudioBuffer(AudioBuffer *buffer); |
| 450 |
void StopAudioBuffer(AudioBuffer *buffer); |
| 451 |
void PauseAudioBuffer(AudioBuffer *buffer); |
| 452 |
void ResumeAudioBuffer(AudioBuffer *buffer); |
| 453 |
void SetAudioBufferVolume(AudioBuffer *buffer, float volume); |
| 454 |
void SetAudioBufferPitch(AudioBuffer *buffer, float pitch); |
| 455 |
void SetAudioBufferPan(AudioBuffer *buffer, float pan); |
| 456 |
void TrackAudioBuffer(AudioBuffer *buffer); |
| 457 |
void UntrackAudioBuffer(AudioBuffer *buffer); |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
void InitAudioDevice(void) |
| 465 |
{ |
| 466 |
|
| 467 |
ma_context_config ctxConfig = ma_context_config_init(); |
| 468 |
ma_log_callback_init(OnLog, NULL); |
| 469 |
|
| 470 |
ma_result result = ma_context_init(NULL, 0, &ctxConfig, &AUDIO.System.context); |
| 471 |
if (result != MA_SUCCESS) |
| 472 |
{ |
| 473 |
TRACELOG(LOG_WARNING, "AUDIO: Failed to initialize context"); |
| 474 |
return; |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
ma_device_config config = ma_device_config_init(ma_device_type_playback); |
| 480 |
config.playback.pDeviceID = NULL; |
| 481 |
config.playback.format = AUDIO_DEVICE_FORMAT; |
| 482 |
config.playback.channels = AUDIO_DEVICE_CHANNELS; |
| 483 |
config.sampleRate = AUDIO_DEVICE_SAMPLE_RATE; |
| 484 |
config.periodSizeInFrames = AUDIO_DEVICE_PERIOD_SIZE_IN_FRAMES; |
| 485 |
config.dataCallback = OnSendAudioDataToDevice; |
| 486 |
config.pUserData = NULL; |
| 487 |
config.noPreSilencedOutputBuffer = true; |
| 488 |
config.noFixedSizedCallback = true; |
| 489 |
|
| 490 |
result = ma_device_init(&AUDIO.System.context, &config, &AUDIO.System.device); |
| 491 |
if (result != MA_SUCCESS) |
| 492 |
{ |
| 493 |
TRACELOG(LOG_WARNING, "AUDIO: Failed to initialize playback device"); |
| 494 |
ma_context_uninit(&AUDIO.System.context); |
| 495 |
return; |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
if (ma_mutex_init(&AUDIO.System.lock) != MA_SUCCESS) |
| 502 |
{ |
| 503 |
TRACELOG(LOG_WARNING, "AUDIO: Failed to create mutex for mixing"); |
| 504 |
ma_device_uninit(&AUDIO.System.device); |
| 505 |
ma_context_uninit(&AUDIO.System.context); |
| 506 |
return; |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
result = ma_device_start(&AUDIO.System.device); |
| 512 |
if (result != MA_SUCCESS) |
| 513 |
{ |
| 514 |
TRACELOG(LOG_WARNING, "AUDIO: Failed to start playback device"); |
| 515 |
ma_device_uninit(&AUDIO.System.device); |
| 516 |
ma_context_uninit(&AUDIO.System.context); |
| 517 |
return; |
| 518 |
} |
| 519 |
|
| 520 |
TRACELOG(LOG_INFO, "AUDIO: Device initialized successfully"); |
| 521 |
TRACELOG(LOG_INFO, " > Backend: miniaudio | %s", ma_get_backend_name(AUDIO.System.context.backend)); |
| 522 |
TRACELOG(LOG_INFO, " > Format: %s -> %s", ma_get_format_name(AUDIO.System.device.playback.format), ma_get_format_name(AUDIO.System.device.playback.internalFormat)); |
| 523 |
TRACELOG(LOG_INFO, " > Channels: %d -> %d", AUDIO.System.device.playback.channels, AUDIO.System.device.playback.internalChannels); |
| 524 |
TRACELOG(LOG_INFO, " > Sample rate: %d -> %d", AUDIO.System.device.sampleRate, AUDIO.System.device.playback.internalSampleRate); |
| 525 |
TRACELOG(LOG_INFO, " > Periods size: %d", AUDIO.System.device.playback.internalPeriodSizeInFrames*AUDIO.System.device.playback.internalPeriods); |
| 526 |
|
| 527 |
AUDIO.System.isReady = true; |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
void CloseAudioDevice(void) |
| 532 |
{ |
| 533 |
if (AUDIO.System.isReady) |
| 534 |
{ |
| 535 |
ma_mutex_uninit(&AUDIO.System.lock); |
| 536 |
ma_device_uninit(&AUDIO.System.device); |
| 537 |
ma_context_uninit(&AUDIO.System.context); |
| 538 |
|
| 539 |
AUDIO.System.isReady = false; |
| 540 |
RL_FREE(AUDIO.System.pcmBuffer); |
| 541 |
AUDIO.System.pcmBuffer = NULL; |
| 542 |
AUDIO.System.pcmBufferSize = 0; |
| 543 |
|
| 544 |
TRACELOG(LOG_INFO, "AUDIO: Device closed successfully"); |
| 545 |
} |
| 546 |
else TRACELOG(LOG_WARNING, "AUDIO: Device could not be closed, not currently initialized"); |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
bool IsAudioDeviceReady(void) |
| 551 |
{ |
| 552 |
return AUDIO.System.isReady; |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
void SetMasterVolume(float volume) |
| 557 |
{ |
| 558 |
ma_device_set_master_volume(&AUDIO.System.device, volume); |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
float GetMasterVolume(void) |
| 563 |
{ |
| 564 |
float volume = 0.0f; |
| 565 |
ma_device_get_master_volume(&AUDIO.System.device, &volume); |
| 566 |
return volume; |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
AudioBuffer *LoadAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sampleRate, ma_uint32 sizeInFrames, int usage) |
| 575 |
{ |
| 576 |
AudioBuffer *audioBuffer = (AudioBuffer *)RL_CALLOC(1, sizeof(AudioBuffer)); |
| 577 |
|
| 578 |
if (audioBuffer == NULL) |
| 579 |
{ |
| 580 |
TRACELOG(LOG_WARNING, "AUDIO: Failed to allocate memory for buffer"); |
| 581 |
return NULL; |
| 582 |
} |
| 583 |
|
| 584 |
if (sizeInFrames > 0) audioBuffer->data = (unsigned char *)RL_CALLOC(sizeInFrames*channels*ma_get_bytes_per_sample(format), 1); |
| 585 |
|
| 586 |
|
| 587 |
ma_data_converter_config converterConfig = ma_data_converter_config_init(format, AUDIO_DEVICE_FORMAT, channels, AUDIO_DEVICE_CHANNELS, sampleRate, AUDIO.System.device.sampleRate); |
| 588 |
converterConfig.allowDynamicSampleRate = true; |
| 589 |
|
| 590 |
ma_result result = ma_data_converter_init(&converterConfig, NULL, &audioBuffer->converter); |
| 591 |
|
| 592 |
if (result != MA_SUCCESS) |
| 593 |
{ |
| 594 |
TRACELOG(LOG_WARNING, "AUDIO: Failed to create data conversion pipeline"); |
| 595 |
RL_FREE(audioBuffer); |
| 596 |
return NULL; |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
audioBuffer->converterResidualCount = 0; |
| 606 |
audioBuffer->converterResidual = (unsigned char*)RL_CALLOC(AUDIO_BUFFER_RESIDUAL_CAPACITY*ma_get_bytes_per_frame(format, channels), 1); |
| 607 |
|
| 608 |
|
| 609 |
audioBuffer->volume = 1.0f; |
| 610 |
audioBuffer->pitch = 1.0f; |
| 611 |
audioBuffer->pan = 0.0f; |
| 612 |
|
| 613 |
audioBuffer->callback = NULL; |
| 614 |
audioBuffer->processor = NULL; |
| 615 |
|
| 616 |
audioBuffer->playing = false; |
| 617 |
audioBuffer->paused = false; |
| 618 |
audioBuffer->looping = false; |
| 619 |
|
| 620 |
audioBuffer->usage = usage; |
| 621 |
audioBuffer->frameCursorPos = 0; |
| 622 |
audioBuffer->framesProcessed = 0; |
| 623 |
audioBuffer->sizeInFrames = sizeInFrames; |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
audioBuffer->isSubBufferProcessed[0] = true; |
| 628 |
audioBuffer->isSubBufferProcessed[1] = true; |
| 629 |
|
| 630 |
|
| 631 |
TrackAudioBuffer(audioBuffer); |
| 632 |
|
| 633 |
return audioBuffer; |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
void UnloadAudioBuffer(AudioBuffer *buffer) |
| 638 |
{ |
| 639 |
if (buffer != NULL) |
| 640 |
{ |
| 641 |
UntrackAudioBuffer(buffer); |
| 642 |
ma_data_converter_uninit(&buffer->converter, NULL); |
| 643 |
RL_FREE(buffer->converterResidual); |
| 644 |
RL_FREE(buffer->data); |
| 645 |
RL_FREE(buffer); |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
bool IsAudioBufferPlaying(AudioBuffer *buffer) |
| 651 |
{ |
| 652 |
bool result = false; |
| 653 |
ma_mutex_lock(&AUDIO.System.lock); |
| 654 |
result = IsAudioBufferPlayingInLockedState(buffer); |
| 655 |
ma_mutex_unlock(&AUDIO.System.lock); |
| 656 |
return result; |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
void PlayAudioBuffer(AudioBuffer *buffer) |
| 663 |
{ |
| 664 |
if (buffer != NULL) |
| 665 |
{ |
| 666 |
ma_mutex_lock(&AUDIO.System.lock); |
| 667 |
buffer->playing = true; |
| 668 |
buffer->paused = false; |
| 669 |
buffer->frameCursorPos = 0; |
| 670 |
buffer->framesProcessed = 0; |
| 671 |
buffer->isSubBufferProcessed[0] = true; |
| 672 |
buffer->isSubBufferProcessed[1] = true; |
| 673 |
ma_mutex_unlock(&AUDIO.System.lock); |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
|
| 678 |
void StopAudioBuffer(AudioBuffer *buffer) |
| 679 |
{ |
| 680 |
ma_mutex_lock(&AUDIO.System.lock); |
| 681 |
StopAudioBufferInLockedState(buffer); |
| 682 |
ma_mutex_unlock(&AUDIO.System.lock); |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
void PauseAudioBuffer(AudioBuffer *buffer) |
| 687 |
{ |
| 688 |
if (buffer != NULL) |
| 689 |
{ |
| 690 |
ma_mutex_lock(&AUDIO.System.lock); |
| 691 |
buffer->paused = true; |
| 692 |
ma_mutex_unlock(&AUDIO.System.lock); |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
void ResumeAudioBuffer(AudioBuffer *buffer) |
| 698 |
{ |
| 699 |
if (buffer != NULL) |
| 700 |
{ |
| 701 |
ma_mutex_lock(&AUDIO.System.lock); |
| 702 |
buffer->paused = false; |
| 703 |
ma_mutex_unlock(&AUDIO.System.lock); |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
void SetAudioBufferVolume(AudioBuffer *buffer, float volume) |
| 709 |
{ |
| 710 |
if (buffer != NULL) |
| 711 |
{ |
| 712 |
ma_mutex_lock(&AUDIO.System.lock); |
| 713 |
buffer->volume = volume; |
| 714 |
ma_mutex_unlock(&AUDIO.System.lock); |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
|
| 719 |
void SetAudioBufferPitch(AudioBuffer *buffer, float pitch) |
| 720 |
{ |
| 721 |
if ((buffer != NULL) && (pitch > 0.0f)) |
| 722 |
{ |
| 723 |
ma_mutex_lock(&AUDIO.System.lock); |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
ma_uint32 outputSampleRate = (ma_uint32)((float)AUDIO.System.device.sampleRate/pitch); |
| 729 |
ma_data_converter_set_rate(&buffer->converter, buffer->converter.sampleRateIn, outputSampleRate); |
| 730 |
|
| 731 |
buffer->pitch = pitch; |
| 732 |
ma_mutex_unlock(&AUDIO.System.lock); |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
void SetAudioBufferPan(AudioBuffer *buffer, float pan) |
| 738 |
{ |
| 739 |
if (pan < -1.0f) pan = -1.0f; |
| 740 |
else if (pan > 1.0f) pan = 1.0f; |
| 741 |
|
| 742 |
if (buffer != NULL) |
| 743 |
{ |
| 744 |
ma_mutex_lock(&AUDIO.System.lock); |
| 745 |
buffer->pan = pan; |
| 746 |
ma_mutex_unlock(&AUDIO.System.lock); |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
|
| 751 |
void TrackAudioBuffer(AudioBuffer *buffer) |
| 752 |
{ |
| 753 |
ma_mutex_lock(&AUDIO.System.lock); |
| 754 |
{ |
| 755 |
if (AUDIO.Buffer.first == NULL) AUDIO.Buffer.first = buffer; |
| 756 |
else |
| 757 |
{ |
| 758 |
AUDIO.Buffer.last->next = buffer; |
| 759 |
buffer->prev = AUDIO.Buffer.last; |
| 760 |
} |
| 761 |
|
| 762 |
AUDIO.Buffer.last = buffer; |
| 763 |
} |
| 764 |
ma_mutex_unlock(&AUDIO.System.lock); |
| 765 |
} |
| 766 |
|
| 767 |
|
| 768 |
void UntrackAudioBuffer(AudioBuffer *buffer) |
| 769 |
{ |
| 770 |
ma_mutex_lock(&AUDIO.System.lock); |
| 771 |
{ |
| 772 |
if (buffer->prev == NULL) AUDIO.Buffer.first = buffer->next; |
| 773 |
else buffer->prev->next = buffer->next; |
| 774 |
|
| 775 |
if (buffer->next == NULL) AUDIO.Buffer.last = buffer->prev; |
| 776 |
else buffer->next->prev = buffer->prev; |
| 777 |
|
| 778 |
buffer->prev = NULL; |
| 779 |
buffer->next = NULL; |
| 780 |
} |
| 781 |
ma_mutex_unlock(&AUDIO.System.lock); |
| 782 |
} |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
Wave LoadWave(const char *fileName) |
| 790 |
{ |
| 791 |
Wave wave = { 0 }; |
| 792 |
|
| 793 |
|
| 794 |
int dataSize = 0; |
| 795 |
unsigned char *fileData = LoadFileData(fileName, &dataSize); |
| 796 |
|
| 797 |
|
| 798 |
if (fileData != NULL) wave = LoadWaveFromMemory(GetFileExtension(fileName), fileData, dataSize); |
| 799 |
|
| 800 |
UnloadFileData(fileData); |
| 801 |
|
| 802 |
return wave; |
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
Wave LoadWaveFromMemory(const char *fileType, const unsigned char *fileData, int dataSize) |
| 808 |
{ |
| 809 |
Wave wave = { 0 }; |
| 810 |
|
| 811 |
if (false) { } |
| 812 |
#if SUPPORT_FILEFORMAT_WAV |
| 813 |
else if ((strcmp(fileType, ".wav") == 0) || (strcmp(fileType, ".WAV") == 0)) |
| 814 |
{ |
| 815 |
drwav wav = { 0 }; |
| 816 |
bool success = drwav_init_memory(&wav, fileData, dataSize, NULL); |
| 817 |
|
| 818 |
if (success) |
| 819 |
{ |
| 820 |
wave.frameCount = (unsigned int)wav.totalPCMFrameCount; |
| 821 |
wave.sampleRate = wav.sampleRate; |
| 822 |
wave.sampleSize = 16; |
| 823 |
wave.channels = wav.channels; |
| 824 |
wave.data = (short *)RL_MALLOC((size_t)wave.frameCount*wave.channels*sizeof(short)); |
| 825 |
|
| 826 |
|
| 827 |
drwav_read_pcm_frames_s16(&wav, wave.frameCount, (drwav_int16 *)wave.data); |
| 828 |
} |
| 829 |
else TRACELOG(LOG_WARNING, "WAVE: Failed to load WAV data"); |
| 830 |
|
| 831 |
drwav_uninit(&wav); |
| 832 |
} |
| 833 |
#endif |
| 834 |
#if SUPPORT_FILEFORMAT_OGG |
| 835 |
else if ((strcmp(fileType, ".ogg") == 0) || (strcmp(fileType, ".OGG") == 0)) |
| 836 |
{ |
| 837 |
stb_vorbis *oggData = stb_vorbis_open_memory((unsigned char *)fileData, dataSize, NULL, NULL); |
| 838 |
|
| 839 |
if (oggData != NULL) |
| 840 |
{ |
| 841 |
stb_vorbis_info info = stb_vorbis_get_info(oggData); |
| 842 |
|
| 843 |
wave.sampleRate = info.sample_rate; |
| 844 |
wave.sampleSize = 16; |
| 845 |
wave.channels = info.channels; |
| 846 |
wave.frameCount = (unsigned int)stb_vorbis_stream_length_in_samples(oggData); |
| 847 |
wave.data = (short *)RL_MALLOC(wave.frameCount*wave.channels*sizeof(short)); |
| 848 |
|
| 849 |
|
| 850 |
stb_vorbis_get_samples_short_interleaved(oggData, info.channels, (short *)wave.data, wave.frameCount*wave.channels); |
| 851 |
stb_vorbis_close(oggData); |
| 852 |
} |
| 853 |
else TRACELOG(LOG_WARNING, "WAVE: Failed to load OGG data"); |
| 854 |
} |
| 855 |
#endif |
| 856 |
#if SUPPORT_FILEFORMAT_MP3 |
| 857 |
else if ((strcmp(fileType, ".mp3") == 0) || (strcmp(fileType, ".MP3") == 0)) |
| 858 |
{ |
| 859 |
drmp3_config config = { 0 }; |
| 860 |
unsigned long long int totalFrameCount = 0; |
| 861 |
|
| 862 |
|
| 863 |
wave.data = drmp3_open_memory_and_read_pcm_frames_f32(fileData, dataSize, &config, &totalFrameCount, NULL); |
| 864 |
wave.sampleSize = 32; |
| 865 |
|
| 866 |
if (wave.data != NULL) |
| 867 |
{ |
| 868 |
wave.channels = config.channels; |
| 869 |
wave.sampleRate = config.sampleRate; |
| 870 |
wave.frameCount = (int)totalFrameCount; |
| 871 |
} |
| 872 |
else TRACELOG(LOG_WARNING, "WAVE: Failed to load MP3 data"); |
| 873 |
|
| 874 |
} |
| 875 |
#endif |
| 876 |
#if SUPPORT_FILEFORMAT_QOA |
| 877 |
else if ((strcmp(fileType, ".qoa") == 0) || (strcmp(fileType, ".QOA") == 0)) |
| 878 |
{ |
| 879 |
qoa_desc qoa = { 0 }; |
| 880 |
|
| 881 |
|
| 882 |
wave.data = qoa_decode(fileData, dataSize, &qoa); |
| 883 |
wave.sampleSize = 16; |
| 884 |
|
| 885 |
if (wave.data != NULL) |
| 886 |
{ |
| 887 |
wave.channels = qoa.channels; |
| 888 |
wave.sampleRate = qoa.samplerate; |
| 889 |
wave.frameCount = qoa.samples; |
| 890 |
} |
| 891 |
else TRACELOG(LOG_WARNING, "WAVE: Failed to load QOA data"); |
| 892 |
|
| 893 |
} |
| 894 |
#endif |
| 895 |
#if SUPPORT_FILEFORMAT_FLAC |
| 896 |
else if ((strcmp(fileType, ".flac") == 0) || (strcmp(fileType, ".FLAC") == 0)) |
| 897 |
{ |
| 898 |
unsigned long long int totalFrameCount = 0; |
| 899 |
|
| 900 |
|
| 901 |
wave.data = drflac_open_memory_and_read_pcm_frames_s16(fileData, dataSize, &wave.channels, &wave.sampleRate, &totalFrameCount, NULL); |
| 902 |
wave.sampleSize = 16; |
| 903 |
|
| 904 |
if (wave.data != NULL) wave.frameCount = (unsigned int)totalFrameCount; |
| 905 |
else TRACELOG(LOG_WARNING, "WAVE: Failed to load FLAC data"); |
| 906 |
} |
| 907 |
#endif |
| 908 |
else TRACELOG(LOG_WARNING, "WAVE: Data format not supported"); |
| 909 |
|
| 910 |
TRACELOG(LOG_INFO, "WAVE: Data loaded successfully (%i Hz, %i bit, %i channels)", wave.sampleRate, wave.sampleSize, wave.channels); |
| 911 |
|
| 912 |
return wave; |
| 913 |
} |
| 914 |
|
| 915 |
|
| 916 |
bool IsWaveValid(Wave wave) |
| 917 |
{ |
| 918 |
bool result = false; |
| 919 |
|
| 920 |
if ((wave.data != NULL) && |
| 921 |
(wave.frameCount > 0) && |
| 922 |
(wave.sampleRate > 0) && |
| 923 |
(wave.sampleSize > 0) && |
| 924 |
(wave.channels > 0)) result = true; |
| 925 |
|
| 926 |
return result; |
| 927 |
} |
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
Sound LoadSound(const char *fileName) |
| 932 |
{ |
| 933 |
Wave wave = LoadWave(fileName); |
| 934 |
|
| 935 |
Sound sound = LoadSoundFromWave(wave); |
| 936 |
|
| 937 |
UnloadWave(wave); |
| 938 |
|
| 939 |
return sound; |
| 940 |
} |
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
Sound LoadSoundFromWave(Wave wave) |
| 945 |
{ |
| 946 |
Sound sound = { 0 }; |
| 947 |
|
| 948 |
if (wave.data != NULL) |
| 949 |
{ |
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
ma_format formatIn = ((wave.sampleSize == 8)? ma_format_u8 : ((wave.sampleSize == 16)? ma_format_s16 : ma_format_f32)); |
| 960 |
ma_uint32 frameCountIn = wave.frameCount; |
| 961 |
|
| 962 |
ma_uint32 frameCount = (ma_uint32)ma_convert_frames(NULL, 0, AUDIO_DEVICE_FORMAT, AUDIO_DEVICE_CHANNELS, AUDIO.System.device.sampleRate, NULL, frameCountIn, formatIn, wave.channels, wave.sampleRate); |
| 963 |
if (frameCount == 0) TRACELOG(LOG_WARNING, "SOUND: Failed to get frame count for format conversion"); |
| 964 |
|
| 965 |
AudioBuffer *audioBuffer = LoadAudioBuffer(AUDIO_DEVICE_FORMAT, AUDIO_DEVICE_CHANNELS, AUDIO.System.device.sampleRate, frameCount, AUDIO_BUFFER_USAGE_STATIC); |
| 966 |
if (audioBuffer != NULL) |
| 967 |
{ |
| 968 |
frameCount = (ma_uint32)ma_convert_frames(audioBuffer->data, frameCount, AUDIO_DEVICE_FORMAT, AUDIO_DEVICE_CHANNELS, AUDIO.System.device.sampleRate, wave.data, frameCountIn, formatIn, wave.channels, wave.sampleRate); |
| 969 |
if (frameCount == 0) TRACELOG(LOG_WARNING, "SOUND: Failed format conversion"); |
| 970 |
|
| 971 |
sound.frameCount = frameCount; |
| 972 |
sound.stream.sampleRate = AUDIO.System.device.sampleRate; |
| 973 |
sound.stream.sampleSize = 32; |
| 974 |
sound.stream.channels = AUDIO_DEVICE_CHANNELS; |
| 975 |
sound.stream.buffer = audioBuffer; |
| 976 |
} |
| 977 |
else TRACELOG(LOG_WARNING, "SOUND: Failed to create buffer"); |
| 978 |
} |
| 979 |
|
| 980 |
return sound; |
| 981 |
} |
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
Sound LoadSoundAlias(Sound source) |
| 986 |
{ |
| 987 |
Sound sound = { 0 }; |
| 988 |
|
| 989 |
if (source.stream.buffer->data != NULL) |
| 990 |
{ |
| 991 |
AudioBuffer *audioBuffer = LoadAudioBuffer(AUDIO_DEVICE_FORMAT, AUDIO_DEVICE_CHANNELS, AUDIO.System.device.sampleRate, 0, AUDIO_BUFFER_USAGE_STATIC); |
| 992 |
|
| 993 |
if (audioBuffer != NULL) |
| 994 |
{ |
| 995 |
audioBuffer->sizeInFrames = source.stream.buffer->sizeInFrames; |
| 996 |
audioBuffer->data = source.stream.buffer->data; |
| 997 |
|
| 998 |
|
| 999 |
audioBuffer->volume = 1.0f; |
| 1000 |
audioBuffer->pitch = 1.0f; |
| 1001 |
audioBuffer->pan = 0.0f; |
| 1002 |
|
| 1003 |
sound.frameCount = source.frameCount; |
| 1004 |
sound.stream.sampleRate = AUDIO.System.device.sampleRate; |
| 1005 |
sound.stream.sampleSize = 32; |
| 1006 |
sound.stream.channels = AUDIO_DEVICE_CHANNELS; |
| 1007 |
sound.stream.buffer = audioBuffer; |
| 1008 |
} |
| 1009 |
else TRACELOG(LOG_WARNING, "SOUND: Failed to create buffer"); |
| 1010 |
} |
| 1011 |
|
| 1012 |
return sound; |
| 1013 |
} |
| 1014 |
|
| 1015 |
|
| 1016 |
bool IsSoundValid(Sound sound) |
| 1017 |
{ |
| 1018 |
bool result = false; |
| 1019 |
|
| 1020 |
if ((sound.frameCount > 0) && |
| 1021 |
(sound.stream.buffer != NULL) && |
| 1022 |
(sound.stream.sampleRate > 0) && |
| 1023 |
(sound.stream.sampleSize > 0) && |
| 1024 |
(sound.stream.channels > 0)) result = true; |
| 1025 |
|
| 1026 |
return result; |
| 1027 |
} |
| 1028 |
|
| 1029 |
|
| 1030 |
void UnloadWave(Wave wave) |
| 1031 |
{ |
| 1032 |
RL_FREE(wave.data); |
| 1033 |
|
| 1034 |
} |
| 1035 |
|
| 1036 |
|
| 1037 |
void UnloadSound(Sound sound) |
| 1038 |
{ |
| 1039 |
UnloadAudioBuffer(sound.stream.buffer); |
| 1040 |
|
| 1041 |
} |
| 1042 |
|
| 1043 |
void UnloadSoundAlias(Sound alias) |
| 1044 |
{ |
| 1045 |
|
| 1046 |
if (alias.stream.buffer != NULL) |
| 1047 |
{ |
| 1048 |
UntrackAudioBuffer(alias.stream.buffer); |
| 1049 |
ma_data_converter_uninit(&alias.stream.buffer->converter, NULL); |
| 1050 |
RL_FREE(alias.stream.buffer); |
| 1051 |
} |
| 1052 |
} |
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
void UpdateSound(Sound sound, const void *data, int frameCount) |
| 1058 |
{ |
| 1059 |
if (sound.stream.buffer != NULL) |
| 1060 |
{ |
| 1061 |
StopAudioBuffer(sound.stream.buffer); |
| 1062 |
|
| 1063 |
memcpy(sound.stream.buffer->data, data, frameCount*ma_get_bytes_per_frame(sound.stream.buffer->converter.formatIn, sound.stream.buffer->converter.channelsIn)); |
| 1064 |
} |
| 1065 |
} |
| 1066 |
|
| 1067 |
|
| 1068 |
bool ExportWave(Wave wave, const char *fileName) |
| 1069 |
{ |
| 1070 |
bool result = false; |
| 1071 |
|
| 1072 |
if (false) { } |
| 1073 |
#if SUPPORT_FILEFORMAT_WAV |
| 1074 |
else if (IsFileExtension(fileName, ".wav")) |
| 1075 |
{ |
| 1076 |
drwav wav = { 0 }; |
| 1077 |
drwav_data_format format = { 0 }; |
| 1078 |
format.container = drwav_container_riff; |
| 1079 |
if (wave.sampleSize == 32) format.format = DR_WAVE_FORMAT_IEEE_FLOAT; |
| 1080 |
else format.format = DR_WAVE_FORMAT_PCM; |
| 1081 |
format.channels = wave.channels; |
| 1082 |
format.sampleRate = wave.sampleRate; |
| 1083 |
format.bitsPerSample = wave.sampleSize; |
| 1084 |
|
| 1085 |
void *fileData = NULL; |
| 1086 |
size_t fileDataSize = 0; |
| 1087 |
result = drwav_init_memory_write(&wav, &fileData, &fileDataSize, &format, NULL); |
| 1088 |
if (result) result = (int)drwav_write_pcm_frames(&wav, wave.frameCount, wave.data); |
| 1089 |
drwav_result result = drwav_uninit(&wav); |
| 1090 |
|
| 1091 |
if (result == DRWAV_SUCCESS) result = SaveFileData(fileName, (unsigned char *)fileData, (unsigned int)fileDataSize); |
| 1092 |
|
| 1093 |
drwav_free(fileData, NULL); |
| 1094 |
} |
| 1095 |
#endif |
| 1096 |
#if SUPPORT_FILEFORMAT_QOA |
| 1097 |
else if (IsFileExtension(fileName, ".qoa")) |
| 1098 |
{ |
| 1099 |
if (wave.sampleSize == 16) |
| 1100 |
{ |
| 1101 |
qoa_desc qoa = { 0 }; |
| 1102 |
qoa.channels = wave.channels; |
| 1103 |
qoa.samplerate = wave.sampleRate; |
| 1104 |
qoa.samples = wave.frameCount; |
| 1105 |
|
| 1106 |
int bytesWritten = qoa_write(fileName, (const short *)wave.data, &qoa); |
| 1107 |
if (bytesWritten > 0) result = true; |
| 1108 |
} |
| 1109 |
else TRACELOG(LOG_WARNING, "AUDIO: Wave data must be 16 bit per sample for QOA format export"); |
| 1110 |
} |
| 1111 |
#endif |
| 1112 |
else if (IsFileExtension(fileName, ".raw")) |
| 1113 |
{ |
| 1114 |
|
| 1115 |
|
| 1116 |
result = SaveFileData(fileName, wave.data, wave.frameCount*wave.channels*wave.sampleSize/8); |
| 1117 |
} |
| 1118 |
|
| 1119 |
if (result) TRACELOG(LOG_INFO, "FILEIO: [%s] Wave data exported successfully", fileName); |
| 1120 |
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to export wave data", fileName); |
| 1121 |
|
| 1122 |
return result; |
| 1123 |
} |
| 1124 |
|
| 1125 |
|
| 1126 |
bool ExportWaveAsCode(Wave wave, const char *fileName) |
| 1127 |
{ |
| 1128 |
bool result = false; |
| 1129 |
|
| 1130 |
#ifndef TEXT_BYTES_PER_LINE |
| 1131 |
#define TEXT_BYTES_PER_LINE 20 |
| 1132 |
#endif |
| 1133 |
|
| 1134 |
int waveDataSize = wave.frameCount*wave.channels*wave.sampleSize/8; |
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
char *txtData = (char *)RL_CALLOC(waveDataSize*12 + 2000, sizeof(char)); |
| 1140 |
|
| 1141 |
int byteCount = 0; |
| 1142 |
byteCount += sprintf(txtData + byteCount, "\n//////////////////////////////////////////////////////////////////////////////////\n"); |
| 1143 |
byteCount += sprintf(txtData + byteCount, "// //\n"); |
| 1144 |
byteCount += sprintf(txtData + byteCount, "// WaveAsCode exporter v1.1 - Wave data exported as an array of bytes //\n"); |
| 1145 |
byteCount += sprintf(txtData + byteCount, "// //\n"); |
| 1146 |
byteCount += sprintf(txtData + byteCount, "// more info and bugs-report: github.com/raysan5/raylib //\n"); |
| 1147 |
byteCount += sprintf(txtData + byteCount, "// feedback and support: ray[at]raylib.com //\n"); |
| 1148 |
byteCount += sprintf(txtData + byteCount, "// //\n"); |
| 1149 |
byteCount += sprintf(txtData + byteCount, "// Copyright (c) 2018-2026 Ramon Santamaria (@raysan5) //\n"); |
| 1150 |
byteCount += sprintf(txtData + byteCount, "// //\n"); |
| 1151 |
byteCount += sprintf(txtData + byteCount, "//////////////////////////////////////////////////////////////////////////////////\n\n"); |
| 1152 |
|
| 1153 |
|
| 1154 |
char varFileName[256] = { 0 }; |
| 1155 |
strncpy(varFileName, GetFileNameWithoutExt(fileName), 256 - 1); |
| 1156 |
for (int i = 0; varFileName[i] != '\0'; i++) if (varFileName[i] >= 'a' && varFileName[i] <= 'z') { varFileName[i] = varFileName[i] - 32; } |
| 1157 |
|
| 1158 |
|
| 1159 |
byteCount += sprintf(txtData + byteCount, "// Wave data information\n"); |
| 1160 |
byteCount += sprintf(txtData + byteCount, "#define %s_FRAME_COUNT %u\n", varFileName, wave.frameCount); |
| 1161 |
byteCount += sprintf(txtData + byteCount, "#define %s_SAMPLE_RATE %u\n", varFileName, wave.sampleRate); |
| 1162 |
byteCount += sprintf(txtData + byteCount, "#define %s_SAMPLE_SIZE %u\n", varFileName, wave.sampleSize); |
| 1163 |
byteCount += sprintf(txtData + byteCount, "#define %s_CHANNELS %u\n\n", varFileName, wave.channels); |
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
if (wave.sampleSize == 32) |
| 1169 |
{ |
| 1170 |
byteCount += sprintf(txtData + byteCount, "static float %s_DATA[%i] = {\n", varFileName, waveDataSize/4); |
| 1171 |
for (int i = 1; i < waveDataSize/4; i++) byteCount += sprintf(txtData + byteCount, ((i%TEXT_BYTES_PER_LINE == 0)? "%.4ff,\n " : "%.4ff, "), ((float *)wave.data)[i - 1]); |
| 1172 |
byteCount += sprintf(txtData + byteCount, "%.4ff };\n", ((float *)wave.data)[waveDataSize/4 - 1]); |
| 1173 |
} |
| 1174 |
else |
| 1175 |
{ |
| 1176 |
byteCount += sprintf(txtData + byteCount, "static unsigned char %s_DATA[%i] = { ", varFileName, waveDataSize); |
| 1177 |
for (int i = 1; i < waveDataSize; i++) byteCount += sprintf(txtData + byteCount, ((i%TEXT_BYTES_PER_LINE == 0)? "0x%x,\n " : "0x%x, "), ((unsigned char *)wave.data)[i - 1]); |
| 1178 |
byteCount += sprintf(txtData + byteCount, "0x%x };\n", ((unsigned char *)wave.data)[waveDataSize - 1]); |
| 1179 |
} |
| 1180 |
|
| 1181 |
|
| 1182 |
result = SaveFileText(fileName, txtData); |
| 1183 |
|
| 1184 |
RL_FREE(txtData); |
| 1185 |
|
| 1186 |
if (result != 0) TRACELOG(LOG_INFO, "FILEIO: [%s] Wave as code exported successfully", fileName); |
| 1187 |
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to export wave as code", fileName); |
| 1188 |
|
| 1189 |
return result; |
| 1190 |
} |
| 1191 |
|
| 1192 |
|
| 1193 |
void PlaySound(Sound sound) |
| 1194 |
{ |
| 1195 |
PlayAudioBuffer(sound.stream.buffer); |
| 1196 |
} |
| 1197 |
|
| 1198 |
|
| 1199 |
void PauseSound(Sound sound) |
| 1200 |
{ |
| 1201 |
PauseAudioBuffer(sound.stream.buffer); |
| 1202 |
} |
| 1203 |
|
| 1204 |
|
| 1205 |
void ResumeSound(Sound sound) |
| 1206 |
{ |
| 1207 |
ResumeAudioBuffer(sound.stream.buffer); |
| 1208 |
} |
| 1209 |
|
| 1210 |
|
| 1211 |
void StopSound(Sound sound) |
| 1212 |
{ |
| 1213 |
StopAudioBuffer(sound.stream.buffer); |
| 1214 |
} |
| 1215 |
|
| 1216 |
|
| 1217 |
bool IsSoundPlaying(Sound sound) |
| 1218 |
{ |
| 1219 |
bool result = false; |
| 1220 |
|
| 1221 |
if (IsAudioBufferPlaying(sound.stream.buffer)) result = true; |
| 1222 |
|
| 1223 |
return result; |
| 1224 |
} |
| 1225 |
|
| 1226 |
|
| 1227 |
void SetSoundVolume(Sound sound, float volume) |
| 1228 |
{ |
| 1229 |
SetAudioBufferVolume(sound.stream.buffer, volume); |
| 1230 |
} |
| 1231 |
|
| 1232 |
|
| 1233 |
void SetSoundPitch(Sound sound, float pitch) |
| 1234 |
{ |
| 1235 |
SetAudioBufferPitch(sound.stream.buffer, pitch); |
| 1236 |
} |
| 1237 |
|
| 1238 |
|
| 1239 |
void SetSoundPan(Sound sound, float pan) |
| 1240 |
{ |
| 1241 |
SetAudioBufferPan(sound.stream.buffer, pan); |
| 1242 |
} |
| 1243 |
|
| 1244 |
|
| 1245 |
void WaveFormat(Wave *wave, int sampleRate, int sampleSize, int channels) |
| 1246 |
{ |
| 1247 |
ma_format formatIn = ((wave->sampleSize == 8)? ma_format_u8 : ((wave->sampleSize == 16)? ma_format_s16 : ma_format_f32)); |
| 1248 |
ma_format formatOut = ((sampleSize == 8)? ma_format_u8 : ((sampleSize == 16)? ma_format_s16 : ma_format_f32)); |
| 1249 |
|
| 1250 |
ma_uint32 frameCountIn = wave->frameCount; |
| 1251 |
ma_uint32 frameCount = (ma_uint32)ma_convert_frames(NULL, 0, formatOut, channels, sampleRate, NULL, frameCountIn, formatIn, wave->channels, wave->sampleRate); |
| 1252 |
|
| 1253 |
if (frameCount == 0) |
| 1254 |
{ |
| 1255 |
TRACELOG(LOG_WARNING, "WAVE: Failed to get frame count for format conversion"); |
| 1256 |
return; |
| 1257 |
} |
| 1258 |
|
| 1259 |
void *data = RL_MALLOC(frameCount*channels*(sampleSize/8)); |
| 1260 |
|
| 1261 |
frameCount = (ma_uint32)ma_convert_frames(data, frameCount, formatOut, channels, sampleRate, wave->data, frameCountIn, formatIn, wave->channels, wave->sampleRate); |
| 1262 |
if (frameCount == 0) |
| 1263 |
{ |
| 1264 |
RL_FREE(data); |
| 1265 |
TRACELOG(LOG_WARNING, "WAVE: Failed format conversion"); |
| 1266 |
return; |
| 1267 |
} |
| 1268 |
|
| 1269 |
wave->frameCount = frameCount; |
| 1270 |
wave->sampleSize = sampleSize; |
| 1271 |
wave->sampleRate = sampleRate; |
| 1272 |
wave->channels = channels; |
| 1273 |
|
| 1274 |
RL_FREE(wave->data); |
| 1275 |
wave->data = data; |
| 1276 |
} |
| 1277 |
|
| 1278 |
|
| 1279 |
Wave WaveCopy(Wave wave) |
| 1280 |
{ |
| 1281 |
Wave newWave = { 0 }; |
| 1282 |
|
| 1283 |
newWave.data = RL_MALLOC(wave.frameCount*wave.channels*wave.sampleSize/8); |
| 1284 |
|
| 1285 |
if (newWave.data != NULL) |
| 1286 |
{ |
| 1287 |
|
| 1288 |
memcpy(newWave.data, wave.data, wave.frameCount*wave.channels*wave.sampleSize/8); |
| 1289 |
|
| 1290 |
newWave.frameCount = wave.frameCount; |
| 1291 |
newWave.sampleRate = wave.sampleRate; |
| 1292 |
newWave.sampleSize = wave.sampleSize; |
| 1293 |
newWave.channels = wave.channels; |
| 1294 |
} |
| 1295 |
|
| 1296 |
return newWave; |
| 1297 |
} |
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
void WaveCrop(Wave *wave, int initFrame, int finalFrame) |
| 1302 |
{ |
| 1303 |
if ((initFrame >= 0) && (initFrame < finalFrame) && ((unsigned int)finalFrame <= wave->frameCount)) |
| 1304 |
{ |
| 1305 |
int frameCount = finalFrame - initFrame; |
| 1306 |
|
| 1307 |
void *data = RL_MALLOC(frameCount*wave->channels*wave->sampleSize/8); |
| 1308 |
|
| 1309 |
memcpy(data, (unsigned char *)wave->data + (initFrame*wave->channels*wave->sampleSize/8), frameCount*wave->channels*wave->sampleSize/8); |
| 1310 |
|
| 1311 |
RL_FREE(wave->data); |
| 1312 |
wave->data = data; |
| 1313 |
wave->frameCount = (unsigned int)frameCount; |
| 1314 |
} |
| 1315 |
else TRACELOG(LOG_WARNING, "WAVE: Crop range out of bounds"); |
| 1316 |
} |
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
float *LoadWaveSamples(Wave wave) |
| 1322 |
{ |
| 1323 |
float *samples = (float *)RL_MALLOC(wave.frameCount*wave.channels*sizeof(float)); |
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
for (unsigned int i = 0; i < wave.frameCount*wave.channels; i++) |
| 1328 |
{ |
| 1329 |
if (wave.sampleSize == 8) samples[i] = (float)(((unsigned char *)wave.data)[i] - 128)/128.0f; |
| 1330 |
else if (wave.sampleSize == 16) samples[i] = (float)(((short *)wave.data)[i])/32768.0f; |
| 1331 |
else if (wave.sampleSize == 32) samples[i] = ((float *)wave.data)[i]; |
| 1332 |
} |
| 1333 |
|
| 1334 |
return samples; |
| 1335 |
} |
| 1336 |
|
| 1337 |
|
| 1338 |
void UnloadWaveSamples(float *samples) |
| 1339 |
{ |
| 1340 |
RL_FREE(samples); |
| 1341 |
} |
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
Music LoadMusicStream(const char *fileName) |
| 1349 |
{ |
| 1350 |
Music music = { 0 }; |
| 1351 |
bool musicLoaded = false; |
| 1352 |
|
| 1353 |
if (false) { } |
| 1354 |
#if SUPPORT_FILEFORMAT_WAV |
| 1355 |
else if (IsFileExtension(fileName, ".wav")) |
| 1356 |
{ |
| 1357 |
drwav *ctxWav = (drwav *)RL_CALLOC(1, sizeof(drwav)); |
| 1358 |
bool success = drwav_init_file(ctxWav, fileName, NULL); |
| 1359 |
|
| 1360 |
if (success) |
| 1361 |
{ |
| 1362 |
music.ctxType = MUSIC_AUDIO_WAV; |
| 1363 |
music.ctxData = ctxWav; |
| 1364 |
int sampleSize = ctxWav->bitsPerSample; |
| 1365 |
if (ctxWav->bitsPerSample == 24) sampleSize = 16; |
| 1366 |
|
| 1367 |
music.stream = LoadAudioStream(ctxWav->sampleRate, sampleSize, ctxWav->channels); |
| 1368 |
music.frameCount = (unsigned int)ctxWav->totalPCMFrameCount; |
| 1369 |
music.looping = true; |
| 1370 |
musicLoaded = true; |
| 1371 |
} |
| 1372 |
else |
| 1373 |
{ |
| 1374 |
RL_FREE(ctxWav); |
| 1375 |
} |
| 1376 |
} |
| 1377 |
#endif |
| 1378 |
#if SUPPORT_FILEFORMAT_OGG |
| 1379 |
else if (IsFileExtension(fileName, ".ogg")) |
| 1380 |
{ |
| 1381 |
|
| 1382 |
stb_vorbis *ctxOgg = stb_vorbis_open_filename(fileName, NULL, NULL); |
| 1383 |
|
| 1384 |
if (ctxOgg != NULL) |
| 1385 |
{ |
| 1386 |
music.ctxType = MUSIC_AUDIO_OGG; |
| 1387 |
music.ctxData = ctxOgg; |
| 1388 |
stb_vorbis_info info = stb_vorbis_get_info((stb_vorbis *)music.ctxData); |
| 1389 |
|
| 1390 |
|
| 1391 |
music.stream = LoadAudioStream(info.sample_rate, 16, info.channels); |
| 1392 |
|
| 1393 |
|
| 1394 |
music.frameCount = (unsigned int)stb_vorbis_stream_length_in_samples((stb_vorbis *)music.ctxData); |
| 1395 |
music.looping = true; |
| 1396 |
musicLoaded = true; |
| 1397 |
} |
| 1398 |
else |
| 1399 |
{ |
| 1400 |
stb_vorbis_close(ctxOgg); |
| 1401 |
} |
| 1402 |
} |
| 1403 |
#endif |
| 1404 |
#if SUPPORT_FILEFORMAT_MP3 |
| 1405 |
else if (IsFileExtension(fileName, ".mp3")) |
| 1406 |
{ |
| 1407 |
drmp3 *ctxMp3 = (drmp3 *)RL_CALLOC(1, sizeof(drmp3)); |
| 1408 |
int result = drmp3_init_file(ctxMp3, fileName, NULL); |
| 1409 |
|
| 1410 |
if (result > 0) |
| 1411 |
{ |
| 1412 |
music.ctxType = MUSIC_AUDIO_MP3; |
| 1413 |
music.ctxData = ctxMp3; |
| 1414 |
music.stream = LoadAudioStream(ctxMp3->sampleRate, 32, ctxMp3->channels); |
| 1415 |
music.frameCount = (unsigned int)drmp3_get_pcm_frame_count(ctxMp3); |
| 1416 |
music.looping = true; |
| 1417 |
musicLoaded = true; |
| 1418 |
} |
| 1419 |
else |
| 1420 |
{ |
| 1421 |
RL_FREE(ctxMp3); |
| 1422 |
} |
| 1423 |
} |
| 1424 |
#endif |
| 1425 |
#if SUPPORT_FILEFORMAT_QOA |
| 1426 |
else if (IsFileExtension(fileName, ".qoa")) |
| 1427 |
{ |
| 1428 |
qoaplay_desc *ctxQoa = qoaplay_open(fileName); |
| 1429 |
|
| 1430 |
if (ctxQoa != NULL) |
| 1431 |
{ |
| 1432 |
music.ctxType = MUSIC_AUDIO_QOA; |
| 1433 |
music.ctxData = ctxQoa; |
| 1434 |
|
| 1435 |
|
| 1436 |
music.stream = LoadAudioStream(ctxQoa->info.samplerate, 32, ctxQoa->info.channels); |
| 1437 |
music.frameCount = ctxQoa->info.samples; |
| 1438 |
music.looping = true; |
| 1439 |
musicLoaded = true; |
| 1440 |
} |
| 1441 |
else{} |
| 1442 |
} |
| 1443 |
#endif |
| 1444 |
#if SUPPORT_FILEFORMAT_FLAC |
| 1445 |
else if (IsFileExtension(fileName, ".flac")) |
| 1446 |
{ |
| 1447 |
drflac *ctxFlac = drflac_open_file(fileName, NULL); |
| 1448 |
|
| 1449 |
if (ctxFlac != NULL) |
| 1450 |
{ |
| 1451 |
music.ctxType = MUSIC_AUDIO_FLAC; |
| 1452 |
music.ctxData = ctxFlac; |
| 1453 |
int sampleSize = ctxFlac->bitsPerSample; |
| 1454 |
if (ctxFlac->bitsPerSample == 24) sampleSize = 16; |
| 1455 |
music.stream = LoadAudioStream(ctxFlac->sampleRate, sampleSize, ctxFlac->channels); |
| 1456 |
music.frameCount = (unsigned int)ctxFlac->totalPCMFrameCount; |
| 1457 |
music.looping = true; |
| 1458 |
musicLoaded = true; |
| 1459 |
} |
| 1460 |
else |
| 1461 |
{ |
| 1462 |
drflac_free(ctxFlac, NULL); |
| 1463 |
} |
| 1464 |
} |
| 1465 |
#endif |
| 1466 |
#if SUPPORT_FILEFORMAT_XM |
| 1467 |
else if (IsFileExtension(fileName, ".xm")) |
| 1468 |
{ |
| 1469 |
jar_xm_context_t *ctxXm = NULL; |
| 1470 |
int result = jar_xm_create_context_from_file(&ctxXm, AUDIO.System.device.sampleRate, fileName); |
| 1471 |
|
| 1472 |
if (result == 0) |
| 1473 |
{ |
| 1474 |
music.ctxType = MUSIC_MODULE_XM; |
| 1475 |
music.ctxData = ctxXm; |
| 1476 |
jar_xm_set_max_loop_count(ctxXm, 0); |
| 1477 |
|
| 1478 |
unsigned int bits = 32; |
| 1479 |
if (AUDIO_DEVICE_FORMAT == ma_format_s16) bits = 16; |
| 1480 |
else if (AUDIO_DEVICE_FORMAT == ma_format_u8) bits = 8; |
| 1481 |
|
| 1482 |
|
| 1483 |
music.stream = LoadAudioStream(AUDIO.System.device.sampleRate, bits, AUDIO_DEVICE_CHANNELS); |
| 1484 |
music.frameCount = (unsigned int)jar_xm_get_remaining_samples(ctxXm); |
| 1485 |
music.looping = true; |
| 1486 |
jar_xm_reset(ctxXm); |
| 1487 |
musicLoaded = true; |
| 1488 |
} |
| 1489 |
else |
| 1490 |
{ |
| 1491 |
jar_xm_free_context(ctxXm); |
| 1492 |
} |
| 1493 |
} |
| 1494 |
#endif |
| 1495 |
#if SUPPORT_FILEFORMAT_MOD |
| 1496 |
else if (IsFileExtension(fileName, ".mod")) |
| 1497 |
{ |
| 1498 |
jar_mod_context_t *ctxMod = (jar_mod_context_t *)RL_CALLOC(1, sizeof(jar_mod_context_t)); |
| 1499 |
jar_mod_init(ctxMod); |
| 1500 |
int result = jar_mod_load_file(ctxMod, fileName); |
| 1501 |
|
| 1502 |
if (result > 0) |
| 1503 |
{ |
| 1504 |
music.ctxType = MUSIC_MODULE_MOD; |
| 1505 |
music.ctxData = ctxMod; |
| 1506 |
|
| 1507 |
music.stream = LoadAudioStream(AUDIO.System.device.sampleRate, 16, AUDIO_DEVICE_CHANNELS); |
| 1508 |
music.frameCount = (unsigned int)jar_mod_max_samples(ctxMod); |
| 1509 |
music.looping = true; |
| 1510 |
musicLoaded = true; |
| 1511 |
} |
| 1512 |
else |
| 1513 |
{ |
| 1514 |
jar_mod_unload(ctxMod); |
| 1515 |
RL_FREE(ctxMod); |
| 1516 |
} |
| 1517 |
} |
| 1518 |
#endif |
| 1519 |
else TRACELOG(LOG_WARNING, "STREAM: [%s] File format not supported", fileName); |
| 1520 |
|
| 1521 |
if (!musicLoaded) |
| 1522 |
{ |
| 1523 |
TRACELOG(LOG_WARNING, "FILEIO: [%s] Music file could not be opened", fileName); |
| 1524 |
} |
| 1525 |
else |
| 1526 |
{ |
| 1527 |
|
| 1528 |
TRACELOG(LOG_INFO, "FILEIO: [%s] Music file loaded successfully", fileName); |
| 1529 |
TRACELOG(LOG_INFO, " > Sample rate: %i Hz", music.stream.sampleRate); |
| 1530 |
TRACELOG(LOG_INFO, " > Sample size: %i bits", music.stream.sampleSize); |
| 1531 |
TRACELOG(LOG_INFO, " > Channels: %i (%s)", music.stream.channels, (music.stream.channels == 1)? "Mono" : (music.stream.channels == 2)? "Stereo" : "Multi"); |
| 1532 |
TRACELOG(LOG_INFO, " > Total frames: %i", music.frameCount); |
| 1533 |
} |
| 1534 |
|
| 1535 |
return music; |
| 1536 |
} |
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
Music LoadMusicStreamFromMemory(const char *fileType, const unsigned char *data, int dataSize) |
| 1541 |
{ |
| 1542 |
Music music = { 0 }; |
| 1543 |
bool musicLoaded = false; |
| 1544 |
|
| 1545 |
if (false) { } |
| 1546 |
#if SUPPORT_FILEFORMAT_WAV |
| 1547 |
else if ((strcmp(fileType, ".wav") == 0) || (strcmp(fileType, ".WAV") == 0)) |
| 1548 |
{ |
| 1549 |
drwav *ctxWav = (drwav *)RL_CALLOC(1, sizeof(drwav)); |
| 1550 |
|
| 1551 |
bool success = drwav_init_memory(ctxWav, (const void *)data, dataSize, NULL); |
| 1552 |
|
| 1553 |
if (success) |
| 1554 |
{ |
| 1555 |
music.ctxType = MUSIC_AUDIO_WAV; |
| 1556 |
music.ctxData = ctxWav; |
| 1557 |
int sampleSize = ctxWav->bitsPerSample; |
| 1558 |
if (ctxWav->bitsPerSample == 24) sampleSize = 16; |
| 1559 |
|
| 1560 |
music.stream = LoadAudioStream(ctxWav->sampleRate, sampleSize, ctxWav->channels); |
| 1561 |
music.frameCount = (unsigned int)ctxWav->totalPCMFrameCount; |
| 1562 |
music.looping = true; |
| 1563 |
musicLoaded = true; |
| 1564 |
} |
| 1565 |
else |
| 1566 |
{ |
| 1567 |
drwav_uninit(ctxWav); |
| 1568 |
RL_FREE(ctxWav); |
| 1569 |
} |
| 1570 |
} |
| 1571 |
#endif |
| 1572 |
#if SUPPORT_FILEFORMAT_OGG |
| 1573 |
else if ((strcmp(fileType, ".ogg") == 0) || (strcmp(fileType, ".OGG") == 0)) |
| 1574 |
{ |
| 1575 |
|
| 1576 |
stb_vorbis *ctxOgg = stb_vorbis_open_memory((const unsigned char *)data, dataSize, NULL, NULL); |
| 1577 |
|
| 1578 |
if (ctxOgg != NULL) |
| 1579 |
{ |
| 1580 |
music.ctxType = MUSIC_AUDIO_OGG; |
| 1581 |
music.ctxData = ctxOgg; |
| 1582 |
stb_vorbis_info info = stb_vorbis_get_info((stb_vorbis *)music.ctxData); |
| 1583 |
|
| 1584 |
|
| 1585 |
music.stream = LoadAudioStream(info.sample_rate, 16, info.channels); |
| 1586 |
|
| 1587 |
|
| 1588 |
music.frameCount = (unsigned int)stb_vorbis_stream_length_in_samples((stb_vorbis *)music.ctxData); |
| 1589 |
music.looping = true; |
| 1590 |
musicLoaded = true; |
| 1591 |
} |
| 1592 |
else |
| 1593 |
{ |
| 1594 |
stb_vorbis_close(ctxOgg); |
| 1595 |
} |
| 1596 |
} |
| 1597 |
#endif |
| 1598 |
#if SUPPORT_FILEFORMAT_MP3 |
| 1599 |
else if ((strcmp(fileType, ".mp3") == 0) || (strcmp(fileType, ".MP3") == 0)) |
| 1600 |
{ |
| 1601 |
drmp3 *ctxMp3 = (drmp3 *)RL_CALLOC(1, sizeof(drmp3)); |
| 1602 |
int success = drmp3_init_memory(ctxMp3, (const void *)data, dataSize, NULL); |
| 1603 |
|
| 1604 |
if (success) |
| 1605 |
{ |
| 1606 |
music.ctxType = MUSIC_AUDIO_MP3; |
| 1607 |
music.ctxData = ctxMp3; |
| 1608 |
music.stream = LoadAudioStream(ctxMp3->sampleRate, 32, ctxMp3->channels); |
| 1609 |
music.frameCount = (unsigned int)drmp3_get_pcm_frame_count(ctxMp3); |
| 1610 |
music.looping = true; |
| 1611 |
musicLoaded = true; |
| 1612 |
} |
| 1613 |
else |
| 1614 |
{ |
| 1615 |
drmp3_uninit(ctxMp3); |
| 1616 |
RL_FREE(ctxMp3); |
| 1617 |
} |
| 1618 |
} |
| 1619 |
#endif |
| 1620 |
#if SUPPORT_FILEFORMAT_QOA |
| 1621 |
else if ((strcmp(fileType, ".qoa") == 0) || (strcmp(fileType, ".QOA") == 0)) |
| 1622 |
{ |
| 1623 |
qoaplay_desc *ctxQoa = NULL; |
| 1624 |
if ((data != NULL) && (dataSize > 0)) |
| 1625 |
{ |
| 1626 |
ctxQoa = qoaplay_open_memory(data, dataSize); |
| 1627 |
} |
| 1628 |
|
| 1629 |
if (ctxQoa != NULL) |
| 1630 |
{ |
| 1631 |
music.ctxType = MUSIC_AUDIO_QOA; |
| 1632 |
music.ctxData = ctxQoa; |
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
music.stream = LoadAudioStream(ctxQoa->info.samplerate, 32, ctxQoa->info.channels); |
| 1637 |
music.frameCount = ctxQoa->info.samples; |
| 1638 |
music.looping = true; |
| 1639 |
musicLoaded = true; |
| 1640 |
} |
| 1641 |
else{} |
| 1642 |
} |
| 1643 |
#endif |
| 1644 |
#if SUPPORT_FILEFORMAT_FLAC |
| 1645 |
else if ((strcmp(fileType, ".flac") == 0) || (strcmp(fileType, ".FLAC") == 0)) |
| 1646 |
{ |
| 1647 |
drflac *ctxFlac = drflac_open_memory((const void *)data, dataSize, NULL); |
| 1648 |
|
| 1649 |
if (ctxFlac != NULL) |
| 1650 |
{ |
| 1651 |
music.ctxType = MUSIC_AUDIO_FLAC; |
| 1652 |
music.ctxData = ctxFlac; |
| 1653 |
int sampleSize = ctxFlac->bitsPerSample; |
| 1654 |
if (ctxFlac->bitsPerSample == 24) sampleSize = 16; |
| 1655 |
music.stream = LoadAudioStream(ctxFlac->sampleRate, sampleSize, ctxFlac->channels); |
| 1656 |
music.frameCount = (unsigned int)ctxFlac->totalPCMFrameCount; |
| 1657 |
music.looping = true; |
| 1658 |
musicLoaded = true; |
| 1659 |
} |
| 1660 |
else |
| 1661 |
{ |
| 1662 |
drflac_free(ctxFlac, NULL); |
| 1663 |
} |
| 1664 |
} |
| 1665 |
#endif |
| 1666 |
#if SUPPORT_FILEFORMAT_XM |
| 1667 |
else if ((strcmp(fileType, ".xm") == 0) || (strcmp(fileType, ".XM") == 0)) |
| 1668 |
{ |
| 1669 |
jar_xm_context_t *ctxXm = NULL; |
| 1670 |
int result = jar_xm_create_context_safe(&ctxXm, (const char *)data, dataSize, AUDIO.System.device.sampleRate); |
| 1671 |
if (result == 0) |
| 1672 |
{ |
| 1673 |
music.ctxType = MUSIC_MODULE_XM; |
| 1674 |
music.ctxData = ctxXm; |
| 1675 |
jar_xm_set_max_loop_count(ctxXm, 0); |
| 1676 |
|
| 1677 |
unsigned int bits = 32; |
| 1678 |
if (AUDIO_DEVICE_FORMAT == ma_format_s16) bits = 16; |
| 1679 |
else if (AUDIO_DEVICE_FORMAT == ma_format_u8) bits = 8; |
| 1680 |
|
| 1681 |
|
| 1682 |
music.stream = LoadAudioStream(AUDIO.System.device.sampleRate, bits, 2); |
| 1683 |
music.frameCount = (unsigned int)jar_xm_get_remaining_samples(ctxXm); |
| 1684 |
music.looping = true; |
| 1685 |
jar_xm_reset(ctxXm); |
| 1686 |
|
| 1687 |
musicLoaded = true; |
| 1688 |
} |
| 1689 |
else |
| 1690 |
{ |
| 1691 |
jar_xm_free_context(ctxXm); |
| 1692 |
} |
| 1693 |
} |
| 1694 |
#endif |
| 1695 |
#if SUPPORT_FILEFORMAT_MOD |
| 1696 |
else if ((strcmp(fileType, ".mod") == 0) || (strcmp(fileType, ".MOD") == 0)) |
| 1697 |
{ |
| 1698 |
jar_mod_context_t *ctxMod = (jar_mod_context_t *)RL_MALLOC(sizeof(jar_mod_context_t)); |
| 1699 |
int result = 0; |
| 1700 |
|
| 1701 |
jar_mod_init(ctxMod); |
| 1702 |
|
| 1703 |
|
| 1704 |
unsigned char *newData = (unsigned char *)RL_MALLOC(dataSize); |
| 1705 |
for (int i = 0; i < dataSize; i++) newData[i] = data[i]; |
| 1706 |
|
| 1707 |
|
| 1708 |
if (dataSize && (dataSize < 32*1024*1024)) |
| 1709 |
{ |
| 1710 |
ctxMod->modfilesize = dataSize; |
| 1711 |
ctxMod->modfile = newData; |
| 1712 |
if (jar_mod_load(ctxMod, (void *)ctxMod->modfile, dataSize)) result = dataSize; |
| 1713 |
} |
| 1714 |
|
| 1715 |
if (result > 0) |
| 1716 |
{ |
| 1717 |
music.ctxType = MUSIC_MODULE_MOD; |
| 1718 |
music.ctxData = ctxMod; |
| 1719 |
|
| 1720 |
|
| 1721 |
music.stream = LoadAudioStream(AUDIO.System.device.sampleRate, 16, 2); |
| 1722 |
music.frameCount = (unsigned int)jar_mod_max_samples(ctxMod); |
| 1723 |
music.looping = true; |
| 1724 |
musicLoaded = true; |
| 1725 |
} |
| 1726 |
else |
| 1727 |
{ |
| 1728 |
jar_mod_unload(ctxMod); |
| 1729 |
RL_FREE(ctxMod); |
| 1730 |
} |
| 1731 |
} |
| 1732 |
#endif |
| 1733 |
else TRACELOG(LOG_WARNING, "STREAM: Data format not supported"); |
| 1734 |
|
| 1735 |
if (!musicLoaded) |
| 1736 |
{ |
| 1737 |
TRACELOG(LOG_WARNING, "FILEIO: Music data could not be loaded"); |
| 1738 |
} |
| 1739 |
else |
| 1740 |
{ |
| 1741 |
|
| 1742 |
TRACELOG(LOG_INFO, "FILEIO: Music data loaded successfully"); |
| 1743 |
TRACELOG(LOG_INFO, " > Sample rate: %i Hz", music.stream.sampleRate); |
| 1744 |
TRACELOG(LOG_INFO, " > Sample size: %i bits", music.stream.sampleSize); |
| 1745 |
TRACELOG(LOG_INFO, " > Channels: %i (%s)", music.stream.channels, (music.stream.channels == 1)? "Mono" : (music.stream.channels == 2)? "Stereo" : "Multi"); |
| 1746 |
TRACELOG(LOG_INFO, " > Total frames: %i", music.frameCount); |
| 1747 |
} |
| 1748 |
|
| 1749 |
return music; |
| 1750 |
} |
| 1751 |
|
| 1752 |
|
| 1753 |
bool IsMusicValid(Music music) |
| 1754 |
{ |
| 1755 |
return ((music.ctxData != NULL) && |
| 1756 |
(music.frameCount > 0) && |
| 1757 |
(music.stream.sampleRate > 0) && |
| 1758 |
(music.stream.sampleSize > 0) && |
| 1759 |
(music.stream.channels > 0)); |
| 1760 |
} |
| 1761 |
|
| 1762 |
|
| 1763 |
void UnloadMusicStream(Music music) |
| 1764 |
{ |
| 1765 |
if (IsMusicStreamPlaying(music)) StopMusicStream(music); |
| 1766 |
|
| 1767 |
UnloadAudioStream(music.stream); |
| 1768 |
|
| 1769 |
if (music.ctxData != NULL) |
| 1770 |
{ |
| 1771 |
if (false) { } |
| 1772 |
#if SUPPORT_FILEFORMAT_WAV |
| 1773 |
else if (music.ctxType == MUSIC_AUDIO_WAV) drwav_uninit((drwav *)music.ctxData); |
| 1774 |
#endif |
| 1775 |
#if SUPPORT_FILEFORMAT_OGG |
| 1776 |
else if (music.ctxType == MUSIC_AUDIO_OGG) stb_vorbis_close((stb_vorbis *)music.ctxData); |
| 1777 |
#endif |
| 1778 |
#if SUPPORT_FILEFORMAT_MP3 |
| 1779 |
else if (music.ctxType == MUSIC_AUDIO_MP3) { drmp3_uninit((drmp3 *)music.ctxData); RL_FREE(music.ctxData); } |
| 1780 |
#endif |
| 1781 |
#if SUPPORT_FILEFORMAT_QOA |
| 1782 |
else if (music.ctxType == MUSIC_AUDIO_QOA) qoaplay_close((qoaplay_desc *)music.ctxData); |
| 1783 |
#endif |
| 1784 |
#if SUPPORT_FILEFORMAT_FLAC |
| 1785 |
else if (music.ctxType == MUSIC_AUDIO_FLAC) { drflac_close((drflac *)music.ctxData); drflac_free((drflac *)music.ctxData, NULL); } |
| 1786 |
#endif |
| 1787 |
#if SUPPORT_FILEFORMAT_XM |
| 1788 |
else if (music.ctxType == MUSIC_MODULE_XM) jar_xm_free_context((jar_xm_context_t *)music.ctxData); |
| 1789 |
#endif |
| 1790 |
#if SUPPORT_FILEFORMAT_MOD |
| 1791 |
else if (music.ctxType == MUSIC_MODULE_MOD) { jar_mod_unload((jar_mod_context_t *)music.ctxData); RL_FREE(music.ctxData); } |
| 1792 |
#endif |
| 1793 |
} |
| 1794 |
} |
| 1795 |
|
| 1796 |
|
| 1797 |
void PlayMusicStream(Music music) |
| 1798 |
{ |
| 1799 |
PlayAudioStream(music.stream); |
| 1800 |
} |
| 1801 |
|
| 1802 |
|
| 1803 |
void PauseMusicStream(Music music) |
| 1804 |
{ |
| 1805 |
PauseAudioStream(music.stream); |
| 1806 |
} |
| 1807 |
|
| 1808 |
|
| 1809 |
void ResumeMusicStream(Music music) |
| 1810 |
{ |
| 1811 |
ResumeAudioStream(music.stream); |
| 1812 |
} |
| 1813 |
|
| 1814 |
|
| 1815 |
void StopMusicStream(Music music) |
| 1816 |
{ |
| 1817 |
StopAudioStream(music.stream); |
| 1818 |
|
| 1819 |
switch (music.ctxType) |
| 1820 |
{ |
| 1821 |
#if SUPPORT_FILEFORMAT_WAV |
| 1822 |
case MUSIC_AUDIO_WAV: drwav_seek_to_first_pcm_frame((drwav *)music.ctxData); break; |
| 1823 |
#endif |
| 1824 |
#if SUPPORT_FILEFORMAT_OGG |
| 1825 |
case MUSIC_AUDIO_OGG: stb_vorbis_seek_start((stb_vorbis *)music.ctxData); break; |
| 1826 |
#endif |
| 1827 |
#if SUPPORT_FILEFORMAT_MP3 |
| 1828 |
case MUSIC_AUDIO_MP3: drmp3_seek_to_start_of_stream((drmp3 *)music.ctxData); break; |
| 1829 |
#endif |
| 1830 |
#if SUPPORT_FILEFORMAT_QOA |
| 1831 |
case MUSIC_AUDIO_QOA: qoaplay_rewind((qoaplay_desc *)music.ctxData); break; |
| 1832 |
#endif |
| 1833 |
#if SUPPORT_FILEFORMAT_FLAC |
| 1834 |
case MUSIC_AUDIO_FLAC: drflac__seek_to_first_frame((drflac *)music.ctxData); break; |
| 1835 |
#endif |
| 1836 |
#if SUPPORT_FILEFORMAT_XM |
| 1837 |
case MUSIC_MODULE_XM: jar_xm_reset((jar_xm_context_t *)music.ctxData); break; |
| 1838 |
#endif |
| 1839 |
#if SUPPORT_FILEFORMAT_MOD |
| 1840 |
case MUSIC_MODULE_MOD: jar_mod_seek_start((jar_mod_context_t *)music.ctxData); break; |
| 1841 |
#endif |
| 1842 |
default: break; |
| 1843 |
} |
| 1844 |
} |
| 1845 |
|
| 1846 |
|
| 1847 |
void SeekMusicStream(Music music, float position) |
| 1848 |
{ |
| 1849 |
|
| 1850 |
if ((music.ctxType == MUSIC_MODULE_XM) || (music.ctxType == MUSIC_MODULE_MOD)) return; |
| 1851 |
|
| 1852 |
unsigned int positionInFrames = (unsigned int)(position*music.stream.sampleRate); |
| 1853 |
|
| 1854 |
switch (music.ctxType) |
| 1855 |
{ |
| 1856 |
#if SUPPORT_FILEFORMAT_WAV |
| 1857 |
case MUSIC_AUDIO_WAV: drwav_seek_to_pcm_frame((drwav *)music.ctxData, positionInFrames); break; |
| 1858 |
#endif |
| 1859 |
#if SUPPORT_FILEFORMAT_OGG |
| 1860 |
case MUSIC_AUDIO_OGG: stb_vorbis_seek_frame((stb_vorbis *)music.ctxData, positionInFrames); break; |
| 1861 |
#endif |
| 1862 |
#if SUPPORT_FILEFORMAT_MP3 |
| 1863 |
case MUSIC_AUDIO_MP3: drmp3_seek_to_pcm_frame((drmp3 *)music.ctxData, positionInFrames); break; |
| 1864 |
#endif |
| 1865 |
#if SUPPORT_FILEFORMAT_QOA |
| 1866 |
case MUSIC_AUDIO_QOA: |
| 1867 |
{ |
| 1868 |
int qoaFrame = positionInFrames/QOA_FRAME_LEN; |
| 1869 |
qoaplay_seek_frame((qoaplay_desc *)music.ctxData, qoaFrame); |
| 1870 |
|
| 1871 |
|
| 1872 |
positionInFrames = ((qoaplay_desc *)music.ctxData)->sample_position; |
| 1873 |
} break; |
| 1874 |
#endif |
| 1875 |
#if SUPPORT_FILEFORMAT_FLAC |
| 1876 |
case MUSIC_AUDIO_FLAC: drflac_seek_to_pcm_frame((drflac *)music.ctxData, positionInFrames); break; |
| 1877 |
#endif |
| 1878 |
default: break; |
| 1879 |
} |
| 1880 |
|
| 1881 |
ma_mutex_lock(&AUDIO.System.lock); |
| 1882 |
music.stream.buffer->framesProcessed = positionInFrames; |
| 1883 |
music.stream.buffer->isSubBufferProcessed[0] = true; |
| 1884 |
music.stream.buffer->isSubBufferProcessed[1] = true; |
| 1885 |
ma_mutex_unlock(&AUDIO.System.lock); |
| 1886 |
} |
| 1887 |
|
| 1888 |
|
| 1889 |
void UpdateMusicStream(Music music) |
| 1890 |
{ |
| 1891 |
if (music.stream.buffer == NULL) return; |
| 1892 |
if (!music.stream.buffer->playing) return; |
| 1893 |
|
| 1894 |
ma_mutex_lock(&AUDIO.System.lock); |
| 1895 |
|
| 1896 |
unsigned int subBufferSizeInFrames = music.stream.buffer->sizeInFrames/2; |
| 1897 |
|
| 1898 |
|
| 1899 |
int frameSize = music.stream.channels*music.stream.sampleSize/8; |
| 1900 |
unsigned int pcmSize = subBufferSizeInFrames*frameSize; |
| 1901 |
|
| 1902 |
if (AUDIO.System.pcmBufferSize < pcmSize) |
| 1903 |
{ |
| 1904 |
RL_FREE(AUDIO.System.pcmBuffer); |
| 1905 |
AUDIO.System.pcmBuffer = RL_CALLOC(1, pcmSize); |
| 1906 |
AUDIO.System.pcmBufferSize = pcmSize; |
| 1907 |
} |
| 1908 |
|
| 1909 |
|
| 1910 |
for (int i = 0; i < 2; i++) |
| 1911 |
{ |
| 1912 |
unsigned int framesLeft = music.frameCount - music.stream.buffer->framesProcessed; |
| 1913 |
unsigned int framesToStream = 0; |
| 1914 |
|
| 1915 |
if ((framesLeft >= subBufferSizeInFrames) || music.looping) framesToStream = subBufferSizeInFrames; |
| 1916 |
else framesToStream = framesLeft; |
| 1917 |
|
| 1918 |
if (framesToStream == 0) |
| 1919 |
{ |
| 1920 |
|
| 1921 |
if (music.stream.buffer->isSubBufferProcessed[0] && music.stream.buffer->isSubBufferProcessed[1]) |
| 1922 |
{ |
| 1923 |
ma_mutex_unlock(&AUDIO.System.lock); |
| 1924 |
StopMusicStream(music); |
| 1925 |
return; |
| 1926 |
} |
| 1927 |
|
| 1928 |
ma_mutex_unlock(&AUDIO.System.lock); |
| 1929 |
return; |
| 1930 |
} |
| 1931 |
|
| 1932 |
if (!music.stream.buffer->isSubBufferProcessed[i]) continue; |
| 1933 |
|
| 1934 |
int frameCountStillNeeded = framesToStream; |
| 1935 |
int frameCountReadTotal = 0; |
| 1936 |
|
| 1937 |
switch (music.ctxType) |
| 1938 |
{ |
| 1939 |
#if SUPPORT_FILEFORMAT_WAV |
| 1940 |
case MUSIC_AUDIO_WAV: |
| 1941 |
{ |
| 1942 |
if (music.stream.sampleSize == 16) |
| 1943 |
{ |
| 1944 |
while (true) |
| 1945 |
{ |
| 1946 |
int frameCountRead = (int)drwav_read_pcm_frames_s16((drwav *)music.ctxData, frameCountStillNeeded, (short *)((char *)AUDIO.System.pcmBuffer + frameCountReadTotal*frameSize)); |
| 1947 |
frameCountReadTotal += frameCountRead; |
| 1948 |
frameCountStillNeeded -= frameCountRead; |
| 1949 |
if (frameCountStillNeeded == 0) break; |
| 1950 |
else drwav_seek_to_first_pcm_frame((drwav *)music.ctxData); |
| 1951 |
} |
| 1952 |
} |
| 1953 |
else if (music.stream.sampleSize == 32) |
| 1954 |
{ |
| 1955 |
while (true) |
| 1956 |
{ |
| 1957 |
int frameCountRead = (int)drwav_read_pcm_frames_f32((drwav *)music.ctxData, frameCountStillNeeded, (float *)((char *)AUDIO.System.pcmBuffer + frameCountReadTotal*frameSize)); |
| 1958 |
frameCountReadTotal += frameCountRead; |
| 1959 |
frameCountStillNeeded -= frameCountRead; |
| 1960 |
if (frameCountStillNeeded == 0) break; |
| 1961 |
else drwav_seek_to_first_pcm_frame((drwav *)music.ctxData); |
| 1962 |
} |
| 1963 |
} |
| 1964 |
} break; |
| 1965 |
#endif |
| 1966 |
#if SUPPORT_FILEFORMAT_OGG |
| 1967 |
case MUSIC_AUDIO_OGG: |
| 1968 |
{ |
| 1969 |
while (true) |
| 1970 |
{ |
| 1971 |
int frameCountRead = stb_vorbis_get_samples_short_interleaved((stb_vorbis *)music.ctxData, music.stream.channels, (short *)((char *)AUDIO.System.pcmBuffer + frameCountReadTotal*frameSize), frameCountStillNeeded*music.stream.channels); |
| 1972 |
frameCountReadTotal += frameCountRead; |
| 1973 |
frameCountStillNeeded -= frameCountRead; |
| 1974 |
if (frameCountStillNeeded == 0) break; |
| 1975 |
else stb_vorbis_seek_start((stb_vorbis *)music.ctxData); |
| 1976 |
} |
| 1977 |
} break; |
| 1978 |
#endif |
| 1979 |
#if SUPPORT_FILEFORMAT_MP3 |
| 1980 |
case MUSIC_AUDIO_MP3: |
| 1981 |
{ |
| 1982 |
while (true) |
| 1983 |
{ |
| 1984 |
int frameCountRead = (int)drmp3_read_pcm_frames_f32((drmp3 *)music.ctxData, frameCountStillNeeded, (float *)((char *)AUDIO.System.pcmBuffer + frameCountReadTotal*frameSize)); |
| 1985 |
frameCountReadTotal += frameCountRead; |
| 1986 |
frameCountStillNeeded -= frameCountRead; |
| 1987 |
if (frameCountStillNeeded == 0) break; |
| 1988 |
else drmp3_seek_to_start_of_stream((drmp3 *)music.ctxData); |
| 1989 |
} |
| 1990 |
} break; |
| 1991 |
#endif |
| 1992 |
#if SUPPORT_FILEFORMAT_QOA |
| 1993 |
case MUSIC_AUDIO_QOA: |
| 1994 |
{ |
| 1995 |
unsigned int frameCountRead = qoaplay_decode((qoaplay_desc *)music.ctxData, (float *)AUDIO.System.pcmBuffer, framesToStream); |
| 1996 |
frameCountReadTotal += frameCountRead; |
| 1997 |
|
| 1998 |
|
| 1999 |
|
| 2000 |
|
| 2001 |
|
| 2002 |
|
| 2003 |
|
| 2004 |
|
| 2005 |
|
| 2006 |
|
| 2007 |
} break; |
| 2008 |
#endif |
| 2009 |
#if SUPPORT_FILEFORMAT_FLAC |
| 2010 |
case MUSIC_AUDIO_FLAC: |
| 2011 |
{ |
| 2012 |
while (true) |
| 2013 |
{ |
| 2014 |
int frameCountRead = (int)drflac_read_pcm_frames_s16((drflac *)music.ctxData, frameCountStillNeeded, (short *)((char *)AUDIO.System.pcmBuffer + frameCountReadTotal*frameSize)); |
| 2015 |
frameCountReadTotal += frameCountRead; |
| 2016 |
frameCountStillNeeded -= frameCountRead; |
| 2017 |
if (frameCountStillNeeded == 0) break; |
| 2018 |
else drflac__seek_to_first_frame((drflac *)music.ctxData); |
| 2019 |
} |
| 2020 |
} break; |
| 2021 |
#endif |
| 2022 |
#if SUPPORT_FILEFORMAT_XM |
| 2023 |
case MUSIC_MODULE_XM: |
| 2024 |
{ |
| 2025 |
|
| 2026 |
if (AUDIO_DEVICE_FORMAT == ma_format_f32) jar_xm_generate_samples((jar_xm_context_t *)music.ctxData, (float *)AUDIO.System.pcmBuffer, framesToStream); |
| 2027 |
else if (AUDIO_DEVICE_FORMAT == ma_format_s16) jar_xm_generate_samples_16bit((jar_xm_context_t *)music.ctxData, (short *)AUDIO.System.pcmBuffer, framesToStream); |
| 2028 |
else if (AUDIO_DEVICE_FORMAT == ma_format_u8) jar_xm_generate_samples_8bit((jar_xm_context_t *)music.ctxData, (char *)AUDIO.System.pcmBuffer, framesToStream); |
| 2029 |
|
| 2030 |
|
| 2031 |
} break; |
| 2032 |
#endif |
| 2033 |
#if SUPPORT_FILEFORMAT_MOD |
| 2034 |
case MUSIC_MODULE_MOD: |
| 2035 |
{ |
| 2036 |
|
| 2037 |
jar_mod_fillbuffer((jar_mod_context_t *)music.ctxData, (short *)AUDIO.System.pcmBuffer, framesToStream, 0); |
| 2038 |
|
| 2039 |
|
| 2040 |
} break; |
| 2041 |
#endif |
| 2042 |
default: break; |
| 2043 |
} |
| 2044 |
|
| 2045 |
UpdateAudioStreamInLockedState(music.stream, AUDIO.System.pcmBuffer, framesToStream); |
| 2046 |
} |
| 2047 |
|
| 2048 |
ma_mutex_unlock(&AUDIO.System.lock); |
| 2049 |
} |
| 2050 |
|
| 2051 |
|
| 2052 |
bool IsMusicStreamPlaying(Music music) |
| 2053 |
{ |
| 2054 |
return IsAudioStreamPlaying(music.stream); |
| 2055 |
} |
| 2056 |
|
| 2057 |
|
| 2058 |
void SetMusicVolume(Music music, float volume) |
| 2059 |
{ |
| 2060 |
SetAudioStreamVolume(music.stream, volume); |
| 2061 |
} |
| 2062 |
|
| 2063 |
|
| 2064 |
void SetMusicPitch(Music music, float pitch) |
| 2065 |
{ |
| 2066 |
SetAudioBufferPitch(music.stream.buffer, pitch); |
| 2067 |
} |
| 2068 |
|
| 2069 |
|
| 2070 |
void SetMusicPan(Music music, float pan) |
| 2071 |
{ |
| 2072 |
SetAudioBufferPan(music.stream.buffer, pan); |
| 2073 |
} |
| 2074 |
|
| 2075 |
|
| 2076 |
float GetMusicTimeLength(Music music) |
| 2077 |
{ |
| 2078 |
float totalSeconds = 0.0f; |
| 2079 |
|
| 2080 |
totalSeconds = (float)music.frameCount/music.stream.sampleRate; |
| 2081 |
|
| 2082 |
return totalSeconds; |
| 2083 |
} |
| 2084 |
|
| 2085 |
|
| 2086 |
float GetMusicTimePlayed(Music music) |
| 2087 |
{ |
| 2088 |
float secondsPlayed = 0.0f; |
| 2089 |
if (music.stream.buffer != NULL) |
| 2090 |
{ |
| 2091 |
#if SUPPORT_FILEFORMAT_XM |
| 2092 |
if (music.ctxType == MUSIC_MODULE_XM) |
| 2093 |
{ |
| 2094 |
uint64_t framesPlayed = 0; |
| 2095 |
|
| 2096 |
jar_xm_get_position((jar_xm_context_t *)music.ctxData, NULL, NULL, NULL, &framesPlayed); |
| 2097 |
secondsPlayed = (float)framesPlayed/music.stream.sampleRate; |
| 2098 |
} |
| 2099 |
else |
| 2100 |
#endif |
| 2101 |
{ |
| 2102 |
ma_mutex_lock(&AUDIO.System.lock); |
| 2103 |
|
| 2104 |
int framesProcessed = (int)music.stream.buffer->framesProcessed; |
| 2105 |
int subBufferSize = (int)music.stream.buffer->sizeInFrames/2; |
| 2106 |
int framesInFirstBuffer = music.stream.buffer->isSubBufferProcessed[0]? 0 : subBufferSize; |
| 2107 |
int framesInSecondBuffer = music.stream.buffer->isSubBufferProcessed[1]? 0 : subBufferSize; |
| 2108 |
int framesInBuffers = framesInFirstBuffer + framesInSecondBuffer; |
| 2109 |
if (((unsigned int)framesInBuffers > music.frameCount) && !music.looping) framesInBuffers = music.frameCount; |
| 2110 |
int framesSentToMix = music.stream.buffer->frameCursorPos%subBufferSize; |
| 2111 |
int framesPlayed = (framesProcessed - framesInBuffers + framesSentToMix)%(int)music.frameCount; |
| 2112 |
if (framesPlayed < 0) framesPlayed += music.frameCount; |
| 2113 |
secondsPlayed = (float)framesPlayed/music.stream.sampleRate; |
| 2114 |
ma_mutex_unlock(&AUDIO.System.lock); |
| 2115 |
} |
| 2116 |
} |
| 2117 |
|
| 2118 |
return secondsPlayed; |
| 2119 |
} |
| 2120 |
|
| 2121 |
|
| 2122 |
AudioStream LoadAudioStream(unsigned int sampleRate, unsigned int sampleSize, unsigned int channels) |
| 2123 |
{ |
| 2124 |
AudioStream stream = { 0 }; |
| 2125 |
|
| 2126 |
stream.sampleRate = sampleRate; |
| 2127 |
stream.sampleSize = sampleSize; |
| 2128 |
stream.channels = channels; |
| 2129 |
|
| 2130 |
ma_format formatIn = ((stream.sampleSize == 8)? ma_format_u8 : ((stream.sampleSize == 16)? ma_format_s16 : ma_format_f32)); |
| 2131 |
|
| 2132 |
|
| 2133 |
unsigned int periodSize = AUDIO.System.device.playback.internalPeriodSizeInFrames; |
| 2134 |
|
| 2135 |
|
| 2136 |
int deviceBitsPerSample = AUDIO.System.device.playback.format; |
| 2137 |
if (deviceBitsPerSample > 4) deviceBitsPerSample = 4; |
| 2138 |
deviceBitsPerSample *= AUDIO.System.device.playback.channels; |
| 2139 |
|
| 2140 |
unsigned int subBufferSize = (AUDIO.Buffer.defaultSize == 0)? (AUDIO.System.device.sampleRate/30*deviceBitsPerSample) : AUDIO.Buffer.defaultSize; |
| 2141 |
|
| 2142 |
if (subBufferSize < periodSize) subBufferSize = periodSize; |
| 2143 |
|
| 2144 |
|
| 2145 |
stream.buffer = LoadAudioBuffer(formatIn, stream.channels, stream.sampleRate, subBufferSize*2, AUDIO_BUFFER_USAGE_STREAM); |
| 2146 |
|
| 2147 |
if (stream.buffer != NULL) |
| 2148 |
{ |
| 2149 |
stream.buffer->looping = true; |
| 2150 |
TRACELOG(LOG_INFO, "STREAM: Initialized successfully (%i Hz, %i bit, %s)", stream.sampleRate, stream.sampleSize, (stream.channels == 1)? "Mono" : "Stereo"); |
| 2151 |
} |
| 2152 |
else TRACELOG(LOG_WARNING, "STREAM: Failed to load audio buffer, stream could not be created"); |
| 2153 |
|
| 2154 |
return stream; |
| 2155 |
} |
| 2156 |
|
| 2157 |
|
| 2158 |
bool IsAudioStreamValid(AudioStream stream) |
| 2159 |
{ |
| 2160 |
return ((stream.buffer != NULL) && |
| 2161 |
(stream.sampleRate > 0) && |
| 2162 |
(stream.sampleSize > 0) && |
| 2163 |
(stream.channels > 0)); |
| 2164 |
} |
| 2165 |
|
| 2166 |
|
| 2167 |
void UnloadAudioStream(AudioStream stream) |
| 2168 |
{ |
| 2169 |
UnloadAudioBuffer(stream.buffer); |
| 2170 |
|
| 2171 |
TRACELOG(LOG_INFO, "STREAM: Unloaded audio stream data from RAM"); |
| 2172 |
} |
| 2173 |
|
| 2174 |
|
| 2175 |
|
| 2176 |
|
| 2177 |
void UpdateAudioStream(AudioStream stream, const void *data, int frameCount) |
| 2178 |
{ |
| 2179 |
ma_mutex_lock(&AUDIO.System.lock); |
| 2180 |
UpdateAudioStreamInLockedState(stream, data, frameCount); |
| 2181 |
ma_mutex_unlock(&AUDIO.System.lock); |
| 2182 |
} |
| 2183 |
|
| 2184 |
|
| 2185 |
bool IsAudioStreamProcessed(AudioStream stream) |
| 2186 |
{ |
| 2187 |
bool result = false; |
| 2188 |
|
| 2189 |
if (stream.buffer != NULL) |
| 2190 |
{ |
| 2191 |
ma_mutex_lock(&AUDIO.System.lock); |
| 2192 |
result = stream.buffer->isSubBufferProcessed[0] || stream.buffer->isSubBufferProcessed[1]; |
| 2193 |
ma_mutex_unlock(&AUDIO.System.lock); |
| 2194 |
} |
| 2195 |
|
| 2196 |
return result; |
| 2197 |
} |
| 2198 |
|
| 2199 |
|
| 2200 |
void PlayAudioStream(AudioStream stream) |
| 2201 |
{ |
| 2202 |
PlayAudioBuffer(stream.buffer); |
| 2203 |
} |
| 2204 |
|
| 2205 |
|
| 2206 |
void PauseAudioStream(AudioStream stream) |
| 2207 |
{ |
| 2208 |
PauseAudioBuffer(stream.buffer); |
| 2209 |
} |
| 2210 |
|
| 2211 |
|
| 2212 |
void ResumeAudioStream(AudioStream stream) |
| 2213 |
{ |
| 2214 |
ResumeAudioBuffer(stream.buffer); |
| 2215 |
} |
| 2216 |
|
| 2217 |
|
| 2218 |
bool IsAudioStreamPlaying(AudioStream stream) |
| 2219 |
{ |
| 2220 |
return IsAudioBufferPlaying(stream.buffer); |
| 2221 |
} |
| 2222 |
|
| 2223 |
|
| 2224 |
void StopAudioStream(AudioStream stream) |
| 2225 |
{ |
| 2226 |
StopAudioBuffer(stream.buffer); |
| 2227 |
} |
| 2228 |
|
| 2229 |
|
| 2230 |
void SetAudioStreamVolume(AudioStream stream, float volume) |
| 2231 |
{ |
| 2232 |
SetAudioBufferVolume(stream.buffer, volume); |
| 2233 |
} |
| 2234 |
|
| 2235 |
|
| 2236 |
void SetAudioStreamPitch(AudioStream stream, float pitch) |
| 2237 |
{ |
| 2238 |
SetAudioBufferPitch(stream.buffer, pitch); |
| 2239 |
} |
| 2240 |
|
| 2241 |
|
| 2242 |
void SetAudioStreamPan(AudioStream stream, float pan) |
| 2243 |
{ |
| 2244 |
SetAudioBufferPan(stream.buffer, pan); |
| 2245 |
} |
| 2246 |
|
| 2247 |
|
| 2248 |
void SetAudioStreamBufferSizeDefault(int size) |
| 2249 |
{ |
| 2250 |
AUDIO.Buffer.defaultSize = size; |
| 2251 |
} |
| 2252 |
|
| 2253 |
|
| 2254 |
void SetAudioStreamCallback(AudioStream stream, AudioCallback callback) |
| 2255 |
{ |
| 2256 |
if (stream.buffer != NULL) |
| 2257 |
{ |
| 2258 |
ma_mutex_lock(&AUDIO.System.lock); |
| 2259 |
stream.buffer->callback = callback; |
| 2260 |
ma_mutex_unlock(&AUDIO.System.lock); |
| 2261 |
} |
| 2262 |
} |
| 2263 |
|
| 2264 |
|
| 2265 |
|
| 2266 |
|
| 2267 |
void AttachAudioStreamProcessor(AudioStream stream, AudioCallback process) |
| 2268 |
{ |
| 2269 |
ma_mutex_lock(&AUDIO.System.lock); |
| 2270 |
|
| 2271 |
rAudioProcessor *processor = (rAudioProcessor *)RL_CALLOC(1, sizeof(rAudioProcessor)); |
| 2272 |
processor->process = process; |
| 2273 |
|
| 2274 |
rAudioProcessor *last = stream.buffer->processor; |
| 2275 |
|
| 2276 |
while (last && last->next) |
| 2277 |
{ |
| 2278 |
last = last->next; |
| 2279 |
} |
| 2280 |
if (last) |
| 2281 |
{ |
| 2282 |
processor->prev = last; |
| 2283 |
last->next = processor; |
| 2284 |
} |
| 2285 |
else stream.buffer->processor = processor; |
| 2286 |
|
| 2287 |
ma_mutex_unlock(&AUDIO.System.lock); |
| 2288 |
} |
| 2289 |
|
| 2290 |
|
| 2291 |
void DetachAudioStreamProcessor(AudioStream stream, AudioCallback process) |
| 2292 |
{ |
| 2293 |
ma_mutex_lock(&AUDIO.System.lock); |
| 2294 |
|
| 2295 |
rAudioProcessor *processor = stream.buffer->processor; |
| 2296 |
|
| 2297 |
while (processor) |
| 2298 |
{ |
| 2299 |
rAudioProcessor *next = processor->next; |
| 2300 |
rAudioProcessor *prev = processor->prev; |
| 2301 |
|
| 2302 |
if (processor->process == process) |
| 2303 |
{ |
| 2304 |
if (stream.buffer->processor == processor) stream.buffer->processor = next; |
| 2305 |
if (prev) prev->next = next; |
| 2306 |
if (next) next->prev = prev; |
| 2307 |
|
| 2308 |
RL_FREE(processor); |
| 2309 |
} |
| 2310 |
|
| 2311 |
processor = next; |
| 2312 |
} |
| 2313 |
|
| 2314 |
ma_mutex_unlock(&AUDIO.System.lock); |
| 2315 |
} |
| 2316 |
|
| 2317 |
|
| 2318 |
|
| 2319 |
|
| 2320 |
void AttachAudioMixedProcessor(AudioCallback process) |
| 2321 |
{ |
| 2322 |
ma_mutex_lock(&AUDIO.System.lock); |
| 2323 |
|
| 2324 |
rAudioProcessor *processor = (rAudioProcessor *)RL_CALLOC(1, sizeof(rAudioProcessor)); |
| 2325 |
processor->process = process; |
| 2326 |
|
| 2327 |
rAudioProcessor *last = AUDIO.mixedProcessor; |
| 2328 |
|
| 2329 |
while (last && last->next) |
| 2330 |
{ |
| 2331 |
last = last->next; |
| 2332 |
} |
| 2333 |
if (last) |
| 2334 |
{ |
| 2335 |
processor->prev = last; |
| 2336 |
last->next = processor; |
| 2337 |
} |
| 2338 |
else AUDIO.mixedProcessor = processor; |
| 2339 |
|
| 2340 |
ma_mutex_unlock(&AUDIO.System.lock); |
| 2341 |
} |
| 2342 |
|
| 2343 |
|
| 2344 |
void DetachAudioMixedProcessor(AudioCallback process) |
| 2345 |
{ |
| 2346 |
ma_mutex_lock(&AUDIO.System.lock); |
| 2347 |
|
| 2348 |
rAudioProcessor *processor = AUDIO.mixedProcessor; |
| 2349 |
|
| 2350 |
while (processor) |
| 2351 |
{ |
| 2352 |
rAudioProcessor *next = processor->next; |
| 2353 |
rAudioProcessor *prev = processor->prev; |
| 2354 |
|
| 2355 |
if (processor->process == process) |
| 2356 |
{ |
| 2357 |
if (AUDIO.mixedProcessor == processor) AUDIO.mixedProcessor = next; |
| 2358 |
if (prev) prev->next = next; |
| 2359 |
if (next) next->prev = prev; |
| 2360 |
|
| 2361 |
RL_FREE(processor); |
| 2362 |
} |
| 2363 |
|
| 2364 |
processor = next; |
| 2365 |
} |
| 2366 |
|
| 2367 |
ma_mutex_unlock(&AUDIO.System.lock); |
| 2368 |
} |
| 2369 |
|
| 2370 |
|
| 2371 |
|
| 2372 |
|
| 2373 |
|
| 2374 |
static void OnLog(void *pUserData, ma_uint32 level, const char *pMessage) |
| 2375 |
{ |
| 2376 |
TRACELOG(LOG_WARNING, "miniaudio: %s", pMessage); |
| 2377 |
} |
| 2378 |
|
| 2379 |
|
| 2380 |
static ma_uint32 ReadAudioBufferFramesInInternalFormat(AudioBuffer *audioBuffer, void *framesOut, ma_uint32 frameCount) |
| 2381 |
{ |
| 2382 |
|
| 2383 |
if (!audioBuffer->playing) return 0; |
| 2384 |
|
| 2385 |
|
| 2386 |
if (audioBuffer->callback) |
| 2387 |
{ |
| 2388 |
audioBuffer->callback(framesOut, frameCount); |
| 2389 |
audioBuffer->framesProcessed += frameCount; |
| 2390 |
|
| 2391 |
return frameCount; |
| 2392 |
} |
| 2393 |
|
| 2394 |
ma_uint32 subBufferSizeInFrames = (audioBuffer->sizeInFrames > 1)? audioBuffer->sizeInFrames/2 : audioBuffer->sizeInFrames; |
| 2395 |
ma_uint32 currentSubBufferIndex = audioBuffer->frameCursorPos/subBufferSizeInFrames; |
| 2396 |
|
| 2397 |
if (currentSubBufferIndex > 1) return 0; |
| 2398 |
|
| 2399 |
|
| 2400 |
|
| 2401 |
bool isSubBufferProcessed[2] = { 0 }; |
| 2402 |
isSubBufferProcessed[0] = audioBuffer->isSubBufferProcessed[0]; |
| 2403 |
isSubBufferProcessed[1] = audioBuffer->isSubBufferProcessed[1]; |
| 2404 |
|
| 2405 |
ma_uint32 frameSizeInBytes = ma_get_bytes_per_frame(audioBuffer->converter.formatIn, audioBuffer->converter.channelsIn); |
| 2406 |
|
| 2407 |
|
| 2408 |
ma_uint32 framesRead = 0; |
| 2409 |
while (1) |
| 2410 |
{ |
| 2411 |
|
| 2412 |
|
| 2413 |
|
| 2414 |
|
| 2415 |
if (audioBuffer->usage == AUDIO_BUFFER_USAGE_STATIC) |
| 2416 |
{ |
| 2417 |
if (framesRead >= frameCount) break; |
| 2418 |
} |
| 2419 |
else |
| 2420 |
{ |
| 2421 |
if (isSubBufferProcessed[currentSubBufferIndex]) break; |
| 2422 |
} |
| 2423 |
|
| 2424 |
ma_uint32 totalFramesRemaining = (frameCount - framesRead); |
| 2425 |
if (totalFramesRemaining == 0) break; |
| 2426 |
|
| 2427 |
ma_uint32 framesRemainingInOutputBuffer; |
| 2428 |
if (audioBuffer->usage == AUDIO_BUFFER_USAGE_STATIC) |
| 2429 |
{ |
| 2430 |
framesRemainingInOutputBuffer = audioBuffer->sizeInFrames - audioBuffer->frameCursorPos; |
| 2431 |
} |
| 2432 |
else |
| 2433 |
{ |
| 2434 |
ma_uint32 firstFrameIndexOfThisSubBuffer = subBufferSizeInFrames*currentSubBufferIndex; |
| 2435 |
framesRemainingInOutputBuffer = subBufferSizeInFrames - (audioBuffer->frameCursorPos - firstFrameIndexOfThisSubBuffer); |
| 2436 |
} |
| 2437 |
|
| 2438 |
ma_uint32 framesToRead = totalFramesRemaining; |
| 2439 |
if (framesToRead > framesRemainingInOutputBuffer) framesToRead = framesRemainingInOutputBuffer; |
| 2440 |
|
| 2441 |
memcpy((unsigned char *)framesOut + (framesRead*frameSizeInBytes), audioBuffer->data + (audioBuffer->frameCursorPos*frameSizeInBytes), framesToRead*frameSizeInBytes); |
| 2442 |
audioBuffer->frameCursorPos = (audioBuffer->frameCursorPos + framesToRead)%audioBuffer->sizeInFrames; |
| 2443 |
framesRead += framesToRead; |
| 2444 |
|
| 2445 |
|
| 2446 |
if (framesToRead == framesRemainingInOutputBuffer) |
| 2447 |
{ |
| 2448 |
audioBuffer->isSubBufferProcessed[currentSubBufferIndex] = true; |
| 2449 |
isSubBufferProcessed[currentSubBufferIndex] = true; |
| 2450 |
|
| 2451 |
currentSubBufferIndex = (currentSubBufferIndex + 1)%2; |
| 2452 |
|
| 2453 |
|
| 2454 |
if (!audioBuffer->looping) |
| 2455 |
{ |
| 2456 |
StopAudioBufferInLockedState(audioBuffer); |
| 2457 |
break; |
| 2458 |
} |
| 2459 |
} |
| 2460 |
} |
| 2461 |
|
| 2462 |
|
| 2463 |
ma_uint32 totalFramesRemaining = (frameCount - framesRead); |
| 2464 |
if (totalFramesRemaining > 0) |
| 2465 |
{ |
| 2466 |
memset((unsigned char *)framesOut + (framesRead*frameSizeInBytes), 0, totalFramesRemaining*frameSizeInBytes); |
| 2467 |
|
| 2468 |
|
| 2469 |
|
| 2470 |
|
| 2471 |
if (audioBuffer->usage != AUDIO_BUFFER_USAGE_STATIC) framesRead += totalFramesRemaining; |
| 2472 |
} |
| 2473 |
|
| 2474 |
return framesRead; |
| 2475 |
} |
| 2476 |
|
| 2477 |
|
| 2478 |
static ma_uint32 ReadAudioBufferFramesInMixingFormat(AudioBuffer *audioBuffer, float *framesOut, ma_uint32 frameCount) |
| 2479 |
{ |
| 2480 |
|
| 2481 |
|
| 2482 |
|
| 2483 |
ma_uint32 bpf = ma_get_bytes_per_frame(audioBuffer->converter.formatIn, audioBuffer->converter.channelsIn); |
| 2484 |
ma_uint8 inputBuffer[4096] = { 0 }; |
| 2485 |
ma_uint32 inputBufferFrameCap = sizeof(inputBuffer)/bpf; |
| 2486 |
|
| 2487 |
ma_uint32 totalOutputFramesProcessed = 0; |
| 2488 |
while (totalOutputFramesProcessed < frameCount) |
| 2489 |
{ |
| 2490 |
float *runningFramesOut = framesOut + (totalOutputFramesProcessed*audioBuffer->converter.channelsOut); |
| 2491 |
ma_uint64 outputFramesToProcessThisIteration = frameCount - totalOutputFramesProcessed; |
| 2492 |
|
| 2493 |
|
| 2494 |
|
| 2495 |
if (audioBuffer->converterResidualCount > 0) |
| 2496 |
{ |
| 2497 |
ma_uint64 inputFramesProcessedThisIteration = audioBuffer->converterResidualCount; |
| 2498 |
ma_uint64 outputFramesProcessedThisIteration = outputFramesToProcessThisIteration; |
| 2499 |
ma_data_converter_process_pcm_frames(&audioBuffer->converter, audioBuffer->converterResidual, &inputFramesProcessedThisIteration, runningFramesOut, &outputFramesProcessedThisIteration); |
| 2500 |
|
| 2501 |
|
| 2502 |
memmove(audioBuffer->converterResidual, audioBuffer->converterResidual + inputFramesProcessedThisIteration*bpf, (size_t)(AUDIO_BUFFER_RESIDUAL_CAPACITY - inputFramesProcessedThisIteration)*bpf); |
| 2503 |
audioBuffer->converterResidualCount -= (ma_uint32)inputFramesProcessedThisIteration; |
| 2504 |
|
| 2505 |
totalOutputFramesProcessed += (ma_uint32)outputFramesProcessedThisIteration; |
| 2506 |
} |
| 2507 |
else |
| 2508 |
{ |
| 2509 |
|
| 2510 |
|
| 2511 |
|
| 2512 |
|
| 2513 |
|
| 2514 |
|
| 2515 |
|
| 2516 |
|
| 2517 |
|
| 2518 |
|
| 2519 |
|
| 2520 |
ma_uint32 estimatedInputFrameCount = (ma_uint32)(((float)audioBuffer->converter.resampler.sampleRateIn / audioBuffer->converter.resampler.sampleRateOut)*outputFramesToProcessThisIteration); |
| 2521 |
if (estimatedInputFrameCount == 0) estimatedInputFrameCount = 1; |
| 2522 |
if (estimatedInputFrameCount > inputBufferFrameCap) estimatedInputFrameCount = inputBufferFrameCap; |
| 2523 |
|
| 2524 |
ma_uint32 inputFramesInInternalFormatCount = ReadAudioBufferFramesInInternalFormat(audioBuffer, inputBuffer, estimatedInputFrameCount); |
| 2525 |
|
| 2526 |
ma_uint64 inputFramesProcessedThisIteration = inputFramesInInternalFormatCount; |
| 2527 |
ma_uint64 outputFramesProcessedThisIteration = outputFramesToProcessThisIteration; |
| 2528 |
ma_data_converter_process_pcm_frames(&audioBuffer->converter, inputBuffer, &inputFramesProcessedThisIteration, runningFramesOut, &outputFramesProcessedThisIteration); |
| 2529 |
|
| 2530 |
totalOutputFramesProcessed += (ma_uint32)outputFramesProcessedThisIteration; |
| 2531 |
|
| 2532 |
if (inputFramesInInternalFormatCount > inputFramesProcessedThisIteration) |
| 2533 |
{ |
| 2534 |
|
| 2535 |
|
| 2536 |
ma_uint64 residualFrameCount = inputFramesInInternalFormatCount - inputFramesProcessedThisIteration; |
| 2537 |
|
| 2538 |
|
| 2539 |
if (residualFrameCount > AUDIO_BUFFER_RESIDUAL_CAPACITY) |
| 2540 |
{ |
| 2541 |
residualFrameCount = AUDIO_BUFFER_RESIDUAL_CAPACITY; |
| 2542 |
} |
| 2543 |
|
| 2544 |
memcpy(audioBuffer->converterResidual, inputBuffer + inputFramesProcessedThisIteration*bpf, (size_t)(residualFrameCount*bpf)); |
| 2545 |
audioBuffer->converterResidualCount = (unsigned int)residualFrameCount; |
| 2546 |
} |
| 2547 |
|
| 2548 |
if (inputFramesInInternalFormatCount < estimatedInputFrameCount) break; |
| 2549 |
} |
| 2550 |
} |
| 2551 |
|
| 2552 |
return totalOutputFramesProcessed; |
| 2553 |
} |
| 2554 |
|
| 2555 |
|
| 2556 |
|
| 2557 |
|
| 2558 |
static void OnSendAudioDataToDevice(ma_device *pDevice, void *pFramesOut, const void *pFramesInput, ma_uint32 frameCount) |
| 2559 |
{ |
| 2560 |
(void)pDevice; |
| 2561 |
|
| 2562 |
|
| 2563 |
memset(pFramesOut, 0, frameCount*pDevice->playback.channels*ma_get_bytes_per_sample(pDevice->playback.format)); |
| 2564 |
|
| 2565 |
|
| 2566 |
|
| 2567 |
ma_mutex_lock(&AUDIO.System.lock); |
| 2568 |
{ |
| 2569 |
for (AudioBuffer *audioBuffer = AUDIO.Buffer.first; audioBuffer != NULL; audioBuffer = audioBuffer->next) |
| 2570 |
{ |
| 2571 |
|
| 2572 |
if (!audioBuffer->playing || audioBuffer->paused) continue; |
| 2573 |
|
| 2574 |
ma_uint32 framesRead = 0; |
| 2575 |
|
| 2576 |
while (1) |
| 2577 |
{ |
| 2578 |
if (framesRead >= frameCount) break; |
| 2579 |
|
| 2580 |
|
| 2581 |
ma_uint32 framesToRead = (frameCount - framesRead); |
| 2582 |
|
| 2583 |
while (framesToRead > 0) |
| 2584 |
{ |
| 2585 |
float tempBuffer[1024] = { 0 }; |
| 2586 |
|
| 2587 |
ma_uint32 framesToReadRightNow = framesToRead; |
| 2588 |
if (framesToReadRightNow > sizeof(tempBuffer)/sizeof(tempBuffer[0])/AUDIO_DEVICE_CHANNELS) |
| 2589 |
{ |
| 2590 |
framesToReadRightNow = sizeof(tempBuffer)/sizeof(tempBuffer[0])/AUDIO_DEVICE_CHANNELS; |
| 2591 |
} |
| 2592 |
|
| 2593 |
ma_uint32 framesJustRead = ReadAudioBufferFramesInMixingFormat(audioBuffer, tempBuffer, framesToReadRightNow); |
| 2594 |
if (framesJustRead > 0) |
| 2595 |
{ |
| 2596 |
float *framesOut = (float *)pFramesOut + (framesRead*AUDIO.System.device.playback.channels); |
| 2597 |
float *framesIn = tempBuffer; |
| 2598 |
|
| 2599 |
|
| 2600 |
rAudioProcessor *processor = audioBuffer->processor; |
| 2601 |
while (processor) |
| 2602 |
{ |
| 2603 |
processor->process(framesIn, framesJustRead); |
| 2604 |
processor = processor->next; |
| 2605 |
} |
| 2606 |
|
| 2607 |
MixAudioFrames(framesOut, framesIn, framesJustRead, audioBuffer); |
| 2608 |
|
| 2609 |
framesToRead -= framesJustRead; |
| 2610 |
framesRead += framesJustRead; |
| 2611 |
} |
| 2612 |
|
| 2613 |
if (!audioBuffer->playing) |
| 2614 |
{ |
| 2615 |
framesRead = frameCount; |
| 2616 |
break; |
| 2617 |
} |
| 2618 |
|
| 2619 |
|
| 2620 |
if (framesJustRead < framesToReadRightNow) |
| 2621 |
{ |
| 2622 |
if (!audioBuffer->looping) |
| 2623 |
{ |
| 2624 |
StopAudioBufferInLockedState(audioBuffer); |
| 2625 |
break; |
| 2626 |
} |
| 2627 |
else |
| 2628 |
{ |
| 2629 |
|
| 2630 |
|
| 2631 |
audioBuffer->frameCursorPos = 0; |
| 2632 |
continue; |
| 2633 |
} |
| 2634 |
} |
| 2635 |
} |
| 2636 |
|
| 2637 |
|
| 2638 |
|
| 2639 |
if (framesToRead > 0) break; |
| 2640 |
} |
| 2641 |
} |
| 2642 |
} |
| 2643 |
|
| 2644 |
rAudioProcessor *processor = AUDIO.mixedProcessor; |
| 2645 |
while (processor) |
| 2646 |
{ |
| 2647 |
processor->process(pFramesOut, frameCount); |
| 2648 |
processor = processor->next; |
| 2649 |
} |
| 2650 |
|
| 2651 |
ma_mutex_unlock(&AUDIO.System.lock); |
| 2652 |
} |
| 2653 |
|
| 2654 |
|
| 2655 |
|
| 2656 |
static void MixAudioFrames(float *framesOut, const float *framesIn, ma_uint32 frameCount, AudioBuffer *buffer) |
| 2657 |
{ |
| 2658 |
const float localVolume = buffer->volume; |
| 2659 |
const ma_uint32 channels = AUDIO.System.device.playback.channels; |
| 2660 |
|
| 2661 |
if (channels == 2) |
| 2662 |
{ |
| 2663 |
const float right = (buffer->pan + 1.0f)/2.0f; |
| 2664 |
const float left = 1.0f - right; |
| 2665 |
|
| 2666 |
|
| 2667 |
const float levels[2] = { localVolume*0.5f*left*(3.0f - left*left), localVolume*0.5f*right*(3.0f - right*right) }; |
| 2668 |
|
| 2669 |
float *frameOut = framesOut; |
| 2670 |
const float *frameIn = framesIn; |
| 2671 |
|
| 2672 |
for (ma_uint32 frame = 0; frame < frameCount; frame++) |
| 2673 |
{ |
| 2674 |
frameOut[0] += (frameIn[0]*levels[0]); |
| 2675 |
frameOut[1] += (frameIn[1]*levels[1]); |
| 2676 |
|
| 2677 |
frameOut += 2; |
| 2678 |
frameIn += 2; |
| 2679 |
} |
| 2680 |
} |
| 2681 |
else |
| 2682 |
{ |
| 2683 |
for (ma_uint32 frame = 0; frame < frameCount; frame++) |
| 2684 |
{ |
| 2685 |
for (ma_uint32 c = 0; c < channels; c++) |
| 2686 |
{ |
| 2687 |
float *frameOut = framesOut + (frame*channels); |
| 2688 |
const float *frameIn = framesIn + (frame*channels); |
| 2689 |
|
| 2690 |
|
| 2691 |
frameOut[c] += (frameIn[c]*localVolume); |
| 2692 |
} |
| 2693 |
} |
| 2694 |
} |
| 2695 |
} |
| 2696 |
|
| 2697 |
|
| 2698 |
static bool IsAudioBufferPlayingInLockedState(AudioBuffer *buffer) |
| 2699 |
{ |
| 2700 |
bool result = false; |
| 2701 |
|
| 2702 |
if (buffer != NULL) result = (buffer->playing && !buffer->paused); |
| 2703 |
|
| 2704 |
return result; |
| 2705 |
} |
| 2706 |
|
| 2707 |
|
| 2708 |
static void StopAudioBufferInLockedState(AudioBuffer *buffer) |
| 2709 |
{ |
| 2710 |
if (buffer != NULL) |
| 2711 |
{ |
| 2712 |
if (IsAudioBufferPlayingInLockedState(buffer)) |
| 2713 |
{ |
| 2714 |
buffer->playing = false; |
| 2715 |
buffer->paused = false; |
| 2716 |
buffer->frameCursorPos = 0; |
| 2717 |
buffer->framesProcessed = 0; |
| 2718 |
buffer->isSubBufferProcessed[0] = true; |
| 2719 |
buffer->isSubBufferProcessed[1] = true; |
| 2720 |
} |
| 2721 |
} |
| 2722 |
} |
| 2723 |
|
| 2724 |
|
| 2725 |
static void UpdateAudioStreamInLockedState(AudioStream stream, const void *data, int frameCount) |
| 2726 |
{ |
| 2727 |
if (stream.buffer != NULL) |
| 2728 |
{ |
| 2729 |
if (stream.buffer->isSubBufferProcessed[0] || stream.buffer->isSubBufferProcessed[1]) |
| 2730 |
{ |
| 2731 |
ma_uint32 subBufferToUpdate = 0; |
| 2732 |
|
| 2733 |
if (stream.buffer->isSubBufferProcessed[0] && stream.buffer->isSubBufferProcessed[1]) |
| 2734 |
{ |
| 2735 |
|
| 2736 |
|
| 2737 |
subBufferToUpdate = 0; |
| 2738 |
stream.buffer->frameCursorPos = 0; |
| 2739 |
} |
| 2740 |
else |
| 2741 |
{ |
| 2742 |
|
| 2743 |
subBufferToUpdate = (stream.buffer->isSubBufferProcessed[0])? 0 : 1; |
| 2744 |
} |
| 2745 |
|
| 2746 |
ma_uint32 subBufferSizeInFrames = stream.buffer->sizeInFrames/2; |
| 2747 |
unsigned char *subBuffer = stream.buffer->data + ((subBufferSizeInFrames*stream.channels*(stream.sampleSize/8))*subBufferToUpdate); |
| 2748 |
|
| 2749 |
stream.buffer->framesProcessed += frameCount; |
| 2750 |
|
| 2751 |
|
| 2752 |
|
| 2753 |
if (subBufferSizeInFrames >= (ma_uint32)frameCount) |
| 2754 |
{ |
| 2755 |
ma_uint32 framesToWrite = (ma_uint32)frameCount; |
| 2756 |
|
| 2757 |
ma_uint32 bytesToWrite = framesToWrite*stream.channels*(stream.sampleSize/8); |
| 2758 |
memcpy(subBuffer, data, bytesToWrite); |
| 2759 |
|
| 2760 |
|
| 2761 |
ma_uint32 leftoverFrameCount = subBufferSizeInFrames - framesToWrite; |
| 2762 |
|
| 2763 |
if (leftoverFrameCount > 0) memset(subBuffer + bytesToWrite, 0, leftoverFrameCount*stream.channels*(stream.sampleSize/8)); |
| 2764 |
|
| 2765 |
stream.buffer->isSubBufferProcessed[subBufferToUpdate] = false; |
| 2766 |
} |
| 2767 |
else TRACELOG(LOG_WARNING, "STREAM: Attempting to write too many frames to buffer"); |
| 2768 |
} |
| 2769 |
else TRACELOG(LOG_WARNING, "STREAM: Buffer not available for updating"); |
| 2770 |
} |
| 2771 |
} |
| 2772 |
|
| 2773 |
|
| 2774 |
#if defined(RAUDIO_STANDALONE) |
| 2775 |
|
| 2776 |
static bool IsFileExtension(const char *fileName, const char *ext) |
| 2777 |
{ |
| 2778 |
bool result = false; |
| 2779 |
const char *fileExt; |
| 2780 |
|
| 2781 |
if ((fileExt = strrchr(fileName, '.')) != NULL) |
| 2782 |
{ |
| 2783 |
if (strcmp(fileExt, ext) == 0) result = true; |
| 2784 |
} |
| 2785 |
|
| 2786 |
return result; |
| 2787 |
} |
| 2788 |
|
| 2789 |
|
| 2790 |
static const char *GetFileExtension(const char *fileName) |
| 2791 |
{ |
| 2792 |
const char *dot = strrchr(fileName, '.'); |
| 2793 |
|
| 2794 |
if (!dot || dot == fileName) return NULL; |
| 2795 |
|
| 2796 |
return dot; |
| 2797 |
} |
| 2798 |
|
| 2799 |
|
| 2800 |
static const char *strprbrk(const char *text, const char *charset) |
| 2801 |
{ |
| 2802 |
const char *latestMatch = NULL; |
| 2803 |
|
| 2804 |
for (; (text != NULL) && (text = strpbrk(text, charset)); latestMatch = text++) { } |
| 2805 |
|
| 2806 |
return latestMatch; |
| 2807 |
} |
| 2808 |
|
| 2809 |
|
| 2810 |
static const char *GetFileName(const char *filePath) |
| 2811 |
{ |
| 2812 |
const char *fileName = NULL; |
| 2813 |
if (filePath != NULL) fileName = strprbrk(filePath, "\\/"); |
| 2814 |
|
| 2815 |
if (!fileName) return filePath; |
| 2816 |
|
| 2817 |
return fileName + 1; |
| 2818 |
} |
| 2819 |
|
| 2820 |
|
| 2821 |
static const char *GetFileNameWithoutExt(const char *filePath) |
| 2822 |
{ |
| 2823 |
#define MAX_FILENAMEWITHOUTEXT_LENGTH 256 |
| 2824 |
|
| 2825 |
static char fileName[MAX_FILENAMEWITHOUTEXT_LENGTH] = { 0 }; |
| 2826 |
memset(fileName, 0, MAX_FILENAMEWITHOUTEXT_LENGTH); |
| 2827 |
|
| 2828 |
if (filePath != NULL) strncpy(fileName, GetFileName(filePath), MAX_FILENAMEWITHOUTEXT_LENGTH - 1); |
| 2829 |
|
| 2830 |
int fileNameLength = (int)strlen(fileName); |
| 2831 |
|
| 2832 |
for (int i = 0; (i < fileNameLength) && (i < MAX_FILENAMEWITHOUTEXT_LENGTH); i++) |
| 2833 |
{ |
| 2834 |
if (fileName[i] == '.') |
| 2835 |
{ |
| 2836 |
|
| 2837 |
fileName[i] = '\0'; |
| 2838 |
break; |
| 2839 |
} |
| 2840 |
} |
| 2841 |
|
| 2842 |
return fileName; |
| 2843 |
} |
| 2844 |
|
| 2845 |
|
| 2846 |
static unsigned char *LoadFileData(const char *fileName, int *dataSize) |
| 2847 |
{ |
| 2848 |
unsigned char *data = NULL; |
| 2849 |
*dataSize = 0; |
| 2850 |
|
| 2851 |
if (fileName != NULL) |
| 2852 |
{ |
| 2853 |
FILE *file = fopen(fileName, "rb"); |
| 2854 |
|
| 2855 |
if (file != NULL) |
| 2856 |
{ |
| 2857 |
|
| 2858 |
|
| 2859 |
fseek(file, 0, SEEK_END); |
| 2860 |
int size = ftell(file); |
| 2861 |
fseek(file, 0, SEEK_SET); |
| 2862 |
|
| 2863 |
if (size > 0) |
| 2864 |
{ |
| 2865 |
data = (unsigned char *)RL_MALLOC(size*sizeof(unsigned char)); |
| 2866 |
|
| 2867 |
|
| 2868 |
unsigned int count = (unsigned int)fread(data, sizeof(unsigned char), size, file); |
| 2869 |
*dataSize = count; |
| 2870 |
|
| 2871 |
if (count != size) TRACELOG(LOG_WARNING, "FILEIO: [%s] File partially loaded", fileName); |
| 2872 |
else TRACELOG(LOG_INFO, "FILEIO: [%s] File loaded successfully", fileName); |
| 2873 |
} |
| 2874 |
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read file", fileName); |
| 2875 |
|
| 2876 |
fclose(file); |
| 2877 |
} |
| 2878 |
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open file", fileName); |
| 2879 |
} |
| 2880 |
else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid"); |
| 2881 |
|
| 2882 |
return data; |
| 2883 |
} |
| 2884 |
|
| 2885 |
|
| 2886 |
static bool SaveFileData(const char *fileName, void *data, int dataSize) |
| 2887 |
{ |
| 2888 |
bool result = true; |
| 2889 |
|
| 2890 |
if (fileName != NULL) |
| 2891 |
{ |
| 2892 |
FILE *file = fopen(fileName, "wb"); |
| 2893 |
|
| 2894 |
if (file != NULL) |
| 2895 |
{ |
| 2896 |
unsigned int count = (unsigned int)fwrite(data, sizeof(unsigned char), dataSize, file); |
| 2897 |
|
| 2898 |
if (count == 0) TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to write file", fileName); |
| 2899 |
else if (count != dataSize) TRACELOG(LOG_WARNING, "FILEIO: [%s] File partially written", fileName); |
| 2900 |
else TRACELOG(LOG_INFO, "FILEIO: [%s] File saved successfully", fileName); |
| 2901 |
|
| 2902 |
fclose(file); |
| 2903 |
} |
| 2904 |
else |
| 2905 |
{ |
| 2906 |
TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open file", fileName); |
| 2907 |
result = false; |
| 2908 |
} |
| 2909 |
} |
| 2910 |
else |
| 2911 |
{ |
| 2912 |
TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid"); |
| 2913 |
result = false; |
| 2914 |
} |
| 2915 |
|
| 2916 |
return result; |
| 2917 |
} |
| 2918 |
|
| 2919 |
|
| 2920 |
static bool SaveFileText(const char *fileName, char *text) |
| 2921 |
{ |
| 2922 |
bool result = true; |
| 2923 |
|
| 2924 |
if (fileName != NULL) |
| 2925 |
{ |
| 2926 |
FILE *file = fopen(fileName, "wt"); |
| 2927 |
|
| 2928 |
if (file != NULL) |
| 2929 |
{ |
| 2930 |
int count = 0; |
| 2931 |
if (text != NULL) count = fprintf(file, "%s", text); |
| 2932 |
|
| 2933 |
if (count == 0) TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to write text file", fileName); |
| 2934 |
else TRACELOG(LOG_INFO, "FILEIO: [%s] Text file saved successfully", fileName); |
| 2935 |
|
| 2936 |
fclose(file); |
| 2937 |
} |
| 2938 |
else |
| 2939 |
{ |
| 2940 |
TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open text file", fileName); |
| 2941 |
result = false; |
| 2942 |
} |
| 2943 |
} |
| 2944 |
else |
| 2945 |
{ |
| 2946 |
TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid"); |
| 2947 |
result = false; |
| 2948 |
} |
| 2949 |
|
| 2950 |
return result; |
| 2951 |
} |
| 2952 |
#endif |
| 2953 |
|
| 2954 |
#undef AudioBuffer |
| 2955 |
|
| 2956 |
#endif |