Daniel Stenberg | febb52f | 2005-07-14 21:46:07 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2005 by Daniel Stenberg <daniel@haxx.se> |
| 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 | |
Dan Everton | 3ba0060 | 2006-02-13 21:46:28 +0000 | [diff] [blame] | 24 | #include <stdbool.h> |
Daniel Stenberg | febb52f | 2005-07-14 21:46:07 +0000 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <unistd.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <sys/ioctl.h> |
Daniel Stenberg | 741a715 | 2005-07-15 21:12:18 +0000 | [diff] [blame] | 31 | #include <sys/soundcard.h> |
Daniel Stenberg | febb52f | 2005-07-14 21:46:07 +0000 | [diff] [blame] | 32 | |
| 33 | #include "sound.h" |
| 34 | |
Dan Everton | 3ba0060 | 2006-02-13 21:46:28 +0000 | [diff] [blame] | 35 | static bool playing = false; |
| 36 | |
Daniel Stenberg | febb52f | 2005-07-14 21:46:07 +0000 | [diff] [blame] | 37 | int sim_sound_init(void) |
| 38 | { |
| 39 | int fd; |
| 40 | int pcmbits; |
| 41 | int rc; |
| 42 | int channels; |
| 43 | int rate; |
| 44 | |
Stepan Moskovchenko | 2be160a | 2005-08-07 22:26:42 +0000 | [diff] [blame] | 45 | fd = open("/dev/dsp", O_WRONLY); |
Daniel Stenberg | febb52f | 2005-07-14 21:46:07 +0000 | [diff] [blame] | 46 | if(-1 == fd) |
| 47 | return 1; |
| 48 | |
| 49 | pcmbits = 16; |
| 50 | rc = ioctl(fd, SOUND_PCM_WRITE_BITS, &pcmbits); |
| 51 | rc = ioctl(fd, SOUND_PCM_READ_BITS, &pcmbits); |
| 52 | |
| 53 | channels = 2; /* Number of channels, 1=mono */ |
| 54 | rc = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &channels); |
| 55 | rc = ioctl(fd, SOUND_PCM_READ_CHANNELS, &channels); |
| 56 | |
| 57 | rate = 44100; /* Yeah. sampling rate */ |
| 58 | rc = ioctl(fd, SOUND_PCM_WRITE_RATE, &rate); |
| 59 | rc = ioctl(fd, SOUND_PCM_READ_RATE, &rate); |
| 60 | |
| 61 | return fd; |
| 62 | } |
| 63 | |
| 64 | void sim_sound_play(int soundfd, char *buffer, long len) |
| 65 | { |
| 66 | write(soundfd, buffer, len); |
| 67 | } |
| 68 | |
| 69 | void sound_playback_thread(void) |
| 70 | { |
| 71 | int soundfd = sim_sound_init(); |
| 72 | unsigned char *buf; |
| 73 | long size; |
| 74 | |
| 75 | while(-1 == soundfd) |
| 76 | sleep(100000); /* wait forever, can't play sound! */ |
| 77 | |
| 78 | do { |
| 79 | |
| 80 | while(!sound_get_pcm) |
| 81 | /* TODO: fix a fine thread-synch mechanism here */ |
| 82 | usleep(10000); |
| 83 | |
| 84 | do { |
| 85 | sound_get_pcm(&buf, &size); |
| 86 | if(!size) { |
| 87 | sound_get_pcm = NULL; |
| 88 | break; |
| 89 | } |
Daniel Stenberg | ee76cc3 | 2005-11-28 09:36:26 +0000 | [diff] [blame] | 90 | sim_sound_play(soundfd, (char *)buf, size); |
Daniel Stenberg | febb52f | 2005-07-14 21:46:07 +0000 | [diff] [blame] | 91 | usleep(10000); |
| 92 | } while(size); |
| 93 | |
| 94 | } while(1); |
| 95 | |
| 96 | } |
| 97 | |
Dan Everton | 3ba0060 | 2006-02-13 21:46:28 +0000 | [diff] [blame] | 98 | /* Stubs for PCM audio playback. */ |
| 99 | bool pcm_is_playing(void) |
| 100 | { |
| 101 | return playing; |
| 102 | } |
| 103 | |
| 104 | void pcm_mute(bool state) |
| 105 | { |
| 106 | (void)state; |
| 107 | } |
| 108 | |
| 109 | void pcm_play_pause(bool state) |
| 110 | { |
| 111 | (void)state; |
| 112 | } |
| 113 | |
| 114 | bool pcm_is_paused(void) |
| 115 | { |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | void pcm_play_stop(void) |
| 120 | { |
| 121 | playing = false; |
| 122 | } |
| 123 | |
| 124 | void pcm_init(void) |
| 125 | { |
| 126 | } |
| 127 | |
| 128 | void (*sound_get_pcm)(unsigned char** start, long* size); |
| 129 | void pcm_play_data(void (*get_more)(unsigned char** start, long* size)) |
| 130 | { |
| 131 | sound_get_pcm = get_more; |
| 132 | playing = true; |
| 133 | } |
| 134 | |
| 135 | long pcm_get_bytes_waiting(void) |
| 136 | { |
| 137 | return 0; |
| 138 | } |
| 139 | |
Daniel Stenberg | febb52f | 2005-07-14 21:46:07 +0000 | [diff] [blame] | 140 | #endif /* ROCKBOX_HAS_SIMSOUND */ |