Mercurial > SDL_sound_CoreAudio
annotate decoders/raw.c @ 62:b13fafb976be
Changed _D macro to DBGSND.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Mon, 24 Sep 2001 15:31:25 +0000 |
parents | ea58bc3b15d7 |
children | 40006625142a |
rev | line source |
---|---|
4 | 1 /* |
2 * SDL_sound -- An abstract sound format decoding API. | |
3 * Copyright (C) 2001 Ryan C. Gordon. | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2.1 of the License, or (at your option) any later version. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Lesser General Public | |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 */ | |
19 | |
20 /* | |
21 * RAW decoder for SDL_sound. This is as simple as it gets. | |
22 * | |
23 * This driver handles raw audio data. You must, regardless of where the | |
24 * data is actually coming from, specify the string "RAW" in the extension | |
25 * parameter of Sound_NewSample() (or, alternately, open a file with the | |
26 * extension ".raw" in Sound_NewSampleFromFile()). The string is checked | |
27 * case-insensitive. We need this check, because raw data, being raw, has | |
28 * no headers or magic number we can use to determine if we should handle a | |
29 * given file, so we needed some way to have this "decoder" discriminate. | |
30 * | |
31 * When calling Sound_NewSample*(), you must also specify a "desired" | |
32 * audio format. The "actual" format will always match what you specify, so | |
33 * there will be no conversion overhead, but these routines need to know how | |
34 * to treat the bits, since it's all random garbage otherwise. | |
35 * | |
36 * Please see the file LICENSE in the source's root directory. | |
37 * | |
38 * This file written by Ryan C. Gordon. (icculus@clutteredmind.org) | |
39 */ | |
40 | |
41 #include <stdio.h> | |
42 #include <stdlib.h> | |
43 #include <string.h> | |
44 #include <assert.h> | |
45 #include "SDL_sound.h" | |
46 | |
47 #define __SDL_SOUND_INTERNAL__ | |
48 #include "SDL_sound_internal.h" | |
49 | |
50 #if (!defined SOUND_SUPPORTS_RAW) | |
51 #error SOUND_SUPPORTS_RAW must be defined. | |
52 #endif | |
53 | |
54 | |
47
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
55 static int RAW_init(void); |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
56 static void RAW_quit(void); |
4 | 57 static int RAW_open(Sound_Sample *sample, const char *ext); |
58 static void RAW_close(Sound_Sample *sample); | |
7
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
59 static Uint32 RAW_read(Sound_Sample *sample); |
4 | 60 |
61 const Sound_DecoderFunctions __Sound_DecoderFunctions_RAW = | |
62 { | |
63 { | |
64 "RAW", | |
65 "Raw audio", | |
66 "Ryan C. Gordon <icculus@clutteredmind.org>", | |
67 "http://www.icculus.org/SDL_sound/" | |
68 }, | |
69 | |
47
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
70 RAW_init, /* init() method */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
71 RAW_quit, /* quit() method */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
72 RAW_open, /* open() method */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
73 RAW_close, /* close() method */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
74 RAW_read /* read() method */ |
4 | 75 }; |
76 | |
77 | |
47
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
78 static int RAW_init(void) |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
79 { |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
80 return(1); /* always succeeds. */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
81 } /* RAW_init */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
82 |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
83 |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
84 static void RAW_quit(void) |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
85 { |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
86 /* it's a no-op. */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
87 } /* RAW_quit */ |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
88 |
ea58bc3b15d7
Added init() and quit() methods.
Ryan C. Gordon <icculus@icculus.org>
parents:
7
diff
changeset
|
89 |
4 | 90 static int RAW_open(Sound_Sample *sample, const char *ext) |
91 { | |
7
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
92 /* |
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
93 * We check this explicitly, since we have no other way to |
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
94 * determine whether we should handle this data or not. |
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
95 */ |
4 | 96 if (__Sound_strcasecmp(ext, "RAW") != 0) |
7
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
97 { |
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
98 Sound_SetError("RAW: extension isn't explicitly \"RAW\"."); |
4 | 99 return(0); |
7
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
100 } /* if */ |
4 | 101 |
7
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
102 /* |
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
103 * You must also specify a desired format, so we know how to |
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
104 * treat the bits that are otherwise binary garbage. |
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
105 */ |
4 | 106 if ( (sample->desired.channels < 1) || |
107 (sample->desired.channels > 2) || | |
108 (sample->desired.rate == 0) || | |
109 (sample->desired.format == 0) ) | |
110 { | |
7
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
111 Sound_SetError("RAW: invalid desired format."); |
4 | 112 return(0); |
113 } /* if */ | |
114 | |
62
b13fafb976be
Changed _D macro to DBGSND.
Ryan C. Gordon <icculus@icculus.org>
parents:
47
diff
changeset
|
115 SNDDBG(("RAW: Accepting data stream.\n")); |
7
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
116 |
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
117 /* |
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
118 * We never convert raw samples; what you ask for is what you get. |
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
119 */ |
4 | 120 memcpy(&sample->actual, &sample->desired, sizeof (Sound_AudioInfo)); |
121 sample->flags = SOUND_SAMPLEFLAG_NONE; | |
7
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
122 |
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
123 return(1); /* we'll handle this data. */ |
4 | 124 } /* RAW_open */ |
125 | |
126 | |
127 static void RAW_close(Sound_Sample *sample) | |
128 { | |
7
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
129 /* we don't allocate anything that we need to free. That's easy, eh? */ |
4 | 130 } /* RAW_close */ |
131 | |
132 | |
7
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
133 static Uint32 RAW_read(Sound_Sample *sample) |
4 | 134 { |
7
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
135 Uint32 retval; |
4 | 136 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; |
7
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
137 |
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
138 /* |
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
139 * We don't actually do any decoding, so we read the raw data |
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
140 * directly into the internal buffer... |
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
141 */ |
4 | 142 retval = SDL_RWread(internal->rw, internal->buffer, |
143 1, internal->buffer_size); | |
144 | |
7
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
145 /* Make sure the read went smoothly... */ |
4 | 146 if (retval == 0) |
147 sample->flags |= SOUND_SAMPLEFLAG_EOF; | |
148 | |
149 else if (retval == -1) | |
150 sample->flags |= SOUND_SAMPLEFLAG_ERROR; | |
151 | |
7
29313c20963d
Updated a lot of comments, and changed read() method's return type.
Ryan C. Gordon <icculus@icculus.org>
parents:
4
diff
changeset
|
152 /* (next call this EAGAIN may turn into an EOF or error.) */ |
4 | 153 else if (retval < internal->buffer_size) |
154 sample->flags |= SOUND_SAMPLEFLAG_EAGAIN; | |
155 | |
156 return(retval); | |
157 } /* RAW_read */ | |
158 | |
159 | |
160 /* end of raw.c ... */ | |
161 |