2006-01-09 11:22:36 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* 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 <stdlib.h>
|
2006-02-13 21:46:28 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <memory.h>
|
2006-03-11 10:22:20 +00:00
|
|
|
#include "debug.h"
|
2006-02-23 21:13:03 +00:00
|
|
|
#include "kernel.h"
|
2006-01-09 11:22:36 +00:00
|
|
|
#include "sound.h"
|
2006-02-13 21:46:28 +00:00
|
|
|
#include "SDL.h"
|
|
|
|
|
|
|
|
static bool pcm_playing;
|
|
|
|
static bool pcm_paused;
|
2006-01-09 11:22:36 +00:00
|
|
|
|
2006-02-13 21:46:28 +00:00
|
|
|
static Uint8* pcm_data;
|
2006-03-22 16:04:01 +00:00
|
|
|
static size_t pcm_data_size;
|
2006-01-09 11:22:36 +00:00
|
|
|
|
2006-02-21 21:48:06 +00:00
|
|
|
extern bool debug_audio;
|
|
|
|
|
2006-02-13 21:46:28 +00:00
|
|
|
static void sdl_dma_start(const void *addr, size_t size)
|
2006-01-09 11:22:36 +00:00
|
|
|
{
|
2006-02-13 21:46:28 +00:00
|
|
|
pcm_playing = true;
|
|
|
|
|
2006-02-21 21:48:06 +00:00
|
|
|
SDL_LockAudio();
|
|
|
|
|
2006-02-13 21:46:28 +00:00
|
|
|
pcm_data = (Uint8 *) addr;
|
|
|
|
pcm_data_size = size;
|
|
|
|
|
2006-02-21 21:48:06 +00:00
|
|
|
SDL_UnlockAudio();
|
|
|
|
|
2006-02-13 21:46:28 +00:00
|
|
|
SDL_PauseAudio(0);
|
2006-01-09 11:22:36 +00:00
|
|
|
}
|
|
|
|
|
2006-03-17 22:09:34 +00:00
|
|
|
static void sdl_dma_stop(void)
|
2006-01-09 11:22:36 +00:00
|
|
|
{
|
2006-02-13 21:46:28 +00:00
|
|
|
pcm_playing = false;
|
|
|
|
|
|
|
|
SDL_PauseAudio(1);
|
|
|
|
|
|
|
|
pcm_paused = false;
|
2006-01-09 11:22:36 +00:00
|
|
|
}
|
|
|
|
|
2006-02-13 21:46:28 +00:00
|
|
|
static void (*callback_for_more)(unsigned char**, size_t*) = NULL;
|
|
|
|
void pcm_play_data(void (*get_more)(unsigned char** start, size_t* size),
|
|
|
|
unsigned char* start, size_t size)
|
2006-01-09 11:22:36 +00:00
|
|
|
{
|
2006-02-13 21:46:28 +00:00
|
|
|
callback_for_more = get_more;
|
|
|
|
|
|
|
|
if (!(start && size)) {
|
|
|
|
if (get_more)
|
|
|
|
get_more(&start, &size);
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (start && size) {
|
|
|
|
sdl_dma_start(start, size);
|
|
|
|
}
|
|
|
|
}
|
2006-01-09 11:22:36 +00:00
|
|
|
|
2006-02-13 21:46:28 +00:00
|
|
|
size_t pcm_get_bytes_waiting(void)
|
|
|
|
{
|
|
|
|
return pcm_data_size;
|
|
|
|
}
|
2006-01-13 13:38:31 +00:00
|
|
|
|
2006-02-13 21:46:28 +00:00
|
|
|
void pcm_mute(bool mute)
|
|
|
|
{
|
|
|
|
(void) mute;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_play_stop(void)
|
|
|
|
{
|
|
|
|
if (pcm_playing) {
|
|
|
|
sdl_dma_stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_play_pause(bool play)
|
|
|
|
{
|
2006-03-22 16:04:01 +00:00
|
|
|
size_t next_size;
|
2006-02-13 21:46:28 +00:00
|
|
|
Uint8 *next_start;
|
|
|
|
|
|
|
|
if (!pcm_playing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pcm_paused && play) {
|
|
|
|
if (pcm_get_bytes_waiting()) {
|
|
|
|
printf("unpause\n");
|
|
|
|
|
|
|
|
SDL_PauseAudio(0);
|
|
|
|
} else {
|
|
|
|
printf("unpause, no data waiting\n");
|
|
|
|
|
|
|
|
void (*get_more)(unsigned char**, size_t*) = callback_for_more;
|
|
|
|
|
|
|
|
if (get_more) {
|
|
|
|
get_more(&next_start, &next_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (next_start && next_size) {
|
|
|
|
sdl_dma_start(next_start, next_size);
|
|
|
|
} else {
|
|
|
|
sdl_dma_stop();
|
|
|
|
printf("unpause attempted, no data\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if(!pcm_paused && !play) {
|
|
|
|
printf("pause\n");
|
|
|
|
|
|
|
|
SDL_PauseAudio(1);
|
|
|
|
}
|
2006-02-22 22:45:10 +00:00
|
|
|
|
2006-02-13 21:46:28 +00:00
|
|
|
pcm_paused = !play;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool pcm_is_paused(void)
|
|
|
|
{
|
|
|
|
return pcm_paused;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool pcm_is_playing(void)
|
|
|
|
{
|
|
|
|
return pcm_playing;
|
|
|
|
}
|
|
|
|
|
2007-01-01 14:01:08 +00:00
|
|
|
void pcm_set_frequency(unsigned int frequency)
|
|
|
|
{
|
|
|
|
/* To be implemented */
|
|
|
|
(void)frequency;
|
|
|
|
}
|
|
|
|
|
2006-02-23 21:13:03 +00:00
|
|
|
/*
|
|
|
|
* This function goes directly into the DMA buffer to calculate the left and
|
|
|
|
* right peak values. To avoid missing peaks it tries to look forward two full
|
|
|
|
* peek periods (2/HZ sec, 100% overlap), although it's always possible that
|
|
|
|
* the entire period will not be visible. To reduce CPU load it only looks at
|
|
|
|
* every third sample, and this can be reduced even further if needed (even
|
|
|
|
* every tenth sample would still be pretty accurate).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define PEAK_SAMPLES (44100*2/HZ) /* 44100 samples * 2 / 100 Hz tick */
|
|
|
|
#define PEAK_STRIDE 3 /* every 3rd sample is plenty... */
|
|
|
|
|
|
|
|
void pcm_calculate_peaks(int *left, int *right)
|
|
|
|
{
|
2006-02-23 21:42:53 +00:00
|
|
|
long samples = (long) pcm_data_size / 4;
|
|
|
|
short *addr = (short *) pcm_data;
|
2006-02-23 21:13:03 +00:00
|
|
|
|
|
|
|
if (samples > PEAK_SAMPLES)
|
|
|
|
samples = PEAK_SAMPLES;
|
|
|
|
|
|
|
|
samples /= PEAK_STRIDE;
|
|
|
|
|
|
|
|
if (left && right) {
|
|
|
|
int left_peak = 0, right_peak = 0, value;
|
|
|
|
|
|
|
|
while (samples--) {
|
|
|
|
if ((value = addr [0]) > left_peak)
|
|
|
|
left_peak = value;
|
|
|
|
else if (-value > left_peak)
|
|
|
|
left_peak = -value;
|
|
|
|
|
|
|
|
if ((value = addr [PEAK_STRIDE | 1]) > right_peak)
|
|
|
|
right_peak = value;
|
|
|
|
else if (-value > right_peak)
|
|
|
|
right_peak = -value;
|
|
|
|
|
|
|
|
addr += PEAK_STRIDE * 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
*left = left_peak;
|
|
|
|
*right = right_peak;
|
|
|
|
}
|
|
|
|
else if (left || right) {
|
|
|
|
int peak_value = 0, value;
|
|
|
|
|
|
|
|
if (right)
|
|
|
|
addr += (PEAK_STRIDE | 1);
|
|
|
|
|
|
|
|
while (samples--) {
|
|
|
|
if ((value = addr [0]) > peak_value)
|
|
|
|
peak_value = value;
|
|
|
|
else if (-value > peak_value)
|
|
|
|
peak_value = -value;
|
|
|
|
|
|
|
|
addr += PEAK_STRIDE * 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
*left = peak_value;
|
|
|
|
else
|
|
|
|
*right = peak_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-13 21:46:28 +00:00
|
|
|
void sdl_audio_callback(void *udata, Uint8 *stream, int len)
|
|
|
|
{
|
2006-03-11 10:22:20 +00:00
|
|
|
Uint32 have_now;
|
2006-02-21 21:48:06 +00:00
|
|
|
FILE *debug = (FILE *)udata;
|
2006-02-13 21:46:28 +00:00
|
|
|
|
2006-02-21 21:48:06 +00:00
|
|
|
/* At all times we need to write a full 'len' bytes to stream. */
|
2006-02-13 21:46:28 +00:00
|
|
|
|
2006-02-22 22:45:10 +00:00
|
|
|
if (pcm_data_size > 0) {
|
2006-03-11 10:22:20 +00:00
|
|
|
have_now = (((Uint32)len) > pcm_data_size) ? pcm_data_size : (Uint32)len;
|
2006-02-22 22:45:10 +00:00
|
|
|
|
2006-03-11 10:22:20 +00:00
|
|
|
memcpy(stream, pcm_data, have_now);
|
2006-02-13 21:46:28 +00:00
|
|
|
|
2006-02-21 21:48:06 +00:00
|
|
|
if (debug != NULL) {
|
2006-03-11 10:22:20 +00:00
|
|
|
fwrite(pcm_data, sizeof(Uint8), have_now, debug);
|
2006-02-21 21:48:06 +00:00
|
|
|
}
|
2006-03-11 10:22:20 +00:00
|
|
|
stream += have_now;
|
|
|
|
len -= have_now;
|
|
|
|
pcm_data += have_now;
|
|
|
|
pcm_data_size -= have_now;
|
|
|
|
}
|
2006-02-21 21:48:06 +00:00
|
|
|
|
2006-03-11 10:22:20 +00:00
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
if (callback_for_more) {
|
2006-02-22 22:45:10 +00:00
|
|
|
callback_for_more(&pcm_data, &pcm_data_size);
|
2006-03-11 10:22:20 +00:00
|
|
|
} else {
|
|
|
|
pcm_data = NULL;
|
|
|
|
pcm_data_size = 0;
|
|
|
|
}
|
|
|
|
if (pcm_data_size > 0) {
|
|
|
|
have_now = (((Uint32)len) > pcm_data_size) ? pcm_data_size : (Uint32)len;
|
2006-02-22 22:45:10 +00:00
|
|
|
|
2006-03-11 10:22:20 +00:00
|
|
|
memcpy(stream, pcm_data, have_now);
|
2006-02-22 22:45:10 +00:00
|
|
|
|
2006-03-11 10:22:20 +00:00
|
|
|
if (debug != NULL) {
|
|
|
|
fwrite(pcm_data, sizeof(Uint8), have_now, debug);
|
2006-02-22 22:45:10 +00:00
|
|
|
}
|
2006-03-11 10:22:20 +00:00
|
|
|
stream += have_now;
|
|
|
|
len -= have_now;
|
|
|
|
pcm_data += have_now;
|
|
|
|
pcm_data_size -= have_now;
|
|
|
|
} else {
|
|
|
|
DEBUGF("sdl_audio_callback: No Data.\n");
|
|
|
|
sdl_dma_stop();
|
|
|
|
break;
|
2006-02-21 21:48:06 +00:00
|
|
|
}
|
2006-02-22 22:45:10 +00:00
|
|
|
}
|
2006-02-13 21:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int pcm_init(void)
|
|
|
|
{
|
|
|
|
SDL_AudioSpec fmt;
|
2006-02-21 21:48:06 +00:00
|
|
|
FILE *debug = NULL;
|
|
|
|
|
|
|
|
if (debug_audio) {
|
|
|
|
debug = fopen("audiodebug.raw", "wb");
|
|
|
|
}
|
2006-02-13 21:46:28 +00:00
|
|
|
|
|
|
|
/* Set 16-bit stereo audio at 44Khz */
|
|
|
|
fmt.freq = 44100;
|
|
|
|
fmt.format = AUDIO_S16SYS;
|
|
|
|
fmt.channels = 2;
|
2006-02-22 22:45:10 +00:00
|
|
|
fmt.samples = 2048;
|
2006-02-13 21:46:28 +00:00
|
|
|
fmt.callback = sdl_audio_callback;
|
2006-02-21 21:48:06 +00:00
|
|
|
fmt.userdata = debug;
|
2006-02-13 21:46:28 +00:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
2006-02-21 21:48:06 +00:00
|
|
|
|
2006-02-13 21:46:28 +00:00
|
|
|
sdl_dma_stop();
|
2006-02-03 15:19:58 +00:00
|
|
|
|
2006-02-13 21:46:28 +00:00
|
|
|
return 0;
|
2006-01-09 11:22:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* ROCKBOX_HAS_SIMSOUND */
|
2006-02-03 15:19:58 +00:00
|
|
|
|