diff options
Diffstat (limited to 'uisimulator/sdl/sound.c')
-rw-r--r-- | uisimulator/sdl/sound.c | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/uisimulator/sdl/sound.c b/uisimulator/sdl/sound.c new file mode 100644 index 0000000000..6068fb3863 --- /dev/null +++ b/uisimulator/sdl/sound.c | |||
@@ -0,0 +1,119 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2005 by Nick Lanham | ||
11 | * | ||
12 | * All files in this archive are subject to the GNU General Public License. | ||
13 | * See the file COPYING in the source tree root for full license agreement. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
18 | ****************************************************************************/ | ||
19 | |||
20 | #include "autoconf.h" | ||
21 | |||
22 | #ifdef ROCKBOX_HAS_SIMSOUND /* play sound in sim enabled */ | ||
23 | |||
24 | #include <stdio.h> | ||
25 | #include <stdlib.h> | ||
26 | #include <unistd.h> | ||
27 | #include <fcntl.h> | ||
28 | #include <string.h> | ||
29 | #include <SDL.h> | ||
30 | |||
31 | #include "sound.h" | ||
32 | |||
33 | //static Uint8 *audio_chunk; | ||
34 | static int audio_len; | ||
35 | static char *audio_pos; | ||
36 | SDL_sem* sem; | ||
37 | |||
38 | /* The audio function callback takes the following parameters: | ||
39 | stream: A pointer to the audio buffer to be filled | ||
40 | len: The length (in bytes) of the audio buffer | ||
41 | */ | ||
42 | void mixaudio(void *udata, Uint8 *stream, int len) | ||
43 | { | ||
44 | (void)udata; | ||
45 | |||
46 | /* Only play if we have data left */ | ||
47 | if ( audio_len == 0 ) | ||
48 | return; | ||
49 | |||
50 | len = (len > audio_len) ? audio_len : len; | ||
51 | memcpy(stream, audio_pos, len); | ||
52 | audio_pos += len; | ||
53 | audio_len -= len; | ||
54 | |||
55 | if(audio_len == 0) { | ||
56 | if(SDL_SemPost(sem)) | ||
57 | fprintf(stderr,"Couldn't post: %s",SDL_GetError()); | ||
58 | |||
59 | } | ||
60 | } | ||
61 | |||
62 | |||
63 | |||
64 | int sim_sound_init(void) | ||
65 | { | ||
66 | SDL_AudioSpec fmt; | ||
67 | |||
68 | /* Set 16-bit stereo audio at 44Khz */ | ||
69 | fmt.freq = 44100; | ||
70 | fmt.format = AUDIO_S16SYS; | ||
71 | fmt.channels = 2; | ||
72 | fmt.samples = 512; /* A good value for games */ | ||
73 | fmt.callback = mixaudio; | ||
74 | fmt.userdata = NULL; | ||
75 | |||
76 | sem = SDL_CreateSemaphore(0); | ||
77 | |||
78 | /* Open the audio device and start playing sound! */ | ||
79 | if(SDL_OpenAudio(&fmt, NULL) < 0) { | ||
80 | fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError()); | ||
81 | return -1; | ||
82 | } | ||
83 | SDL_PauseAudio(0); | ||
84 | return 0; | ||
85 | |||
86 | //... | ||
87 | |||
88 | //SDL_CloseAudio(); | ||
89 | } | ||
90 | |||
91 | void sound_playback_thread(void) | ||
92 | { | ||
93 | int sndret = sim_sound_init(); | ||
94 | unsigned char *buf; | ||
95 | long size; | ||
96 | |||
97 | while(sndret) | ||
98 | sleep(100000); /* wait forever, can't play sound! */ | ||
99 | |||
100 | do { | ||
101 | while(!sound_get_pcm) | ||
102 | /* TODO: fix a fine thread-synch mechanism here */ | ||
103 | usleep(10000); | ||
104 | do { | ||
105 | sound_get_pcm(&buf, &size); | ||
106 | if(!size) { | ||
107 | sound_get_pcm = NULL; | ||
108 | break; | ||
109 | } | ||
110 | audio_pos = buf; // TODO: is this safe? | ||
111 | audio_len = size; | ||
112 | //printf("len: %i\n",audio_len); | ||
113 | if(SDL_SemWait(sem)) | ||
114 | fprintf(stderr,"Couldn't wait: %s",SDL_GetError()); | ||
115 | } while(size); | ||
116 | } while(1); | ||
117 | } | ||
118 | |||
119 | #endif /* ROCKBOX_HAS_SIMSOUND */ | ||