Atlas - SDL_diskaudio.c

Home / ext / SDL / src / audio / disk Lines: 1 | Size: 5953 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20*/ 21#include "SDL_internal.h" 22 23#ifdef SDL_AUDIO_DRIVER_DISK 24 25// Output raw audio data to a file. 26 27#include "../SDL_sysaudio.h" 28#include "SDL_diskaudio.h" 29 30#define DISKDEFAULT_OUTFILE "sdlaudio.raw" 31#define DISKDEFAULT_INFILE "sdlaudio-in.raw" 32 33static bool DISKAUDIO_WaitDevice(SDL_AudioDevice *device) 34{ 35 SDL_Delay(device->hidden->io_delay); 36 return true; 37} 38 39static bool DISKAUDIO_PlayDevice(SDL_AudioDevice *device, const Uint8 *buffer, int buffer_size) 40{ 41 const int written = (int)SDL_WriteIO(device->hidden->io, buffer, (size_t)buffer_size); 42 if (written != buffer_size) { // If we couldn't write, assume fatal error for now 43 return false; 44 } 45#ifdef DEBUG_AUDIO 46 SDL_Log("DISKAUDIO: Wrote %d bytes of audio data", (int) written); 47#endif 48 return true; 49} 50 51static Uint8 *DISKAUDIO_GetDeviceBuf(SDL_AudioDevice *device, int *buffer_size) 52{ 53 return device->hidden->mixbuf; 54} 55 56static int DISKAUDIO_RecordDevice(SDL_AudioDevice *device, void *buffer, int buflen) 57{ 58 struct SDL_PrivateAudioData *h = device->hidden; 59 const int origbuflen = buflen; 60 61 if (h->io) { 62 const int br = (int)SDL_ReadIO(h->io, buffer, (size_t)buflen); 63 buflen -= br; 64 buffer = ((Uint8 *)buffer) + br; 65 if (buflen > 0) { // EOF (or error, but whatever). 66 SDL_CloseIO(h->io); 67 h->io = NULL; 68 } 69 } 70 71 // if we ran out of file, just write silence. 72 SDL_memset(buffer, device->silence_value, buflen); 73 74 return origbuflen; 75} 76 77static void DISKAUDIO_FlushRecording(SDL_AudioDevice *device) 78{ 79 // no op...we don't advance the file pointer or anything. 80} 81 82static void DISKAUDIO_CloseDevice(SDL_AudioDevice *device) 83{ 84 if (device->hidden) { 85 if (device->hidden->io) { 86 SDL_CloseIO(device->hidden->io); 87 } 88 SDL_free(device->hidden->mixbuf); 89 SDL_free(device->hidden); 90 device->hidden = NULL; 91 } 92} 93 94static const char *get_filename(const bool recording) 95{ 96 const char *devname = SDL_GetHint(recording ? SDL_HINT_AUDIO_DISK_INPUT_FILE : SDL_HINT_AUDIO_DISK_OUTPUT_FILE); 97 if (!devname) { 98 devname = recording ? DISKDEFAULT_INFILE : DISKDEFAULT_OUTFILE; 99 } 100 return devname; 101} 102 103static const char *AudioFormatString(SDL_AudioFormat fmt) 104{ 105 const char *str = SDL_GetAudioFormatName(fmt); 106 SDL_assert(str); 107 if (SDL_strncmp(str, "SDL_AUDIO_", 10) == 0) { 108 str += 10; // so we return "S8" instead of "SDL_AUDIO_S8", etc. 109 } 110 return str; 111} 112 113static bool DISKAUDIO_OpenDevice(SDL_AudioDevice *device) 114{ 115 bool recording = device->recording; 116 const char *fname = get_filename(recording); 117 118 device->hidden = (struct SDL_PrivateAudioData *) SDL_calloc(1, sizeof(*device->hidden)); 119 if (!device->hidden) { 120 return false; 121 } 122 123 device->hidden->io_delay = ((device->sample_frames * 1000) / device->spec.freq); 124 125 const char *hint = SDL_GetHint(SDL_HINT_AUDIO_DISK_TIMESCALE); 126 if (hint) { 127 double scale = SDL_atof(hint); 128 if (scale >= 0.0) { 129 device->hidden->io_delay = (Uint32)SDL_round(device->hidden->io_delay * scale); 130 } 131 } 132 133 // Open the "audio device" 134 device->hidden->io = SDL_IOFromFile(fname, recording ? "rb" : "wb"); 135 if (!device->hidden->io) { 136 return false; 137 } 138 139 // Allocate mixing buffer 140 if (!recording) { 141 device->hidden->mixbuf = (Uint8 *)SDL_malloc(device->buffer_size); 142 if (!device->hidden->mixbuf) { 143 return false; 144 } 145 SDL_memset(device->hidden->mixbuf, device->silence_value, device->buffer_size); 146 } 147 148 SDL_LogCritical(SDL_LOG_CATEGORY_AUDIO, "You are using the SDL disk i/o audio driver!"); 149 SDL_LogCritical(SDL_LOG_CATEGORY_AUDIO, " %s file [%s], format=%s channels=%d freq=%d.", 150 recording ? "Reading from" : "Writing to", fname, 151 AudioFormatString(device->spec.format), device->spec.channels, device->spec.freq); 152 153 return true; // We're ready to rock and roll. :-) 154} 155 156static void DISKAUDIO_DetectDevices(SDL_AudioDevice **default_playback, SDL_AudioDevice **default_recording) 157{ 158 *default_playback = SDL_AddAudioDevice(false, DEFAULT_PLAYBACK_DEVNAME, NULL, (void *)0x1); 159 *default_recording = SDL_AddAudioDevice(true, DEFAULT_RECORDING_DEVNAME, NULL, (void *)0x2); 160} 161 162static bool DISKAUDIO_Init(SDL_AudioDriverImpl *impl) 163{ 164 impl->OpenDevice = DISKAUDIO_OpenDevice; 165 impl->WaitDevice = DISKAUDIO_WaitDevice; 166 impl->WaitRecordingDevice = DISKAUDIO_WaitDevice; 167 impl->PlayDevice = DISKAUDIO_PlayDevice; 168 impl->GetDeviceBuf = DISKAUDIO_GetDeviceBuf; 169 impl->RecordDevice = DISKAUDIO_RecordDevice; 170 impl->FlushRecording = DISKAUDIO_FlushRecording; 171 impl->CloseDevice = DISKAUDIO_CloseDevice; 172 impl->DetectDevices = DISKAUDIO_DetectDevices; 173 174 impl->HasRecordingSupport = true; 175 176 return true; 177} 178 179AudioBootStrap DISKAUDIO_bootstrap = { 180 "disk", "direct-to-disk audio", DISKAUDIO_Init, true, false 181}; 182 183#endif // SDL_AUDIO_DRIVER_DISK 184
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.