1 /* SDL Driver for P.E.Op.S Sound Plugin
2 * Copyright (c) 2010, Wei Mingzhi <whistler_wmz@users.sf.net>.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA
21 #include "externals.h"
24 #define BUFFER_SIZE 22050
26 short *pSndBuffer = NULL;
28 volatile int iReadPos = 0, iWritePos = 0;
30 static void SOUND_FillAudio(void *unused, Uint8 *stream, int len) {
31 short *p = (short *)stream;
35 while (iReadPos != iWritePos && len > 0) {
36 *p++ = pSndBuffer[iReadPos++];
37 if (iReadPos >= iBufSize) iReadPos = 0;
41 // Fill remaining space with zero
48 static void InitSDL() {
49 if (SDL_WasInit(SDL_INIT_EVERYTHING)) {
50 SDL_InitSubSystem(SDL_INIT_AUDIO);
52 SDL_Init(SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE);
56 static void DestroySDL() {
57 if (SDL_WasInit(SDL_INIT_EVERYTHING & ~SDL_INIT_AUDIO)) {
58 SDL_QuitSubSystem(SDL_INIT_AUDIO);
64 void SetupSound(void) {
67 if (pSndBuffer != NULL) return;
72 spec.format = AUDIO_S16SYS;
73 spec.channels = iDisStereo ? 1 : 2;
75 spec.callback = SOUND_FillAudio;
77 if (SDL_OpenAudio(&spec, NULL) < 0) {
82 iBufSize = BUFFER_SIZE;
83 if (iDisStereo) iBufSize /= 2;
85 pSndBuffer = (short *)malloc(iBufSize * sizeof(short));
86 if (pSndBuffer == NULL) {
97 void RemoveSound(void) {
98 if (pSndBuffer == NULL) return;
107 unsigned long SoundGetBytesBuffered(void) {
110 if (pSndBuffer == NULL) return SOUNDSIZE;
112 size = iReadPos - iWritePos;
113 if (size <= 0) size += iBufSize;
115 if (size < iBufSize / 2) return SOUNDSIZE;
120 void SoundFeedStreamData(unsigned char *pSound, long lBytes) {
121 short *p = (short *)pSound;
123 if (pSndBuffer == NULL) return;
126 if (((iWritePos + 1) % iBufSize) == iReadPos) break;
128 pSndBuffer[iWritePos] = *p++;
131 if (iWritePos >= iBufSize) iWritePos = 0;
133 lBytes -= sizeof(short);