Skip to content

Latest commit

 

History

History
207 lines (170 loc) · 5.88 KB

SDL_diskaudio.c

File metadata and controls

207 lines (170 loc) · 5.88 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 3, 2018
Jan 3, 2018
3
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
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
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#if SDL_AUDIO_DRIVER_DISK
/* Output raw audio data to a file. */
#if HAVE_STDIO_H
#include <stdio.h>
#endif
#include "SDL_rwops.h"
#include "SDL_timer.h"
#include "SDL_audio.h"
#include "../SDL_audio_c.h"
#include "SDL_diskaudio.h"
Jan 6, 2017
Jan 6, 2017
36
#include "SDL_log.h"
Aug 6, 2016
Aug 6, 2016
38
/* !!! FIXME: these should be SDL hints, not environment variables. */
39
40
41
/* environment variables and defaults. */
#define DISKENVR_OUTFILE "SDL_DISKAUDIOFILE"
#define DISKDEFAULT_OUTFILE "sdlaudio.raw"
Aug 6, 2016
Aug 6, 2016
42
43
44
#define DISKENVR_INFILE "SDL_DISKAUDIOFILEIN"
#define DISKDEFAULT_INFILE "sdlaudio-in.raw"
#define DISKENVR_IODELAY "SDL_DISKAUDIODELAY"
45
46
47
/* This function waits until it is possible to write a full sound buffer */
static void
Aug 12, 2016
Aug 12, 2016
48
DISKAUDIO_WaitDevice(_THIS)
Aug 6, 2016
Aug 6, 2016
50
SDL_Delay(this->hidden->io_delay);
Aug 12, 2016
Aug 12, 2016
54
DISKAUDIO_PlayDevice(_THIS)
Aug 6, 2016
Aug 6, 2016
56
57
58
const size_t written = SDL_RWwrite(this->hidden->io,
this->hidden->mixbuf,
1, this->spec.size);
59
60
/* If we couldn't write, assume fatal error for now */
Aug 6, 2016
Aug 6, 2016
61
if (written != this->spec.size) {
62
63
64
65
66
67
68
69
SDL_OpenedAudioDeviceDisconnected(this);
}
#ifdef DEBUG_AUDIO
fprintf(stderr, "Wrote %d bytes of audio data\n", written);
#endif
}
static Uint8 *
Aug 12, 2016
Aug 12, 2016
70
DISKAUDIO_GetDeviceBuf(_THIS)
71
72
73
74
{
return (this->hidden->mixbuf);
}
Aug 6, 2016
Aug 6, 2016
75
static int
Aug 12, 2016
Aug 12, 2016
76
DISKAUDIO_CaptureFromDevice(_THIS, void *buffer, int buflen)
Aug 6, 2016
Aug 6, 2016
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
{
struct SDL_PrivateAudioData *h = this->hidden;
const int origbuflen = buflen;
SDL_Delay(h->io_delay);
if (h->io) {
const size_t br = SDL_RWread(h->io, buffer, 1, buflen);
buflen -= (int) br;
buffer = ((Uint8 *) buffer) + br;
if (buflen > 0) { /* EOF (or error, but whatever). */
SDL_RWclose(h->io);
h->io = NULL;
}
}
/* if we ran out of file, just write silence. */
SDL_memset(buffer, this->spec.silence, buflen);
return origbuflen;
}
static void
Aug 12, 2016
Aug 12, 2016
100
DISKAUDIO_FlushCapture(_THIS)
Aug 6, 2016
Aug 6, 2016
101
102
103
104
105
{
/* no op...we don't advance the file pointer or anything. */
}
Aug 12, 2016
Aug 12, 2016
107
DISKAUDIO_CloseDevice(_THIS)
Aug 6, 2016
Aug 6, 2016
109
110
if (this->hidden->io != NULL) {
SDL_RWclose(this->hidden->io);
Aug 5, 2016
Aug 5, 2016
112
SDL_free(this->hidden->mixbuf);
Aug 5, 2016
Aug 5, 2016
113
SDL_free(this->hidden);
Aug 6, 2016
Aug 6, 2016
116
117
118
119
120
121
122
123
124
125
126
127
128
static const char *
get_filename(const int iscapture, const char *devname)
{
if (devname == NULL) {
devname = SDL_getenv(iscapture ? DISKENVR_INFILE : DISKENVR_OUTFILE);
if (devname == NULL) {
devname = iscapture ? DISKDEFAULT_INFILE : DISKDEFAULT_OUTFILE;
}
}
return devname;
}
Aug 12, 2016
Aug 12, 2016
130
DISKAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
Jun 30, 2015
Jun 30, 2015
132
/* handle != NULL means "user specified the placeholder name on the fake detected device list" */
Aug 6, 2016
Aug 6, 2016
133
134
const char *fname = get_filename(iscapture, handle ? NULL : devname);
const char *envr = SDL_getenv(DISKENVR_IODELAY);
135
136
137
138
139
140
this->hidden = (struct SDL_PrivateAudioData *)
SDL_malloc(sizeof(*this->hidden));
if (this->hidden == NULL) {
return SDL_OutOfMemory();
}
Aug 5, 2016
Aug 5, 2016
141
SDL_zerop(this->hidden);
Aug 6, 2016
Aug 6, 2016
143
144
145
146
147
if (envr != NULL) {
this->hidden->io_delay = SDL_atoi(envr);
} else {
this->hidden->io_delay = ((this->spec.samples * 1000) / this->spec.freq);
}
148
149
/* Open the audio device */
Aug 6, 2016
Aug 6, 2016
150
151
this->hidden->io = SDL_RWFromFile(fname, iscapture ? "rb" : "wb");
if (this->hidden->io == NULL) {
152
153
154
155
return -1;
}
/* Allocate mixing buffer */
Aug 6, 2016
Aug 6, 2016
156
157
158
159
160
161
if (!iscapture) {
this->hidden->mixbuf = (Uint8 *) SDL_malloc(this->spec.size);
if (this->hidden->mixbuf == NULL) {
return SDL_OutOfMemory();
}
SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
Jan 6, 2017
Jan 6, 2017
164
165
166
167
168
SDL_LogCritical(SDL_LOG_CATEGORY_AUDIO,
"You are using the SDL disk i/o audio driver!\n");
SDL_LogCritical(SDL_LOG_CATEGORY_AUDIO,
" %s file [%s].\n", iscapture ? "Reading from" : "Writing to",
fname);
169
170
171
172
173
/* We're ready to rock and roll. :-) */
return 0;
}
Jun 30, 2015
Jun 30, 2015
174
static void
Aug 12, 2016
Aug 12, 2016
175
DISKAUDIO_DetectDevices(void)
Jun 30, 2015
Jun 30, 2015
176
{
Aug 6, 2016
Aug 6, 2016
177
178
SDL_AddAudioDevice(SDL_FALSE, DEFAULT_OUTPUT_DEVNAME, (void *) 0x1);
SDL_AddAudioDevice(SDL_TRUE, DEFAULT_INPUT_DEVNAME, (void *) 0x2);
Jun 30, 2015
Jun 30, 2015
179
180
}
Aug 12, 2016
Aug 12, 2016
182
DISKAUDIO_Init(SDL_AudioDriverImpl * impl)
183
184
{
/* Set the function pointers */
Aug 12, 2016
Aug 12, 2016
185
186
187
188
189
190
impl->OpenDevice = DISKAUDIO_OpenDevice;
impl->WaitDevice = DISKAUDIO_WaitDevice;
impl->PlayDevice = DISKAUDIO_PlayDevice;
impl->GetDeviceBuf = DISKAUDIO_GetDeviceBuf;
impl->CaptureFromDevice = DISKAUDIO_CaptureFromDevice;
impl->FlushCapture = DISKAUDIO_FlushCapture;
Aug 6, 2016
Aug 6, 2016
191
Aug 12, 2016
Aug 12, 2016
192
193
impl->CloseDevice = DISKAUDIO_CloseDevice;
impl->DetectDevices = DISKAUDIO_DetectDevices;
194
195
impl->AllowsArbitraryDeviceNames = 1;
Aug 6, 2016
Aug 6, 2016
196
impl->HasCaptureSupport = SDL_TRUE;
197
198
199
200
return 1; /* this audio target is available. */
}
Aug 12, 2016
Aug 12, 2016
201
202
AudioBootStrap DISKAUDIO_bootstrap = {
"disk", "direct-to-disk audio", DISKAUDIO_Init, 1
203
204
205
206
207
};
#endif /* SDL_AUDIO_DRIVER_DISK */
/* vi: set ts=4 sw=4 expandtab: */