summaryrefslogtreecommitdiffstats
path: root/uisimulator/sdl/sound.c
blob: 6068fb38635afae08498e6d2353afafaaf635315 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/***************************************************************************
 *             __________               __   ___.                  
 *   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();
}

void sound_playback_thread(void)
{
  int sndret = sim_sound_init();
  unsigned char *buf;
  long size;

  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 */