summaryrefslogtreecommitdiffstats
path: root/uisimulator/win32/sound.c
blob: 54140f59e41e11662afaccc9f2b696252c6aa60b (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/***************************************************************************
 *             __________               __   ___.                  
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___  
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /  
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <   
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \  
 *                     \/            \/     \/    \/            \/ 
 * $Id$
 *
 * Copyright (C) 2005 by Magnus Holmgren
 *
 * 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

#define WINDOWS_LEAN_AND_MEAN
#include <windows.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#include "sound.h"
#include "kernel.h"
#include "thread-win32.h"
#include "debug.h"

static bool playing = false;

void pcm_play_stop(void);

static void sound_play_chunk(HWAVEOUT wave_out, LPWAVEHDR header, 
    HANDLE event)
{
    unsigned char* buf;
    long len;

    if (!(header->dwFlags & WHDR_DONE) || !sound_get_pcm)
    {
        return;
    }

    EnterCriticalSection(&CriticalSection);
    sound_get_pcm(&buf, &len);
    LeaveCriticalSection(&CriticalSection);

    if (len == 0)
    {
        DEBUGF("simulator got no pcm\n");
        sound_get_pcm = NULL;
        return;
    }

    header->lpData = buf;
    header->dwBufferLength = len;
    header->dwBytesRecorded = 0;
    header->dwUser = 0;
    header->dwFlags = 0;
    header->dwLoops = 1;
    header->lpNext = NULL;
    header->reserved = 0;

    if (MMSYSERR_NOERROR != waveOutPrepareHeader(wave_out, header, 
        sizeof(*header)))
    {
        return;
    }

    ResetEvent(event);

    waveOutWrite(wave_out, header, sizeof(*header));
}

static int sound_init(LPHWAVEOUT wave_out, HANDLE event)
{
    static const WAVEFORMATEX format =
    {
        WAVE_FORMAT_PCM,    /* Format */
        2,                  /* Number of channels */
        44100,              /* Samples per second */
        44100 * 4,          /* Bytes per second */
        4,                  /* Block align */
        16,                 /* Bits per sample */
        0                   /* Extra size */
    };

    if (MMSYSERR_NOERROR != waveOutOpen(wave_out, WAVE_MAPPER, &format, 
        (DWORD_PTR) event, 0, CALLBACK_EVENT))
    {
        return -1;
    }

    /* Full volume on left and right */
    waveOutSetVolume(*wave_out, 0xffffffff);

    return 0;
}

void sound_playback_thread(void)
{
    /* To get smooth playback, two buffers are needed, which are queued for
     * playback. (There can still be glitches though.)
     */
    HWAVEOUT wave_out;
    WAVEHDR header1;
    WAVEHDR header2;
    HANDLE event;
    int result = -1;

    if ((event = CreateEvent(NULL, FALSE, FALSE, NULL)))
    {
        result = sound_init(&wave_out, event);
    }
    
    while(-1 == result || !event)
    {
        Sleep(100000); /* Wait forever, can't play sound! */
    }
    
    while (true)
    {
        while (!sound_get_pcm)
        {
            /* TODO: fix a fine thread-synch mechanism here */
            Sleep(100);
        }

        DEBUGF("starting simulator playback\n");
        header1.dwFlags = WHDR_DONE;
        header2.dwFlags = WHDR_DONE;
        sound_play_chunk(wave_out, &header1, event);
        sound_play_chunk(wave_out, &header2, event);

        while (sound_get_pcm 
            && (WAIT_FAILED != WaitForSingleObject(event, 1000)))
        {
            sound_play_chunk(wave_out, &header1, event);
            sound_play_chunk(wave_out, &header2, event);
        }
        
        pcm_play_stop();
        
        DEBUGF("stopping simulator playback\n");

        waveOutReset(wave_out);
    }
}


/* Stubs for PCM audio playback. */
bool pcm_is_playing(void)
{
    return playing;
}

void pcm_mute(bool state)
{
    (void)state;
}

void pcm_play_pause(bool state)
{
    (void)state;
}

bool pcm_is_paused(void)
{
    return false;
}

void pcm_play_stop(void)
{
    playing = false;
}

void pcm_init(void)
{
}

void (*sound_get_pcm)(unsigned char** start, long* size);
void pcm_play_data(void (*get_more)(unsigned char** start, long* size))
{
    sound_get_pcm = get_more;
    playing = true;
}

long pcm_get_bytes_waiting(void)
{
    return 0;
}


#endif /* ROCKBOX_HAS_SIMSOUND */