annotate decoders/voc.c @ 14:41e5e07c5fed

Initial add.
author Ryan C. Gordon <icculus@icculus.org>
date Tue, 18 Sep 2001 11:01:48 +0000
parents
children f6e679afe88b
rev   line source
14
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
1 /*
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
2 * SDL_sound -- An abstract sound format decoding API.
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
3 * Copyright (C) 2001 Ryan C. Gordon.
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
4 *
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
8 * version 2.1 of the License, or (at your option) any later version.
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
9 *
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
13 * Lesser General Public License for more details.
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
14 *
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
18 */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
19
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
20 /*
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
21 * VOC decoder for SDL_sound.
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
22 *
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
23 * This driver handles Creative Labs VOC audio data...this is a legacy format,
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
24 * but there's some game ports that could make use of such a decoder. Plus,
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
25 * VOC is fairly straightforward to decode, so this is a more complex, but
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
26 * still palatable example of an SDL_sound decoder. Y'know, in case the
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
27 * RAW decoder didn't do it for you. :)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
28 *
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
29 * This code was ripped from a decoder I had written for SDL_mixer, which was
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
30 * largely ripped from sox v12.17.1's voc.c.
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
31 *
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
32 * SDL_mixer: http://www.libsdl.org/projects/SDL_mixer/
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
33 * sox: http://www.freshmeat.net/projects/sox/
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
34 *
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
35 * Please see the file LICENSE in the source's root directory.
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
36 *
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
37 * This file written by Ryan C. Gordon. (icculus@clutteredmind.org)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
38 */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
39
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
40 #include <stdio.h>
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
41 #include <stdlib.h>
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
42 #include <string.h>
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
43 #include <assert.h>
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
44
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
45 #include "SDL.h"
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
46 #include "SDL_endian.h"
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
47 #include "SDL_sound.h"
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
48
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
49 #define __SDL_SOUND_INTERNAL__
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
50 #include "SDL_sound_internal.h"
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
51
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
52 #if (!defined SOUND_SUPPORTS_VOC)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
53 #error SOUND_SUPPORTS_VOC must be defined.
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
54 #endif
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
55
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
56
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
57 static int VOC_open(Sound_Sample *sample, const char *ext);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
58 static void VOC_close(Sound_Sample *sample);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
59 static Uint32 VOC_read(Sound_Sample *sample);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
60
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
61 const Sound_DecoderFunctions __Sound_DecoderFunctions_VOC =
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
62 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
63 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
64 "VOC",
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
65 "Creative Labs Voice format",
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
66 "Ryan C. Gordon <icculus@clutteredmind.org>",
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
67 "http://www.icculus.org/SDL_sound/"
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
68 },
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
69
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
70 VOC_open, /* open() method */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
71 VOC_close, /* close() method */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
72 VOC_read /* read() method */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
73 };
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
74
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
75
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
76 /* Private data for VOC file */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
77 typedef struct vocstuff {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
78 Uint32 rest; /* bytes remaining in current block */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
79 Uint32 rate; /* rate code (byte) of this chunk */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
80 int silent; /* sound or silence? */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
81 Uint32 srate; /* rate code (byte) of silence */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
82 Uint32 blockseek; /* start of current output block */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
83 Uint32 samples; /* number of samples output */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
84 Uint32 size; /* word length of data */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
85 int channels; /* number of sound channels */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
86 int extended; /* Has an extended block been read? */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
87 Uint32 bufpos; /* byte position in internal->buffer. */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
88 } vs_t;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
89
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
90 /* Size field */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
91 /* SJB: note that the 1st 3 are sometimes used as sizeof(type) */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
92 #define ST_SIZE_BYTE 1
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
93 #define ST_SIZE_8BIT 1
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
94 #define ST_SIZE_WORD 2
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
95 #define ST_SIZE_16BIT 2
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
96 #define ST_SIZE_DWORD 4
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
97 #define ST_SIZE_32BIT 4
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
98 #define ST_SIZE_FLOAT 5
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
99 #define ST_SIZE_DOUBLE 6
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
100 #define ST_SIZE_IEEE 7 /* IEEE 80-bit floats. */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
101
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
102 /* Style field */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
103 #define ST_ENCODING_UNSIGNED 1 /* unsigned linear: Sound Blaster */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
104 #define ST_ENCODING_SIGN2 2 /* signed linear 2's comp: Mac */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
105 #define ST_ENCODING_ULAW 3 /* U-law signed logs: US telephony, SPARC */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
106 #define ST_ENCODING_ALAW 4 /* A-law signed logs: non-US telephony */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
107 #define ST_ENCODING_ADPCM 5 /* Compressed PCM */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
108 #define ST_ENCODING_IMA_ADPCM 6 /* Compressed PCM */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
109 #define ST_ENCODING_GSM 7 /* GSM 6.10 33-byte frame lossy compression */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
110
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
111 #define VOC_TERM 0
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
112 #define VOC_DATA 1
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
113 #define VOC_CONT 2
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
114 #define VOC_SILENCE 3
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
115 #define VOC_MARKER 4
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
116 #define VOC_TEXT 5
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
117 #define VOC_LOOP 6
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
118 #define VOC_LOOPEND 7
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
119 #define VOC_EXTENDED 8
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
120 #define VOC_DATA_16 9
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
121
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
122
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
123 static __inline__ int voc_check_header(SDL_RWops *src)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
124 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
125 /* VOC magic header */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
126 Uint8 signature[20]; /* "Creative Voice File\032" */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
127 Uint16 datablockofs;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
128
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
129 if (SDL_RWread(src, signature, sizeof (signature), 1) != 1)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
130 return(0);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
131
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
132 if (memcmp(signature, "Creative Voice File\032", sizeof (signature)) != 0) {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
133 Sound_SetError("Unrecognized file type (not VOC)");
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
134 return(0);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
135 }
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
136
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
137 /* get the offset where the first datablock is located */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
138 if (SDL_RWread(src, &datablockofs, sizeof (Uint16), 1) != 1)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
139 return(0);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
140
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
141 datablockofs = SDL_SwapLE16(datablockofs);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
142
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
143 if (SDL_RWseek(src, datablockofs, SEEK_SET) != datablockofs)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
144 return(0);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
145
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
146 return(1); /* success! */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
147 } /* voc_check_header */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
148
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
149 /* !!! FIXME : Add a flag to vs_t that distinguishes EOF and Error conditions. */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
150
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
151 /* Read next block header, save info, leave position at start of data */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
152 static int voc_get_block(Sound_Sample *sample)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
153 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
154 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
155 SDL_RWops *src = internal->rw;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
156 vs_t *v = (vs_t *) internal->decoder_private;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
157 Uint8 bits24[3];
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
158 Uint8 uc, block;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
159 Uint32 sblen;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
160 Uint16 new_rate_short;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
161 Uint32 new_rate_long;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
162 Uint8 trash[6];
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
163 Uint16 period;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
164 int i;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
165
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
166 v->silent = 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
167 while (v->rest == 0)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
168 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
169 if (SDL_RWread(src, &block, sizeof (block), 1) != 1)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
170 return 1; /* assume that's the end of the file. */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
171
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
172 if (block == VOC_TERM)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
173 return 1;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
174
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
175 if (SDL_RWread(src, bits24, sizeof (bits24), 1) != 1)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
176 return 1; /* assume that's the end of the file. */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
177
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
178 /* Size is an 24-bit value. Ugh. */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
179 sblen = ( (bits24[0]) | (bits24[1] << 8) | (bits24[2] << 16) );
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
180
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
181 switch(block)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
182 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
183 case VOC_DATA:
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
184 if (SDL_RWread(src, &uc, sizeof (uc), 1) != 1)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
185 return 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
186
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
187 /* When DATA block preceeded by an EXTENDED */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
188 /* block, the DATA blocks rate value is invalid */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
189 if (!v->extended)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
190 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
191 if (uc == 0)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
192 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
193 Sound_SetError("VOC Sample rate is zero?");
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
194 return 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
195 }
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
196
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
197 if ((v->rate != -1) && (uc != v->rate))
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
198 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
199 Sound_SetError("VOC sample rate codes differ");
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
200 return 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
201 }
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
202
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
203 v->rate = uc;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
204 sample->actual.rate = 1000000.0/(256 - v->rate);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
205 v->channels = 1;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
206 }
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
207
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
208 if (SDL_RWread(src, &uc, sizeof (uc), 1) != 1)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
209 return 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
210
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
211 if (uc != 0)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
212 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
213 Sound_SetError("VOC decoder only interprets 8-bit data");
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
214 return 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
215 }
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
216
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
217 v->extended = 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
218 v->rest = sblen - 2;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
219 v->size = ST_SIZE_BYTE;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
220 return 1;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
221
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
222 case VOC_DATA_16:
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
223 if (SDL_RWread(src, &new_rate_long, sizeof (new_rate_long), 1) != 1)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
224 return 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
225 new_rate_long = SDL_SwapLE32(new_rate_long);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
226 if (new_rate_long == 0)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
227 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
228 Sound_SetError("VOC Sample rate is zero?");
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
229 return 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
230 }
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
231 if ((v->rate != -1) && (new_rate_long != v->rate))
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
232 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
233 Sound_SetError("VOC sample rate codes differ");
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
234 return 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
235 }
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
236 v->rate = new_rate_long;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
237 sample->actual.rate = new_rate_long;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
238
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
239 if (SDL_RWread(src, &uc, sizeof (uc), 1) != 1)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
240 return 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
241
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
242 switch (uc)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
243 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
244 case 8: v->size = ST_SIZE_BYTE; break;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
245 case 16: v->size = ST_SIZE_WORD; break;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
246 default:
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
247 Sound_SetError("VOC with unknown data size");
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
248 return 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
249 }
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
250
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
251 if (SDL_RWread(src, &v->channels, sizeof (Uint8), 1) != 1)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
252 return 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
253
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
254 if (SDL_RWread(src, trash, sizeof (Uint8), 6) != 6)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
255 return 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
256
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
257 v->rest = sblen - 12;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
258 return 1;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
259
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
260 case VOC_CONT:
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
261 v->rest = sblen;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
262 return 1;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
263
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
264 case VOC_SILENCE:
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
265 if (SDL_RWread(src, &period, sizeof (period), 1) != 1)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
266 return 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
267 period = SDL_SwapLE16(period);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
268
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
269 if (SDL_RWread(src, &uc, sizeof (uc), 1) != 1)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
270 return 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
271 if (uc == 0)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
272 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
273 Sound_SetError("VOC silence sample rate is zero");
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
274 return 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
275 }
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
276
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
277 /*
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
278 * Some silence-packed files have gratuitously
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
279 * different sample rate codes in silence.
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
280 * Adjust period.
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
281 */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
282 if ((v->rate != -1) && (uc != v->rate))
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
283 period = (period * (256 - uc))/(256 - v->rate);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
284 else
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
285 v->rate = uc;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
286 v->rest = period;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
287 v->silent = 1;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
288 return 1;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
289
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
290 case VOC_LOOP:
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
291 case VOC_LOOPEND:
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
292 for(i = 0; i < sblen; i++) /* skip repeat loops. */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
293 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
294 if (SDL_RWread(src, trash, sizeof (Uint8), 1) != 1)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
295 return 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
296 }
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
297 break;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
298
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
299 case VOC_EXTENDED:
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
300 /* An Extended block is followed by a data block */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
301 /* Set this byte so we know to use the rate */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
302 /* value from the extended block and not the */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
303 /* data block. */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
304 v->extended = 1;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
305 if (SDL_RWread(src, &new_rate_short, sizeof (new_rate_short), 1) != 1)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
306 return 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
307 new_rate_short = SDL_SwapLE16(new_rate_short);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
308 if (new_rate_short == 0)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
309 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
310 Sound_SetError("VOC sample rate is zero");
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
311 return 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
312 }
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
313 if ((v->rate != -1) && (new_rate_short != v->rate))
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
314 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
315 Sound_SetError("VOC sample rate codes differ");
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
316 return 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
317 }
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
318 v->rate = new_rate_short;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
319
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
320 if (SDL_RWread(src, &uc, sizeof (uc), 1) != 1)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
321 return 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
322
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
323 if (uc != 0)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
324 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
325 Sound_SetError("VOC decoder only interprets 8-bit data");
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
326 return 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
327 }
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
328
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
329 if (SDL_RWread(src, &uc, sizeof (uc), 1) != 1)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
330 return 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
331
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
332 if (uc)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
333 sample->actual.channels = 2; /* Stereo */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
334 /* Needed number of channels before finishing
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
335 compute for rate */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
336 sample->actual.rate =
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
337 (256000000L/(65536L - v->rate)) / sample->actual.channels;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
338 /* An extended block must be followed by a data */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
339 /* block to be valid so loop back to top so it */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
340 /* can be grabed. */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
341 continue;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
342
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
343 case VOC_MARKER:
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
344 if (SDL_RWread(src, trash, sizeof (Uint8), 2) != 2)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
345 return 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
346
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
347 /* Falling! Falling! */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
348
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
349 default: /* text block or other krapola. */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
350 for(i = 0; i < sblen; i++)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
351 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
352 if (SDL_RWread(src, &trash, sizeof (Uint8), 1) != 1)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
353 return 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
354 }
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
355
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
356 if (block == VOC_TEXT)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
357 continue; /* get next block */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
358 }
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
359 }
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
360
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
361 return 1;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
362 }
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
363
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
364
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
365 static int voc_read(Sound_Sample *sample)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
366 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
367 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
368 SDL_RWops *src = internal->rw;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
369 vs_t *v = (vs_t *) internal->decoder_private;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
370 int done = 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
371 Uint8 silence = 0x80;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
372 Uint8 *buf = internal->buffer;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
373
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
374 if (v->rest == 0)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
375 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
376 if (!voc_get_block(sample))
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
377 return 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
378 }
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
379
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
380 if (v->rest == 0)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
381 return 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
382
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
383 if (v->silent)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
384 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
385 if (v->size == ST_SIZE_WORD)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
386 silence = 0x00;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
387
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
388 /* Fill in silence */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
389 memset(buf, silence, v->rest);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
390 done = v->rest;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
391 v->rest = 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
392 }
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
393
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
394 else
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
395 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
396 Uint32 max = (v->rest < internal->buffer_size) ?
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
397 v->rest : internal->buffer_size;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
398 done = SDL_RWread(src, buf + v->bufpos, 1, max);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
399 v->rest -= done;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
400 v->bufpos += done;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
401 if (v->size == ST_SIZE_WORD)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
402 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
403 #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
404 for (; v->rest > 0; v->rest -= 2)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
405 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
406 *((Uint16 *) buf) = SDL_SwapLE16(*((Uint16 *) buf));
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
407 ((Uint16 *) buf)++;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
408 }
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
409 #endif
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
410 }
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
411 }
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
412
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
413 return done;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
414 } /* voc_read */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
415
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
416
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
417 static int VOC_open(Sound_Sample *sample, const char *ext)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
418 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
419 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
420 vs_t *v = NULL;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
421
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
422 if (!voc_check_header(internal->rw))
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
423 return(0);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
424
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
425 v = (vs_t *) malloc(sizeof (vs_t));
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
426 BAIL_IF_MACRO(v == NULL, ERR_OUT_OF_MEMORY, 0);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
427 memset(v, '\0', sizeof (vs_t));
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
428 internal->decoder_private = v;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
429
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
430 v->rate = -1;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
431 if (!voc_get_block(sample))
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
432 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
433 free(v);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
434 return(0);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
435 } /* if */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
436
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
437 if (v->rate == -1)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
438 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
439 Sound_SetError("VOC data had no sound!");
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
440 free(v);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
441 return(0);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
442 } /* if */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
443
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
444 _D(("VOC: Accepting data stream.\n"));
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
445 sample->actual.format = (v->size == ST_SIZE_WORD) ? AUDIO_S16LSB:AUDIO_U8;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
446 sample->actual.channels = v->channels;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
447 sample->flags = SOUND_SAMPLEFLAG_NONE;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
448 return(1);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
449 } /* VOC_open */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
450
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
451
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
452 static void VOC_close(Sound_Sample *sample)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
453 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
454 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
455 free(internal->decoder_private);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
456 } /* VOC_close */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
457
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
458
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
459 static Uint32 VOC_read(Sound_Sample *sample)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
460 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
461 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
462 vs_t *v = (vs_t *) internal->decoder_private;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
463
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
464 v->bufpos = 0;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
465 while (v->bufpos < internal->buffer_size)
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
466 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
467 Uint32 rc = voc_read(sample);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
468 if (rc == 0) /* !!! FIXME: Could be an error... */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
469 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
470 sample->flags |= SOUND_SAMPLEFLAG_EOF;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
471 break;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
472 } /* if */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
473
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
474 if (!voc_get_block(sample))
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
475 {
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
476 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
477 break;
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
478 } /* if */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
479 } /* while */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
480
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
481 return(v->bufpos);
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
482 } /* VOC_read */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
483
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
484
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
485 /* end of voc.c ... */
41e5e07c5fed Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
486