rockbox/uisimulator/sdl/sound.c
Dave Chapman bae4e2acdd Use SDL's thread wrappers instead of pthreads to increase portability in the SDL sim. Patch by Andrew Pilley with some changes by me.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@8347 a1c6a512-1295-4272-9138-f99709370657
2006-01-13 13:38:31 +00:00

121 lines
2.9 KiB
C

/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2005 by Nick Lanham
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "autoconf.h"
#ifdef ROCKBOX_HAS_SIMSOUND /* play sound in sim enabled */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <SDL.h>
#include "sound.h"
//static Uint8 *audio_chunk;
static int audio_len;
static char *audio_pos;
SDL_sem* sem;
/* The audio function callback takes the following parameters:
stream: A pointer to the audio buffer to be filled
len: The length (in bytes) of the audio buffer
*/
void mixaudio(void *udata, Uint8 *stream, int len)
{
(void)udata;
/* Only play if we have data left */
if ( audio_len == 0 )
return;
len = (len > audio_len) ? audio_len : len;
memcpy(stream, audio_pos, len);
audio_pos += len;
audio_len -= len;
if(audio_len == 0) {
if(SDL_SemPost(sem))
fprintf(stderr,"Couldn't post: %s",SDL_GetError());
}
}
int sim_sound_init(void)
{
SDL_AudioSpec fmt;
/* Set 16-bit stereo audio at 44Khz */
fmt.freq = 44100;
fmt.format = AUDIO_S16SYS;
fmt.channels = 2;
fmt.samples = 512; /* A good value for games */
fmt.callback = mixaudio;
fmt.userdata = NULL;
sem = SDL_CreateSemaphore(0);
/* Open the audio device and start playing sound! */
if(SDL_OpenAudio(&fmt, NULL) < 0) {
fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError());
return -1;
}
SDL_PauseAudio(0);
return 0;
//...
//SDL_CloseAudio();
}
int sound_playback_thread(void* p)
{
int sndret = sim_sound_init();
unsigned char *buf;
long size;
(void)p;
while(sndret)
sleep(100000); /* wait forever, can't play sound! */
do {
while(!sound_get_pcm)
/* TODO: fix a fine thread-synch mechanism here */
usleep(10000);
do {
sound_get_pcm(&buf, &size);
if(!size) {
sound_get_pcm = NULL;
break;
}
audio_pos = buf; // TODO: is this safe?
audio_len = size;
//printf("len: %i\n",audio_len);
if(SDL_SemWait(sem))
fprintf(stderr,"Couldn't wait: %s",SDL_GetError());
} while(size);
} while(1);
}
#endif /* ROCKBOX_HAS_SIMSOUND */