SDL  2.0
SDL_diskaudio.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2016 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 
37 /* !!! FIXME: these should be SDL hints, not environment variables. */
38 /* environment variables and defaults. */
39 #define DISKENVR_OUTFILE "SDL_DISKAUDIOFILE"
40 #define DISKDEFAULT_OUTFILE "sdlaudio.raw"
41 #define DISKENVR_INFILE "SDL_DISKAUDIOFILEIN"
42 #define DISKDEFAULT_INFILE "sdlaudio-in.raw"
43 #define DISKENVR_IODELAY "SDL_DISKAUDIODELAY"
44 
45 /* This function waits until it is possible to write a full sound buffer */
46 static void
47 DISKAUDIO_WaitDevice(_THIS)
48 {
49  SDL_Delay(this->hidden->io_delay);
50 }
51 
52 static void
53 DISKAUDIO_PlayDevice(_THIS)
54 {
55  const size_t written = SDL_RWwrite(this->hidden->io,
56  this->hidden->mixbuf,
57  1, this->spec.size);
58 
59  /* If we couldn't write, assume fatal error for now */
60  if (written != this->spec.size) {
62  }
63 #ifdef DEBUG_AUDIO
64  fprintf(stderr, "Wrote %d bytes of audio data\n", written);
65 #endif
66 }
67 
68 static Uint8 *
69 DISKAUDIO_GetDeviceBuf(_THIS)
70 {
71  return (this->hidden->mixbuf);
72 }
73 
74 static int
75 DISKAUDIO_CaptureFromDevice(_THIS, void *buffer, int buflen)
76 {
77  struct SDL_PrivateAudioData *h = this->hidden;
78  const int origbuflen = buflen;
79 
80  SDL_Delay(h->io_delay);
81 
82  if (h->io) {
83  const size_t br = SDL_RWread(h->io, buffer, 1, buflen);
84  buflen -= (int) br;
85  buffer = ((Uint8 *) buffer) + br;
86  if (buflen > 0) { /* EOF (or error, but whatever). */
87  SDL_RWclose(h->io);
88  h->io = NULL;
89  }
90  }
91 
92  /* if we ran out of file, just write silence. */
93  SDL_memset(buffer, this->spec.silence, buflen);
94 
95  return origbuflen;
96 }
97 
98 static void
99 DISKAUDIO_FlushCapture(_THIS)
100 {
101  /* no op...we don't advance the file pointer or anything. */
102 }
103 
104 
105 static void
106 DISKAUDIO_CloseDevice(_THIS)
107 {
108  if (this->hidden->io != NULL) {
109  SDL_RWclose(this->hidden->io);
110  }
111  SDL_free(this->hidden->mixbuf);
112  SDL_free(this->hidden);
113 }
114 
115 
116 static const char *
117 get_filename(const int iscapture, const char *devname)
118 {
119  if (devname == NULL) {
120  devname = SDL_getenv(iscapture ? DISKENVR_INFILE : DISKENVR_OUTFILE);
121  if (devname == NULL) {
122  devname = iscapture ? DISKDEFAULT_INFILE : DISKDEFAULT_OUTFILE;
123  }
124  }
125  return devname;
126 }
127 
128 static int
129 DISKAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
130 {
131  /* handle != NULL means "user specified the placeholder name on the fake detected device list" */
132  const char *fname = get_filename(iscapture, handle ? NULL : devname);
133  const char *envr = SDL_getenv(DISKENVR_IODELAY);
134 
135  this->hidden = (struct SDL_PrivateAudioData *)
136  SDL_malloc(sizeof(*this->hidden));
137  if (this->hidden == NULL) {
138  return SDL_OutOfMemory();
139  }
140  SDL_zerop(this->hidden);
141 
142  if (envr != NULL) {
143  this->hidden->io_delay = SDL_atoi(envr);
144  } else {
145  this->hidden->io_delay = ((this->spec.samples * 1000) / this->spec.freq);
146  }
147 
148  /* Open the audio device */
149  this->hidden->io = SDL_RWFromFile(fname, iscapture ? "rb" : "wb");
150  if (this->hidden->io == NULL) {
151  return -1;
152  }
153 
154  /* Allocate mixing buffer */
155  if (!iscapture) {
156  this->hidden->mixbuf = (Uint8 *) SDL_malloc(this->spec.size);
157  if (this->hidden->mixbuf == NULL) {
158  return SDL_OutOfMemory();
159  }
160  SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
161  }
162 
163 #if HAVE_STDIO_H
164  fprintf(stderr,
165  "WARNING: You are using the SDL disk i/o audio driver!\n"
166  " %s file [%s].\n", iscapture ? "Reading from" : "Writing to",
167  fname);
168 #endif
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: */
GLuint GLfloat GLfloat GLfloat x1
void(* DetectDevices)(void)
Definition: SDL_sysaudio.h:75
Uint8 silence
Definition: SDL_audio.h:173
#define SDL_RWwrite(ctx, ptr, size, n)
Definition: SDL_rwops.h:188
#define SDL_RWread(ctx, ptr, size, n)
Definition: SDL_rwops.h:187
void(* PlayDevice)(_THIS)
Definition: SDL_sysaudio.h:79
Uint16 samples
Definition: SDL_audio.h:174
void(* WaitDevice)(_THIS)
Definition: SDL_sysaudio.h:78
void SDL_OpenedAudioDeviceDisconnected(SDL_AudioDevice *device)
Definition: SDL_audio.c:381
#define SDL_zerop(x)
Definition: SDL_stdinc.h:360
GLfixed GLfixed x2
SDL_AudioSpec spec
Definition: loopwave.c:35
#define SDL_RWFromFile
#define _THIS
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:143
void SDL_free(void *mem)
AudioBootStrap DISKAUDIO_bootstrap
#define SDL_atoi
#define SDL_Delay
#define SDL_getenv
Uint32 size
Definition: SDL_audio.h:176
int(* OpenDevice)(_THIS, void *handle, const char *devname, int iscapture)
Definition: SDL_sysaudio.h:76
#define NULL
Definition: begin_code.h:143
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
int(* CaptureFromDevice)(_THIS, void *buffer, int buflen)
Definition: SDL_sysaudio.h:82
#define SDL_RWclose(ctx)
Definition: SDL_rwops.h:189
void(* CloseDevice)(_THIS)
Definition: SDL_sysaudio.h:85
void(* FlushCapture)(_THIS)
Definition: SDL_sysaudio.h:83
GLuint buffer
Uint8 *(* GetDeviceBuf)(_THIS)
Definition: SDL_sysaudio.h:81
#define DEFAULT_OUTPUT_DEVNAME
Definition: SDL_sysaudio.h:30
#define DEFAULT_INPUT_DEVNAME
Definition: SDL_sysaudio.h:31
#define SDL_malloc
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 local 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
GLfloat GLfloat GLfloat GLfloat h
#define SDL_memset
void SDL_AddAudioDevice(const int iscapture, const char *name, void *handle)
Definition: SDL_audio.c:364