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_audiomem.h"
35 #include "../SDL_audio_c.h"
36 #include "SDL_diskaudio.h"
37 
38 /* environment variables and defaults. */
39 #define DISKENVR_OUTFILE "SDL_DISKAUDIOFILE"
40 #define DISKDEFAULT_OUTFILE "sdlaudio.raw"
41 #define DISKENVR_WRITEDELAY "SDL_DISKAUDIODELAY"
42 #define DISKDEFAULT_WRITEDELAY 150
43 
44 static const char *
45 DISKAUD_GetOutputFilename(const char *devname)
46 {
47  if (devname == NULL) {
48  devname = SDL_getenv(DISKENVR_OUTFILE);
49  if (devname == NULL) {
50  devname = DISKDEFAULT_OUTFILE;
51  }
52  }
53  return devname;
54 }
55 
56 /* This function waits until it is possible to write a full sound buffer */
57 static void
58 DISKAUD_WaitDevice(_THIS)
59 {
60  SDL_Delay(this->hidden->write_delay);
61 }
62 
63 static void
64 DISKAUD_PlayDevice(_THIS)
65 {
66  size_t written;
67 
68  /* Write the audio data */
69  written = SDL_RWwrite(this->hidden->output,
70  this->hidden->mixbuf, 1, this->hidden->mixlen);
71 
72  /* If we couldn't write, assume fatal error for now */
73  if (written != this->hidden->mixlen) {
75  }
76 #ifdef DEBUG_AUDIO
77  fprintf(stderr, "Wrote %d bytes of audio data\n", written);
78 #endif
79 }
80 
81 static Uint8 *
82 DISKAUD_GetDeviceBuf(_THIS)
83 {
84  return (this->hidden->mixbuf);
85 }
86 
87 static void
88 DISKAUD_CloseDevice(_THIS)
89 {
90  if (this->hidden != NULL) {
91  SDL_FreeAudioMem(this->hidden->mixbuf);
92  this->hidden->mixbuf = NULL;
93  if (this->hidden->output != NULL) {
94  SDL_RWclose(this->hidden->output);
95  this->hidden->output = NULL;
96  }
97  SDL_free(this->hidden);
98  this->hidden = NULL;
99  }
100 }
101 
102 static int
103 DISKAUD_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
104 {
105  /* handle != NULL means "user specified the placeholder name on the fake detected device list" */
106  const char *fname = DISKAUD_GetOutputFilename(handle ? NULL : devname);
107  const char *envr = SDL_getenv(DISKENVR_WRITEDELAY);
108 
109  this->hidden = (struct SDL_PrivateAudioData *)
110  SDL_malloc(sizeof(*this->hidden));
111  if (this->hidden == NULL) {
112  return SDL_OutOfMemory();
113  }
114  SDL_memset(this->hidden, 0, sizeof(*this->hidden));
115 
116  this->hidden->mixlen = this->spec.size;
117  this->hidden->write_delay =
118  (envr) ? SDL_atoi(envr) : DISKDEFAULT_WRITEDELAY;
119 
120  /* Open the audio device */
121  this->hidden->output = SDL_RWFromFile(fname, "wb");
122  if (this->hidden->output == NULL) {
123  DISKAUD_CloseDevice(this);
124  return -1;
125  }
126 
127  /* Allocate mixing buffer */
128  this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
129  if (this->hidden->mixbuf == NULL) {
130  DISKAUD_CloseDevice(this);
131  return -1;
132  }
133  SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
134 
135 #if HAVE_STDIO_H
136  fprintf(stderr,
137  "WARNING: You are using the SDL disk writer audio driver!\n"
138  " Writing to file [%s].\n", fname);
139 #endif
140 
141  /* We're ready to rock and roll. :-) */
142  return 0;
143 }
144 
145 static void
146 DISKAUD_DetectDevices(void)
147 {
148  /* !!! FIXME: stole this literal string from DEFAULT_OUTPUT_DEVNAME in SDL_audio.c */
149  SDL_AddAudioDevice(SDL_FALSE, "System audio output device", (void *) 0x1);
150 }
151 
152 static int
153 DISKAUD_Init(SDL_AudioDriverImpl * impl)
154 {
155  /* Set the function pointers */
156  impl->OpenDevice = DISKAUD_OpenDevice;
157  impl->WaitDevice = DISKAUD_WaitDevice;
158  impl->PlayDevice = DISKAUD_PlayDevice;
159  impl->GetDeviceBuf = DISKAUD_GetDeviceBuf;
160  impl->CloseDevice = DISKAUD_CloseDevice;
161  impl->DetectDevices = DISKAUD_DetectDevices;
162 
163  impl->AllowsArbitraryDeviceNames = 1;
164 
165  return 1; /* this audio target is available. */
166 }
167 
169  "disk", "direct-to-disk audio", DISKAUD_Init, 1
170 };
171 
172 #endif /* SDL_AUDIO_DRIVER_DISK */
173 
174 /* vi: set ts=4 sw=4 expandtab: */
AudioBootStrap DISKAUD_bootstrap
GLuint GLfloat GLfloat GLfloat x1
void(* DetectDevices)(void)
Definition: SDL_sysaudio.h:71
#define SDL_RWwrite(ctx, ptr, size, n)
Definition: SDL_rwops.h:188
#define SDL_FreeAudioMem
Definition: SDL_audiomem.h:24
void(* PlayDevice)(_THIS)
Definition: SDL_sysaudio.h:75
void(* WaitDevice)(_THIS)
Definition: SDL_sysaudio.h:74
void SDL_OpenedAudioDeviceDisconnected(SDL_AudioDevice *device)
Definition: SDL_audio.c:364
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:139
void SDL_free(void *mem)
#define SDL_AllocAudioMem
Definition: SDL_audiomem.h:23
#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:72
#define NULL
Definition: begin_code.h:143
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
#define SDL_RWclose(ctx)
Definition: SDL_rwops.h:189
void(* CloseDevice)(_THIS)
Definition: SDL_sysaudio.h:79
Uint8 *(* GetDeviceBuf)(_THIS)
Definition: SDL_sysaudio.h:77
#define SDL_malloc
#define SDL_memset
void SDL_AddAudioDevice(const int iscapture, const char *name, void *handle)
Definition: SDL_audio.c:347