49 |
|
#include "debug.h" |
50 |
|
|
51 |
|
|
52 |
< |
// Supported sample rates, sizes and channels |
52 |
> |
// Supported sample rates, sizes and channels (defaults) |
53 |
|
int audio_num_sample_rates = 1; |
54 |
|
uint32 audio_sample_rates[] = {44100 << 16}; |
55 |
|
int audio_num_sample_sizes = 1; |
67 |
|
static bool sem_inited = false; // Flag: audio_irq_done_sem initialized |
68 |
|
static int sound_buffer_size; // Size of sound buffer in bytes |
69 |
|
static bool little_endian = false; // Flag: DSP accepts only little-endian 16-bit sound data |
70 |
+ |
static uint8 silence_byte; // Byte value to use to fill sound buffers with silence |
71 |
|
static pthread_t stream_thread; // Audio streaming thread |
72 |
|
static pthread_attr_t stream_thread_attr; // Streaming thread attributes |
73 |
|
static bool stream_thread_active = false; // Flag: streaming thread installed |
81 |
|
* Initialization |
82 |
|
*/ |
83 |
|
|
84 |
+ |
// Set AudioStatus to reflect current audio stream format |
85 |
+ |
static void set_audio_status_format(void) |
86 |
+ |
{ |
87 |
+ |
AudioStatus.sample_rate = audio_sample_rates[0]; |
88 |
+ |
AudioStatus.sample_size = audio_sample_sizes[0]; |
89 |
+ |
AudioStatus.channels = audio_channel_counts[0]; |
90 |
+ |
} |
91 |
+ |
|
92 |
|
// Init using /dev/dsp, returns false on error |
93 |
|
bool audio_init_dsp(void) |
94 |
|
{ |
103 |
|
audio_fd = -1; |
104 |
|
return false; |
105 |
|
} |
106 |
< |
if (format & (AFMT_S16_BE | AFMT_S16_LE)) |
106 |
> |
if (format & (AFMT_S16_BE | AFMT_S16_LE)) { |
107 |
|
audio_sample_sizes[0] = 16; |
108 |
< |
else |
108 |
> |
silence_byte = 0; |
109 |
> |
} else { |
110 |
|
audio_sample_sizes[0] = 8; |
111 |
+ |
silence_byte = 0x80; |
112 |
+ |
} |
113 |
|
if (!(format & AFMT_S16_BE)) |
114 |
|
little_endian = true; |
115 |
|
|
116 |
|
// Set DSP parameters |
117 |
< |
format = AudioStatus.sample_size == 8 ? AFMT_U8 : (little_endian ? AFMT_S16_LE : AFMT_S16_BE); |
117 |
> |
format = audio_sample_sizes[0] == 8 ? AFMT_U8 : (little_endian ? AFMT_S16_LE : AFMT_S16_BE); |
118 |
|
ioctl(audio_fd, SNDCTL_DSP_SETFMT, &format); |
119 |
|
int frag = 0x0004000c; // Block size: 4096 frames |
120 |
|
ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag); |
121 |
< |
int stereo = (AudioStatus.channels == 2); |
121 |
> |
int stereo = (audio_channel_counts[0] == 2); |
122 |
|
ioctl(audio_fd, SNDCTL_DSP_STEREO, &stereo); |
123 |
< |
int rate = AudioStatus.sample_rate >> 16; |
123 |
> |
int rate = audio_sample_rates[0] >> 16; |
124 |
|
ioctl(audio_fd, SNDCTL_DSP_SPEED, &rate); |
125 |
+ |
audio_sample_rates[0] = rate << 16; |
126 |
+ |
|
127 |
+ |
// Set AudioStatus again because we now know more about the sound |
128 |
+ |
// system's capabilities |
129 |
+ |
set_audio_status_format(); |
130 |
|
|
131 |
|
// Get sound buffer size |
132 |
|
ioctl(audio_fd, SNDCTL_DSP_GETBLKSIZE, &audio_frames_per_block); |
157 |
|
#else |
158 |
|
little_endian = true; |
159 |
|
#endif |
160 |
+ |
silence_byte = 0; // Is this correct for 8-bit mode? |
161 |
|
|
162 |
|
// Open connection to ESD server |
163 |
|
audio_fd = esd_play_stream(format, AudioStatus.sample_rate >> 16, NULL, NULL); |
183 |
|
char str[256]; |
184 |
|
|
185 |
|
// Init audio status (defaults) and feature flags |
186 |
< |
AudioStatus.sample_rate = audio_sample_rates[0]; |
169 |
< |
AudioStatus.sample_size = audio_sample_sizes[0]; |
170 |
< |
AudioStatus.channels = audio_channel_counts[0]; |
186 |
> |
set_audio_status_format(); |
187 |
|
AudioStatus.mixer = 0; |
188 |
|
AudioStatus.num_sources = 0; |
189 |
|
audio_component_flags = cmpWantsRegisterMessage | kStereoOut | k16BitOut; |
293 |
|
{ |
294 |
|
int16 *silent_buffer = new int16[sound_buffer_size / 2]; |
295 |
|
int16 *last_buffer = new int16[sound_buffer_size / 2]; |
296 |
< |
memset(silent_buffer, 0, sound_buffer_size); |
296 |
> |
memset(silent_buffer, silence_byte, sound_buffer_size); |
297 |
|
|
298 |
|
while (!stream_thread_cancel) { |
299 |
|
if (AudioStatus.num_sources) { |
327 |
|
last_buffer[i] = ntohs(p[i]); |
328 |
|
} else |
329 |
|
memcpy(last_buffer, Mac2HostAddr(ReadMacInt32(apple_stream_info + scd_buffer)), work_size); |
330 |
< |
memset((uint8 *)last_buffer + work_size, 0, sound_buffer_size - work_size); |
330 |
> |
memset((uint8 *)last_buffer + work_size, silence_byte, sound_buffer_size - work_size); |
331 |
|
write(audio_fd, last_buffer, sound_buffer_size); |
332 |
|
} |
333 |
|
D(bug("stream: data written\n")); |