SDL  2.0
SDL_diskaudio.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2020 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 #if SDL_AUDIO_DRIVER_DISK
24 
25 /* Output raw audio data to a file. */
26 
27 #if HAVE_STDIO_H
28 #include <stdio.h>
29 #endif
30 
31 #include "SDL_rwops.h"
32 #include "SDL_timer.h"
33 #include "SDL_audio.h"
34 #include "../SDL_audio_c.h"
35 #include "SDL_diskaudio.h"
36 #include "SDL_log.h"
37 
38 /* !!! FIXME: these should be SDL hints, not environment variables. */
39 /* environment variables and defaults. */
40 #define DISKENVR_OUTFILE "SDL_DISKAUDIOFILE"
41 #define DISKDEFAULT_OUTFILE "sdlaudio.raw"
42 #define DISKENVR_INFILE "SDL_DISKAUDIOFILEIN"
43 #define DISKDEFAULT_INFILE "sdlaudio-in.raw"
44 #define DISKENVR_IODELAY "SDL_DISKAUDIODELAY"
45 
46 /* This function waits until it is possible to write a full sound buffer */
47 static void
48 DISKAUDIO_WaitDevice(_THIS)
49 {
50  SDL_Delay(this->hidden->io_delay);
51 }
52 
53 static void
54 DISKAUDIO_PlayDevice(_THIS)
55 {
56  const size_t written = SDL_RWwrite(this->hidden->io,
57  this->hidden->mixbuf,
58  1, this->spec.size);
59 
60  /* If we couldn't write, assume fatal error for now */
61  if (written != this->spec.size) {
63  }
64 #ifdef DEBUG_AUDIO
65  fprintf(stderr, "Wrote %d bytes of audio data\n", written);
66 #endif
67 }
68 
69 static Uint8 *
70 DISKAUDIO_GetDeviceBuf(_THIS)
71 {
72  return (this->hidden->mixbuf);
73 }
74 
75 static int
76 DISKAUDIO_CaptureFromDevice(_THIS, void *buffer, int buflen)
77 {
78  struct SDL_PrivateAudioData *h = this->hidden;
79  const int origbuflen = buflen;
80 
81  SDL_Delay(h->io_delay);
82 
83  if (h->io) {
84  const size_t br = SDL_RWread(h->io, buffer, 1, buflen);
85  buflen -= (int) br;
86  buffer = ((Uint8 *) buffer) + br;
87  if (buflen > 0) { /* EOF (or error, but whatever). */
88  SDL_RWclose(h->io);
89  h->io = NULL;
90  }
91  }
92 
93  /* if we ran out of file, just write silence. */
94  SDL_memset(buffer, this->spec.silence, buflen);
95 
96  return origbuflen;
97 }
98 
99 static void
100 DISKAUDIO_FlushCapture(_THIS)
101 {
102  /* no op...we don't advance the file pointer or anything. */
103 }
104 
105 
106 static void
107 DISKAUDIO_CloseDevice(_THIS)
108 {
109  if (this->hidden->io != NULL) {
110  SDL_RWclose(this->hidden->io);
111  }
112  SDL_free(this->hidden->mixbuf);
113  SDL_free(this->hidden);
114 }
115 
116 
117 static const char *
118 get_filename(const int iscapture, const char *devname)
119 {
120  if (devname == NULL) {
121  devname = SDL_getenv(iscapture ? DISKENVR_INFILE : DISKENVR_OUTFILE);
122  if (devname == NULL) {
123  devname = iscapture ? DISKDEFAULT_INFILE : DISKDEFAULT_OUTFILE;
124  }
125  }
126  return devname;
127 }
128 
129 static int
130 DISKAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
131 {
132  /* handle != NULL means "user specified the placeholder name on the fake detected device list" */
133  const char *fname = get_filename(iscapture, handle ? NULL : devname);
134  const char *envr = SDL_getenv(DISKENVR_IODELAY);
135 
136  this->hidden = (struct SDL_PrivateAudioData *)
137  SDL_malloc(sizeof(*this->hidden));
138  if (this->hidden == NULL) {
139  return SDL_OutOfMemory();
140  }
141  SDL_zerop(this->hidden);
142 
143  if (envr != NULL) {
144  this->hidden->io_delay = SDL_atoi(envr);
145  } else {
146  this->hidden->io_delay = ((this->spec.samples * 1000) / this->spec.freq);
147  }
148 
149  /* Open the audio device */
150  this->hidden->io = SDL_RWFromFile(fname, iscapture ? "rb" : "wb");
151  if (this->hidden->io == NULL) {
152  return -1;
153  }
154 
155  /* Allocate mixing buffer */
156  if (!iscapture) {
157  this->hidden->mixbuf = (Uint8 *) SDL_malloc(this->spec.size);
158  if (this->hidden->mixbuf == NULL) {
159  return SDL_OutOfMemory();
160  }
161  SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
162  }
163 
165  "You are using the SDL disk i/o audio driver!\n");
167  " %s file [%s].\n", iscapture ? "Reading from" : "Writing to",
168  fname);
169 
170  /* We're ready to rock and roll. :-) */
171  return 0;
172 }
173 
174 static void
175 DISKAUDIO_DetectDevices(void)
176 {
179 }
180 
181 static int
182 DISKAUDIO_Init(SDL_AudioDriverImpl * impl)
183 {
184  /* Set the function pointers */
185  impl->OpenDevice = DISKAUDIO_OpenDevice;
186  impl->WaitDevice = DISKAUDIO_WaitDevice;
187  impl->PlayDevice = DISKAUDIO_PlayDevice;
188  impl->GetDeviceBuf = DISKAUDIO_GetDeviceBuf;
189  impl->CaptureFromDevice = DISKAUDIO_CaptureFromDevice;
190  impl->FlushCapture = DISKAUDIO_FlushCapture;
191 
192  impl->CloseDevice = DISKAUDIO_CloseDevice;
193  impl->DetectDevices = DISKAUDIO_DetectDevices;
194 
195  impl->AllowsArbitraryDeviceNames = 1;
196  impl->HasCaptureSupport = SDL_TRUE;
197 
198  return 1; /* this audio target is available. */
199 }
200 
202  "disk", "direct-to-disk audio", DISKAUDIO_Init, 1
203 };
204 
205 #endif /* SDL_AUDIO_DRIVER_DISK */
206 
207 /* vi: set ts=4 sw=4 expandtab: */
Uint8
uint8_t Uint8
Definition: SDL_stdinc.h:179
SDL_memset
#define SDL_memset
Definition: SDL_dynapi_overrides.h:386
SDL_AudioDriverImpl::HasCaptureSupport
int HasCaptureSupport
Definition: SDL_sysaudio.h:90
DEFAULT_OUTPUT_DEVNAME
#define DEFAULT_OUTPUT_DEVNAME
Definition: SDL_sysaudio.h:32
fname
set set set set set set set macro pixldst1 abits if abits op else op endif endm macro pixldst2 abits if abits op else op endif endm macro pixldst4 abits if abits op else op endif endm macro pixldst0 abits op endm macro pixldst3 mem_operand op endm macro pixldst30 mem_operand op endm macro pixldst abits if abits elseif abits elseif abits elseif abits elseif abits pixldst0 abits else pixldst0 abits pixldst0 abits pixldst0 abits pixldst0 abits endif elseif abits else pixldst0 abits pixldst0 abits endif elseif abits else error unsupported bpp *numpix else pixst endif endm macro pixld1_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl else error unsupported endif endm macro pixld2_s mem_operand if mov asr add asl add asl mov asr sub UNIT_X add asl mov asr add asl add asl mov asr add UNIT_X add asl else pixld1_s mem_operand pixld1_s mem_operand endif endm macro pixld0_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl endif endm macro pixld_s_internal mem_operand if mem_operand pixld2_s mem_operand pixdeinterleave basereg elseif mem_operand elseif mem_operand elseif mem_operand elseif mem_operand pixld0_s mem_operand else pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else error unsupported mem_operand if bpp mem_operand endif endm macro vuzp8 reg2 vuzp d d &reg2 endm macro vzip8 reg2 vzip d d &reg2 endm macro pixdeinterleave basereg basereg basereg basereg basereg endif endm macro pixinterleave basereg basereg basereg basereg basereg endif endm macro PF boost_increment endif if endif PF tst PF addne PF subne PF cmp ORIG_W if endif if endif if endif PF subge ORIG_W PF subges if endif if endif if endif endif endm macro cache_preload_simple endif if dst_r_bpp pld[DST_R, #(PREFETCH_DISTANCE_SIMPLE *dst_r_bpp/8)] endif if mask_bpp pld fname[MASK, #(PREFETCH_DISTANCE_SIMPLE *mask_bpp/8)] endif endif endm macro fetch_mask_pixblock pixld mask_basereg pixblock_size MASK endm macro ensure_destination_ptr_alignment process_pixblock_tail_head if beq irp skip1 beq endif SRC MASK if dst_r_bpp DST_R else add endif PF add sub src_basereg pixdeinterleave mask_basereg pixdeinterleave dst_r_basereg process_pixblock_head pixblock_size cache_preload_simple process_pixblock_tail pixinterleave dst_w_basereg irp beq endif process_pixblock_tail_head tst beq irp if pixblock_size chunk_size tst beq pixld_src SRC pixld MASK if DST_R else pixld DST_R endif if src_basereg pixdeinterleave mask_basereg pixdeinterleave dst_r_basereg process_pixblock_head if pixblock_size cache_preload_simple endif process_pixblock_tail pixinterleave dst_w_basereg irp if pixblock_size chunk_size tst beq if DST_W else pixst DST_W else mov ORIG_W endif add lsl if lsl endif if lsl endif lsl endif lsl endif lsl endif subs mov DST_W if regs_shortage str endif bge start_of_loop_label endm macro generate_composite_function
Definition: pixman-arm-neon-asm.h:617
SDL_AudioDriverImpl::FlushCapture
void(* FlushCapture)(_THIS)
Definition: SDL_sysaudio.h:76
NULL
#define NULL
Definition: begin_code.h:167
handle
EGLImageKHR EGLint EGLint * handle
Definition: eglext.h:937
SDL_LogCritical
#define SDL_LogCritical
Definition: SDL_dynapi_overrides.h:37
SDL_timer.h
SDL_diskaudio.h
SDL_AudioSpec::samples
Uint16 samples
Definition: SDL_audio.h:184
SDL_zerop
#define SDL_zerop(x)
Definition: SDL_stdinc.h:419
SDL_log.h
DEFAULT_INPUT_DEVNAME
#define DEFAULT_INPUT_DEVNAME
Definition: SDL_sysaudio.h:33
SDL_AudioDriverImpl::OpenDevice
int(* OpenDevice)(_THIS, void *handle, const char *devname, int iscapture)
Definition: SDL_sysaudio.h:68
DISKAUDIO_bootstrap
AudioBootStrap DISKAUDIO_bootstrap
SDL_RWread
#define SDL_RWread
Definition: SDL_dynapi_overrides.h:723
x1
GLuint GLfloat GLfloat GLfloat x1
Definition: SDL_opengl_glext.h:8586
SDL_OpenedAudioDeviceDisconnected
void SDL_OpenedAudioDeviceDisconnected(SDL_AudioDevice *device)
Definition: SDL_audio.c:486
h
GLfloat GLfloat GLfloat GLfloat h
Definition: SDL_opengl_glext.h:1949
SDL_PrivateAudioData::iscapture
SDL_bool iscapture
Definition: SDL_qsa_audio.h:37
AudioBootStrap
Definition: SDL_sysaudio.h:177
SDL_PrivateAudioData
Definition: SDL_alsa_audio.h:34
SDL_RWwrite
#define SDL_RWwrite
Definition: SDL_dynapi_overrides.h:724
SDL_AudioDriverImpl::WaitDevice
void(* WaitDevice)(_THIS)
Definition: SDL_sysaudio.h:72
buffer
GLuint buffer
Definition: SDL_opengl_glext.h:536
SDL_audio.h
SDL_AudioDriverImpl
Definition: SDL_sysaudio.h:66
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
SDL_RWFromFile
#define SDL_RWFromFile
Definition: SDL_dynapi_overrides.h:351
SDL_AudioDriverImpl::DetectDevices
void(* DetectDevices)(void)
Definition: SDL_sysaudio.h:67
_THIS
#define _THIS
Definition: SDL_alsa_audio.h:31
SDL_AudioDriverImpl::PlayDevice
void(* PlayDevice)(_THIS)
Definition: SDL_sysaudio.h:73
SDL_TRUE
@ SDL_TRUE
Definition: SDL_stdinc.h:164
SDL_Delay
#define SDL_Delay
Definition: SDL_dynapi_overrides.h:486
SDL_AudioSpec::freq
int freq
Definition: SDL_audio.h:180
SDL_AudioSpec::silence
Uint8 silence
Definition: SDL_audio.h:183
SDL_AudioDriverImpl::AllowsArbitraryDeviceNames
int AllowsArbitraryDeviceNames
Definition: SDL_sysaudio.h:93
SDL_OutOfMemory
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
spec
SDL_AudioSpec spec
Definition: loopwave.c:31
SDL_AudioDriverImpl::CaptureFromDevice
int(* CaptureFromDevice)(_THIS, void *buffer, int buflen)
Definition: SDL_sysaudio.h:75
SDL_atoi
#define SDL_atoi
Definition: SDL_dynapi_overrides.h:410
SDL_AudioSpec::size
Uint32 size
Definition: SDL_audio.h:186
SDL_getenv
#define SDL_getenv
Definition: SDL_dynapi_overrides.h:378
x2
GLfixed GLfixed x2
Definition: SDL_opengl_glext.h:4586
SDL_AudioDriverImpl::GetDeviceBuf
Uint8 *(* GetDeviceBuf)(_THIS)
Definition: SDL_sysaudio.h:74
SDL_RWclose
#define SDL_RWclose
Definition: SDL_dynapi_overrides.h:725
SDL_LOG_CATEGORY_AUDIO
@ SDL_LOG_CATEGORY_AUDIO
Definition: SDL_log.h:70
SDL_FALSE
@ SDL_FALSE
Definition: SDL_stdinc.h:163
SDL_malloc
#define SDL_malloc
Definition: SDL_dynapi_overrides.h:374
SDL_AudioDriverImpl::CloseDevice
void(* CloseDevice)(_THIS)
Definition: SDL_sysaudio.h:78
SDL_AddAudioDevice
void SDL_AddAudioDevice(const int iscapture, const char *name, void *handle)
Definition: SDL_audio.c:469
SDL_rwops.h