1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
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 bool DISKAUDIO_OpenDevice(SDL_AudioDevice *device)
104{
105 bool recording = device->recording;
106 const char *fname = get_filename(recording);
107
108 device->hidden = (struct SDL_PrivateAudioData *) SDL_calloc(1, sizeof(*device->hidden));
109 if (!device->hidden) {
110 return false;
111 }
112
113 device->hidden->io_delay = ((device->sample_frames * 1000) / device->spec.freq);
114
115 const char *hint = SDL_GetHint(SDL_HINT_AUDIO_DISK_TIMESCALE);
116 if (hint) {
117 double scale = SDL_atof(hint);
118 if (scale >= 0.0) {
119 device->hidden->io_delay = (Uint32)SDL_round(device->hidden->io_delay * scale);
120 }
121 }
122
123 // Open the "audio device"
124 device->hidden->io = SDL_IOFromFile(fname, recording ? "rb" : "wb");
125 if (!device->hidden->io) {
126 return false;
127 }
128
129 // Allocate mixing buffer
130 if (!recording) {
131 device->hidden->mixbuf = (Uint8 *)SDL_malloc(device->buffer_size);
132 if (!device->hidden->mixbuf) {
133 return false;
134 }
135 SDL_memset(device->hidden->mixbuf, device->silence_value, device->buffer_size);
136 }
137
138 SDL_LogCritical(SDL_LOG_CATEGORY_AUDIO, "You are using the SDL disk i/o audio driver!");
139 SDL_LogCritical(SDL_LOG_CATEGORY_AUDIO, " %s file [%s].", recording ? "Reading from" : "Writing to", fname);
140
141 return true; // We're ready to rock and roll. :-)
142}
143
144static void DISKAUDIO_DetectDevices(SDL_AudioDevice **default_playback, SDL_AudioDevice **default_recording)
145{
146 *default_playback = SDL_AddAudioDevice(false, DEFAULT_PLAYBACK_DEVNAME, NULL, (void *)0x1);
147 *default_recording = SDL_AddAudioDevice(true, DEFAULT_RECORDING_DEVNAME, NULL, (void *)0x2);
148}
149
150static bool DISKAUDIO_Init(SDL_AudioDriverImpl *impl)
151{
152 impl->OpenDevice = DISKAUDIO_OpenDevice;
153 impl->WaitDevice = DISKAUDIO_WaitDevice;
154 impl->WaitRecordingDevice = DISKAUDIO_WaitDevice;
155 impl->PlayDevice = DISKAUDIO_PlayDevice;
156 impl->GetDeviceBuf = DISKAUDIO_GetDeviceBuf;
157 impl->RecordDevice = DISKAUDIO_RecordDevice;
158 impl->FlushRecording = DISKAUDIO_FlushRecording;
159 impl->CloseDevice = DISKAUDIO_CloseDevice;
160 impl->DetectDevices = DISKAUDIO_DetectDevices;
161
162 impl->HasRecordingSupport = true;
163
164 return true;
165}
166
167AudioBootStrap DISKAUDIO_bootstrap = {
168 "disk", "direct-to-disk audio", DISKAUDIO_Init, true, false
169};
170
171#endif // SDL_AUDIO_DRIVER_DISK
172