annotate decoders/speex.c @ 493:38b656ccd029 stable-1.0

Updated.
author Ryan C. Gordon <icculus@icculus.org>
date Sat, 01 Oct 2005 15:35:53 +0000
parents 6bd7ca7d218b
children 1841bdcf2122 50bb9a6cebfe
rev   line source
451
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
1 /*
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
2 * SDL_sound -- An abstract sound format decoding API.
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
3 * Copyright (C) 2001 Ryan C. Gordon.
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
4 *
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
573eaefef003 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.
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
9 *
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
13 * Lesser General Public License for more details.
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
14 *
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
18 */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
19
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
20 /*
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
21 * Speex decoder for SDL_sound.
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
22 *
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
23 * This driver handles Speex audio data. Speex is a codec for speech that is
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
24 * meant to be transmitted over narrowband network connections. Epic Games
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
25 * estimates that their VoIP solution, built on top of Speex, uses around
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
26 * 500 bytes per second or less to transmit relatively good sounding speech.
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
27 *
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
28 * This decoder processes the .spx files that the speexenc program produces.
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
29 *
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
30 * Speex isn't meant for general audio compression. Something like Ogg Vorbis
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
31 * will give better results in that case.
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
32 *
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
33 * Further Speex information can be found at http://www.speex.org/
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
34 *
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
35 * This code is based on speexdec.c (see the Speex website).
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
36 *
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
37 * Please see the file COPYING in the source's root directory.
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
38 *
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
39 * This file written by Ryan C. Gordon. (icculus@clutteredmind.org)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
40 */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
41
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
42 #if HAVE_CONFIG_H
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
43 # include <config.h>
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
44 #endif
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
45
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
46 #ifdef SOUND_SUPPORTS_SPEEX
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
47
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
48 #include <stdio.h>
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
49 #include <stdlib.h>
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
50 #include <string.h>
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
51 #include <assert.h>
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
52
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
53 #include <ogg/ogg.h>
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
54 #include <speex.h>
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
55 #include <speex_header.h>
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
56
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
57 #include "SDL_sound.h"
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
58
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
59 #define __SDL_SOUND_INTERNAL__
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
60 #include "SDL_sound_internal.h"
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
61
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
62 static int SPEEX_init(void);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
63 static void SPEEX_quit(void);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
64 static int SPEEX_open(Sound_Sample *sample, const char *ext);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
65 static void SPEEX_close(Sound_Sample *sample);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
66 static Uint32 SPEEX_read(Sound_Sample *sample);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
67 static int SPEEX_rewind(Sound_Sample *sample);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
68 static int SPEEX_seek(Sound_Sample *sample, Uint32 ms);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
69
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
70 static const char *extensions_speex[] = { "spx", NULL };
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
71 const Sound_DecoderFunctions __Sound_DecoderFunctions_SPEEX =
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
72 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
73 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
74 extensions_speex,
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
75 "SPEEX speech compression format",
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
76 "Ryan C. Gordon <icculus@clutteredmind.org>",
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
77 "http://www.icculus.org/SDL_sound/"
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
78 },
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
79
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
80 SPEEX_init, /* init() method */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
81 SPEEX_quit, /* quit() method */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
82 SPEEX_open, /* open() method */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
83 SPEEX_close, /* close() method */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
84 SPEEX_read, /* read() method */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
85 SPEEX_rewind, /* rewind() method */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
86 SPEEX_seek /* seek() method */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
87 };
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
88
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
89 #define SPEEX_USE_PERCEPTUAL_ENHANCER 1
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
90 #define SPEEX_MAGIC 0x5367674F /* "OggS" in ASCII (littleendian) */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
91 #define SPEEX_OGG_BUFSIZE 200
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
92
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
93 /* this is what we store in our internal->decoder_private field... */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
94 typedef struct
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
95 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
96 ogg_sync_state oy;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
97 ogg_page og;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
98 ogg_packet op;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
99 ogg_stream_state os;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
100 void *state;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
101 SpeexBits bits;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
102 int header_count;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
103 int frame_size;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
104 int nframes;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
105 int frames_avail;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
106 float *decode_buf;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
107 int decode_total;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
108 int decode_pos;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
109 int have_ogg_packet;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
110 } speex_t;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
111
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
112
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
113 static int SPEEX_init(void)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
114 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
115 return(1); /* no-op. */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
116 } /* SPEEX_init */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
117
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
118
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
119 static void SPEEX_quit(void)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
120 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
121 /* no-op. */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
122 } /* SPEEX_quit */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
123
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
124
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
125 static int process_header(speex_t *speex, Sound_Sample *sample)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
126 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
127 SpeexMode *mode;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
128 SpeexHeader *hptr;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
129 SpeexHeader header;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
130 int enh_enabled = SPEEX_USE_PERCEPTUAL_ENHANCER;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
131 int tmp;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
132
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
133 hptr = speex_packet_to_header((char*) speex->op.packet, speex->op.bytes);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
134 BAIL_IF_MACRO(!hptr, "SPEEX: Cannot read header", 0);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
135 memcpy(&header, hptr, sizeof (SpeexHeader)); /* move to stack. */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
136 free(hptr); /* lame that this forces you to malloc... */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
137
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
138 BAIL_IF_MACRO(header.mode >= SPEEX_NB_MODES, "SPEEX: Unknown mode", 0);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
139 mode = speex_mode_list[header.mode];
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
140 BAIL_IF_MACRO(header.speex_version_id > 1, "SPEEX: Unknown version", 0);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
141 BAIL_IF_MACRO(mode->bitstream_version < header.mode_bitstream_version,
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
142 "SPEEX: Unsupported bitstream version", 0);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
143 BAIL_IF_MACRO(mode->bitstream_version > header.mode_bitstream_version,
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
144 "SPEEX: Unsupported bitstream version", 0);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
145
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
146 speex->state = speex_decoder_init(mode);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
147 BAIL_IF_MACRO(!speex->state, "SPEEX: Decoder initialization error", 0);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
148
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
149 speex_decoder_ctl(speex->state, SPEEX_SET_ENH, &enh_enabled);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
150 speex_decoder_ctl(speex->state, SPEEX_GET_FRAME_SIZE, &speex->frame_size);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
151
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
152 speex->decode_buf = (float *) malloc(speex->frame_size * sizeof (float));
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
153 BAIL_IF_MACRO(!speex->decode_buf, ERR_OUT_OF_MEMORY, 0);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
154
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
155 speex->nframes = header.frames_per_packet;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
156 if (!speex->nframes)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
157 speex->nframes = 1;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
158
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
159 /* !!! FIXME: Write converters to match desired format.
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
160 !!! FIXME: We have to convert from Float32 anyhow. */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
161 /* !!! FIXME: Is it a performance hit to alter sampling rate?
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
162 !!! FIXME: If not, try to match desired rate. */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
163 /* !!! FIXME: We force mono output, but speexdec.c has code for stereo.
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
164 !!! FIXME: Use that if sample->desired.channels == 2? */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
165 tmp = header.rate;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
166 speex_decoder_ctl(speex->state, SPEEX_SET_SAMPLING_RATE, &tmp);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
167 speex_decoder_ctl(speex->state, SPEEX_GET_SAMPLING_RATE, &tmp);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
168 sample->actual.rate = tmp;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
169 sample->actual.channels = 1;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
170 sample->actual.format = AUDIO_S16SYS;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
171
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
172 SNDDBG(("SPEEX: %dHz, mono, %svbr, %s mode.\n",
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
173 (int) sample->actual.rate,
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
174 header.vbr ? "" : "not ",
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
175 mode->modeName));
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
176
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
177 /* plus 2: one for this header, one for the comment header. */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
178 speex->header_count = header.extra_headers + 2;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
179 return(1);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
180 } /* process_header */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
181
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
182
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
183 /* !!! FIXME: this code sucks. */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
184 static int SPEEX_open(Sound_Sample *sample, const char *ext)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
185 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
186 int set_error_str = 1;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
187 int bitstream_initialized = 0;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
188 Uint8 *buffer = NULL;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
189 int packet_count = 0;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
190 speex_t *speex = NULL;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
191 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
192 SDL_RWops *rw = internal->rw;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
193 Uint32 magic;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
194
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
195 /* Quick rejection. */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
196 /*
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
197 * !!! FIXME: If (ext) is .spx, ignore bad magic number and assume
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
198 * !!! FIXME: this is a corrupted file...try to sync up further in
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
199 * !!! FIXME: stream. But for general purposes we can't read the
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
200 * !!! FIXME: whole RWops here in case it's not a Speex file at all.
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
201 */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
202 magic = SDL_ReadLE32(rw); /* make sure this is an ogg stream. */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
203 BAIL_IF_MACRO(magic != SPEEX_MAGIC, "SPEEX: Not a complete ogg stream", 0);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
204 BAIL_IF_MACRO(SDL_RWseek(rw, -4, SEEK_CUR) < 0, ERR_IO_ERROR, 0);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
205
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
206 speex = (speex_t *) malloc(sizeof (speex_t));
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
207 BAIL_IF_MACRO(speex == NULL, ERR_OUT_OF_MEMORY, 0);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
208 memset(speex, '\0', sizeof (speex_t));
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
209
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
210 speex_bits_init(&speex->bits);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
211 if (ogg_sync_init(&speex->oy) != 0) goto speex_open_failed;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
212
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
213 while (1)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
214 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
215 int rc;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
216 Uint8 *buffer = (Uint8*)ogg_sync_buffer(&speex->oy, SPEEX_OGG_BUFSIZE);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
217 if (buffer == NULL) goto speex_open_failed;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
218 rc = SDL_RWread(rw, buffer, 1, SPEEX_OGG_BUFSIZE);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
219 if (rc <= 0) goto speex_open_failed;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
220 if (ogg_sync_wrote(&speex->oy, rc) != 0) goto speex_open_failed;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
221 while (ogg_sync_pageout(&speex->oy, &speex->og) == 1)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
222 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
223 if (!bitstream_initialized)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
224 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
225 if (ogg_stream_init(&speex->os, ogg_page_serialno(&speex->og)))
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
226 goto speex_open_failed;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
227 bitstream_initialized = 1;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
228 } /* if */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
229
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
230 if (ogg_stream_pagein(&speex->os, &speex->og) != 0)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
231 goto speex_open_failed;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
232
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
233 while (ogg_stream_packetout(&speex->os, &speex->op) == 1)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
234 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
235 if (speex->op.e_o_s)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
236 goto speex_open_failed; /* end of stream already?! */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
237
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
238 packet_count++;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
239 if (packet_count == 1) /* need speex header. */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
240 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
241 if (!process_header(speex, sample))
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
242 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
243 set_error_str = 0; /* process_header will set error string. */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
244 goto speex_open_failed;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
245 } /* if */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
246 } /* if */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
247
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
248 if (packet_count > speex->header_count)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
249 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
250 /* if you made it here, you're ready to get a waveform. */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
251 SNDDBG(("SPEEX: Accepting data stream.\n"));
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
252
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
253 /* sample->actual is configured in process_header()... */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
254 speex->have_ogg_packet = 1;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
255 sample->flags = SOUND_SAMPLEFLAG_NONE;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
256 internal->decoder_private = speex;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
257 return(1); /* we'll handle this data. */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
258 } /* if */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
259 } /* while */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
260
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
261 } /* while */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
262
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
263 } /* while */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
264
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
265 assert(0); /* shouldn't hit this point. */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
266
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
267 speex_open_failed:
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
268 if (speex != NULL)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
269 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
270 if (speex->state != NULL)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
271 speex_decoder_destroy(speex->state);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
272 if (bitstream_initialized)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
273 ogg_stream_clear(&speex->os);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
274 speex_bits_destroy(&speex->bits);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
275 ogg_sync_clear(&speex->oy);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
276 free(speex->decode_buf);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
277 free(speex);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
278 } /* if */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
279
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
280 if (set_error_str)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
281 BAIL_MACRO("SPEEX: decoding error", 0);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
282
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
283 return(0);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
284 } /* SPEEX_open */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
285
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
286
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
287 static void SPEEX_close(Sound_Sample *sample)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
288 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
289 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
290 speex_t *speex = (speex_t *) internal->decoder_private;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
291 speex_decoder_destroy(speex->state);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
292 ogg_stream_clear(&speex->os);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
293 speex_bits_destroy(&speex->bits);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
294 ogg_sync_clear(&speex->oy);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
295 free(speex->decode_buf);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
296 free(speex);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
297 } /* SPEEX_close */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
298
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
299
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
300 static Uint32 copy_from_decoded(speex_t *speex,
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
301 Sound_SampleInternal *internal,
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
302 Uint32 _cpypos)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
303 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
304 /*
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
305 * !!! FIXME: Obviously, this all needs to change if we allow for
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
306 * !!! FIXME: more than mono, S16SYS data.
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
307 */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
308 Uint32 cpypos = _cpypos >> 1;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
309 Sint16 *dst = ((Sint16 *) internal->buffer) + cpypos;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
310 Sint16 *max;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
311 Uint32 maxoutput = (internal->buffer_size >> 1) - cpypos;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
312 Uint32 maxavail = speex->decode_total - speex->decode_pos;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
313 float *src = speex->decode_buf + speex->decode_pos;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
314
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
315 if (maxavail < maxoutput)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
316 maxoutput = maxavail;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
317
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
318 speex->decode_pos += maxoutput;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
319 cpypos += maxoutput;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
320
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
321 for (max = dst + maxoutput; dst < max; dst++, src++)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
322 {
454
6bd7ca7d218b Took out a C++-style comment.
Ryan C. Gordon <icculus@icculus.org>
parents: 451
diff changeset
323 /* !!! FIXME: This screams for vectorization. */
451
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
324 register float f = *src;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
325 if (f > 32000.0f) /* eh, speexdec.c clamps like this, too. */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
326 f = 32000.0f;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
327 else if (f < -32000.0f)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
328 f = -32000.0f;
454
6bd7ca7d218b Took out a C++-style comment.
Ryan C. Gordon <icculus@icculus.org>
parents: 451
diff changeset
329 *dst = (Sint16) (0.5f + f);
451
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
330 } /* for */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
331
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
332 return(cpypos << 1);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
333 } /* copy_from_decoded */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
334
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
335
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
336 /* !!! FIXME: this code sucks. */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
337 static Uint32 SPEEX_read(Sound_Sample *sample)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
338 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
339 Uint32 retval = 0;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
340 Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
341 speex_t *speex = (speex_t *) internal->decoder_private;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
342 SDL_RWops *rw = internal->rw;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
343 Uint8 *buffer;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
344 int rc;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
345
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
346 while (1)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
347 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
348 /* see if there's some already-decoded leftovers... */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
349 if (speex->decode_total != speex->decode_pos)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
350 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
351 retval = copy_from_decoded(speex, internal, retval);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
352 if (retval >= internal->buffer_size)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
353 return(retval); /* whee. */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
354 } /* if */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
355
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
356 /* okay, decoded buffer is spent. What else do we have? */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
357 speex->decode_total = speex->decode_pos = 0;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
358
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
359 if (speex->frames_avail) /* have more frames to decode? */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
360 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
361 rc = speex_decode(speex->state, &speex->bits, speex->decode_buf);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
362 if (rc < 0) goto speex_read_failed;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
363 if (speex_bits_remaining(&speex->bits) < 0) goto speex_read_failed;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
364 speex->frames_avail--;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
365 speex->decode_total = speex->frame_size;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
366 continue; /* go fill the output buffer... */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
367 } /* if */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
368
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
369 /* need to get more speex frames from available ogg packets... */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
370 if (speex->have_ogg_packet)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
371 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
372 speex_bits_read_from(&speex->bits,
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
373 (char *) speex->op.packet,
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
374 speex->op.bytes);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
375
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
376 speex->frames_avail += speex->nframes;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
377 if (ogg_stream_packetout(&speex->os, &speex->op) <= 0)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
378 speex->have_ogg_packet = 0;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
379 continue; /* go decode these frames. */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
380 } /* if */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
381
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
382 /* need to get more ogg packets from bitstream... */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
383
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
384 if (speex->op.e_o_s) /* okay, we're really spent. */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
385 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
386 sample->flags |= SOUND_SAMPLEFLAG_EOF;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
387 return(retval);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
388 } /* if */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
389
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
390 while ((!speex->op.e_o_s) && (!speex->have_ogg_packet))
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
391 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
392 buffer = (Uint8 *) ogg_sync_buffer(&speex->oy, SPEEX_OGG_BUFSIZE);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
393 if (buffer == NULL) goto speex_read_failed;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
394 rc = SDL_RWread(rw, buffer, 1, SPEEX_OGG_BUFSIZE);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
395 if (rc <= 0) goto speex_read_failed;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
396 if (ogg_sync_wrote(&speex->oy, rc) != 0) goto speex_read_failed;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
397
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
398 /* got complete ogg page? */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
399 if (ogg_sync_pageout(&speex->oy, &speex->og) == 1)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
400 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
401 if (ogg_stream_pagein(&speex->os, &speex->og) != 0)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
402 goto speex_read_failed;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
403
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
404 /* got complete ogg packet? */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
405 if (ogg_stream_packetout(&speex->os, &speex->op) == 1)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
406 speex->have_ogg_packet = 1;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
407 } /* if */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
408 } /* while */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
409 } /* while */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
410
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
411 assert(0); /* never hit this. Either return or goto speex_read_failed */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
412
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
413 speex_read_failed:
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
414 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
415 /* !!! FIXME: "i/o error" is better in some situations. */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
416 BAIL_MACRO("SPEEX: Decoding error", retval);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
417 } /* SPEEX_read */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
418
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
419
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
420 static int SPEEX_rewind(Sound_Sample *sample)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
421 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
422 /* !!! FIXME */ return(0);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
423 } /* SPEEX_rewind */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
424
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
425
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
426 static int SPEEX_seek(Sound_Sample *sample, Uint32 ms)
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
427 {
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
428 /* !!! FIXME */ return(0);
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
429 } /* SPEEX_seek */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
430
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
431
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
432 #endif /* SOUND_SUPPORTS_SPEEX */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
433
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
434 /* end of speex.c ... */
573eaefef003 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
435