From ebe651761b6c02b0e0e3fb01c37a1378be860a70 Mon Sep 17 00:00:00 2001 From: Domppari Date: Sat, 3 Jan 2026 21:24:33 +0200 Subject: [PATCH] Initial HDD sound implementation using IBM example drive --- src/disk/CMakeLists.txt | 1 + src/disk/hdd.c | 6 +- src/disk/hdd_audio.c | 232 +++++++++++++++++++++++++++++++++ src/floppy/fdd_audio.c | 108 ++------------- src/include/86box/fdd_audio.h | 17 --- src/include/86box/hdd_audio.h | 6 + src/include/86box/sound.h | 4 + src/include/86box/sound_util.h | 28 ++++ src/sound/CMakeLists.txt | 1 + src/sound/audio4.c | 20 ++- src/sound/openal.c | 58 ++++++--- src/sound/sndio.c | 14 +- src/sound/sound.c | 67 ++++++++++ src/sound/sound_util.c | 85 ++++++++++++ src/sound/xaudio2.c | 19 +++ 15 files changed, 523 insertions(+), 143 deletions(-) create mode 100644 src/disk/hdd_audio.c create mode 100644 src/include/86box/hdd_audio.h create mode 100644 src/include/86box/sound_util.h create mode 100644 src/sound/sound_util.c diff --git a/src/disk/CMakeLists.txt b/src/disk/CMakeLists.txt index 9b8d72f54..fff17a5e4 100644 --- a/src/disk/CMakeLists.txt +++ b/src/disk/CMakeLists.txt @@ -36,6 +36,7 @@ add_library(hdd OBJECT hdc_ide_sff8038i.c hdc_ide_um8673f.c hdc_ide_w83769f.c + hdd_audio.c ) add_library(rdisk OBJECT rdisk.c) diff --git a/src/disk/hdd.c b/src/disk/hdd.c index 357c787b8..f6d769b9e 100644 --- a/src/disk/hdd.c +++ b/src/disk/hdd.c @@ -27,6 +27,7 @@ #include <86box/hdd.h> #include <86box/cdrom.h> #include <86box/video.h> +#include <86box/hdd_audio.h> #include "cpu.h" #define HDD_OVERHEAD_TIME 50.0 @@ -38,7 +39,7 @@ hdd_init(void) { /* Clear all global data. */ memset(hdd, 0x00, sizeof(hdd)); - + hdd_audio_init(); return 0; } @@ -196,6 +197,9 @@ hdd_seek_get_time(hard_disk_t *hdd, uint32_t dst_addr, uint8_t operation, uint8_ } if (!max_seek_time || seek_time <= max_seek_time) { + if (new_cylinder != hdd->cur_cylinder) + hdd_audio_seek(hdd, new_cylinder); + hdd->cur_addr = dst_addr; hdd->cur_track = new_track; hdd->cur_cylinder = new_cylinder; diff --git a/src/disk/hdd_audio.c b/src/disk/hdd_audio.c new file mode 100644 index 000000000..54419c244 --- /dev/null +++ b/src/disk/hdd_audio.c @@ -0,0 +1,232 @@ +#include +#include +#include +#include <86box/86box.h> +#include <86box/hdd.h> +#include <86box/sound.h> +#include <86box/sound_util.h> +#include <86box/thread.h> + +/* Maximum number of simultaneous seek sounds */ +#define HDD_MAX_SEEK_VOICES 8 + +typedef struct { + int active; + int position; + float volume; +} hdd_seek_voice_t; + +static int16_t *hdd_spindle_sound_buffer = NULL; +static int16_t *hdd_seek_sound_buffer = NULL; +static int hdd_samples; +static int hdd_seek_samples; + +static hdd_seek_voice_t hdd_seek_voices[HDD_MAX_SEEK_VOICES]; +static mutex_t *hdd_audio_mutex = NULL; + +void +hdd_audio_init(void) +{ + hdd_spindle_sound_buffer = sound_load_wav( + "assets/sounds/hdd/1993 IBM H3171/1993_IBM_H3171_3.5_SPINDLE_RUNNING.wav", + &hdd_samples); + + if (hdd_spindle_sound_buffer) { + pclog("HDD Audio: Loaded spindle sound, %d frames\n", hdd_samples); + } else { + pclog("HDD Audio: Failed to load spindle sound!\n"); + } + + hdd_seek_sound_buffer = sound_load_wav( + "assets/sounds/hdd/1993 IBM H3171/1993_IBM_H3171_3.5_SEEK_1TRACK.wav", + &hdd_seek_samples); + + if (hdd_seek_sound_buffer) { + pclog("HDD Audio: Loaded seek sound, %d frames (%.1f ms)\n", + hdd_seek_samples, (float)hdd_seek_samples / 48.0f); + } else { + pclog("HDD Audio: Failed to load seek sound!\n"); + } + + for (int i = 0; i < HDD_MAX_SEEK_VOICES; i++) { + hdd_seek_voices[i].active = 0; + hdd_seek_voices[i].position = 0; + hdd_seek_voices[i].volume = 1.0f; + } + + hdd_audio_mutex = thread_create_mutex(); + + sound_hdd_thread_init(); +} + +void +hdd_audio_seek(hard_disk_t *hdd, uint32_t new_cylinder) +{ + uint32_t cylinder_diff = abs((int) hdd->cur_cylinder - (int) new_cylinder); + + if (cylinder_diff == 0) + return; + + pclog("HDD Audio Seek: cur_cyl=%u -> new_cyl=%u, diff=%u, speed_preset=%d, cyl_switch_usec=%.1f\n", + hdd->cur_cylinder, new_cylinder, cylinder_diff, hdd->speed_preset, + hdd->cyl_switch_usec); + + if (hdd->speed_preset == 0) + return; + + if (!hdd_seek_sound_buffer || hdd_seek_samples == 0) { + pclog("HDD Audio Seek: No seek sound buffer loaded!\n"); + return; + } + + int min_seek_spacing = 0; + if (hdd->cyl_switch_usec > 0) + min_seek_spacing = (int)(hdd->cyl_switch_usec * 48000.0 / 1000000.0); + + thread_wait_mutex(hdd_audio_mutex); + + for (int i = 0; i < HDD_MAX_SEEK_VOICES; i++) { + if (hdd_seek_voices[i].active) { + int pos = hdd_seek_voices[i].position; + if (pos >= 0 && pos < min_seek_spacing) { + thread_release_mutex(hdd_audio_mutex); + return; + } + } + } + + int active_count = 0; + + for (int i = 0; i < HDD_MAX_SEEK_VOICES; i++) { + if (!hdd_seek_voices[i].active) { + hdd_seek_voices[i].active = 1; + hdd_seek_voices[i].position = 0; + hdd_seek_voices[i].volume = 0.5f; + pclog("HDD Audio Seek: Using free voice %d\n", i); + thread_release_mutex(hdd_audio_mutex); + return; + } + active_count++; + } + + pclog("HDD Audio Seek: All %d voices active, skipping seek\n", active_count); + + thread_release_mutex(hdd_audio_mutex); +} + +void +hdd_audio_callback(int16_t *buffer, int length) +{ + static int spindle_pos = 0; + static int debug_counter = 0; + const float spindle_volume = 0.2f; + const float seek_volume = 0.5f; + int frames_in_buffer = length / 2; + + if (sound_is_float) { + float *float_buffer = (float *) buffer; + + if (hdd_spindle_sound_buffer && hdd_samples > 0) { + for (int i = 0; i < frames_in_buffer; i++) { + float left_sample = (float) hdd_spindle_sound_buffer[spindle_pos * 2] / 131072.0f * spindle_volume; + float right_sample = (float) hdd_spindle_sound_buffer[spindle_pos * 2 + 1] / 131072.0f * spindle_volume; + float_buffer[i * 2] = left_sample; + float_buffer[i * 2 + 1] = right_sample; + + spindle_pos++; + if (spindle_pos >= hdd_samples) { + spindle_pos = 0; + } + } + } else { + for (int i = 0; i < length; i++) { + float_buffer[i] = 0.0f; + } + } + + if (hdd_seek_sound_buffer && hdd_seek_samples > 0 && hdd_audio_mutex) { + thread_wait_mutex(hdd_audio_mutex); + + for (int v = 0; v < HDD_MAX_SEEK_VOICES; v++) { + if (!hdd_seek_voices[v].active) + continue; + + float voice_vol = hdd_seek_voices[v].volume; + int pos = hdd_seek_voices[v].position; + if (pos < 0) pos = 0; + + for (int i = 0; i < frames_in_buffer && pos < hdd_seek_samples; i++, pos++) { + float seek_left = (float) hdd_seek_sound_buffer[pos * 2] / 131072.0f * seek_volume * voice_vol; + float seek_right = (float) hdd_seek_sound_buffer[pos * 2 + 1] / 131072.0f * seek_volume * voice_vol; + + float_buffer[i * 2] += seek_left; + float_buffer[i * 2 + 1] += seek_right; + } + + if (pos >= hdd_seek_samples) { + hdd_seek_voices[v].active = 0; + hdd_seek_voices[v].position = 0; + } else { + hdd_seek_voices[v].position = pos; + } + } + + thread_release_mutex(hdd_audio_mutex); + } + + if (debug_counter++ % 100 == 0) { + pclog("HDD Audio: float_buffer[0]=%.6f, float_buffer[1]=%.6f, spindle_pos=%d, frames=%d\n", + float_buffer[0], float_buffer[1], spindle_pos, frames_in_buffer); + } + } else { + if (hdd_spindle_sound_buffer && hdd_samples > 0) { + for (int i = 0; i < frames_in_buffer; i++) { + buffer[i * 2] = hdd_spindle_sound_buffer[spindle_pos * 2]; + buffer[i * 2 + 1] = hdd_spindle_sound_buffer[spindle_pos * 2 + 1]; + + spindle_pos++; + if (spindle_pos >= hdd_samples) { + spindle_pos = 0; + } + } + } else { + for (int i = 0; i < length; i++) { + buffer[i] = 0; + } + } + + if (hdd_seek_sound_buffer && hdd_seek_samples > 0 && hdd_audio_mutex) { + thread_wait_mutex(hdd_audio_mutex); + + for (int v = 0; v < HDD_MAX_SEEK_VOICES; v++) { + if (!hdd_seek_voices[v].active) + continue; + + int pos = hdd_seek_voices[v].position; + if (pos < 0) pos = 0; + + for (int i = 0; i < frames_in_buffer && pos < hdd_seek_samples; i++, pos++) { + int32_t left = buffer[i * 2] + hdd_seek_sound_buffer[pos * 2] / 2; + int32_t right = buffer[i * 2 + 1] + hdd_seek_sound_buffer[pos * 2 + 1] / 2; + + if (left > 32767) left = 32767; + if (left < -32768) left = -32768; + if (right > 32767) right = 32767; + if (right < -32768) right = -32768; + + buffer[i * 2] = (int16_t) left; + buffer[i * 2 + 1] = (int16_t) right; + } + + if (pos >= hdd_seek_samples) { + hdd_seek_voices[v].active = 0; + hdd_seek_voices[v].position = 0; + } else { + hdd_seek_voices[v].position = pos; + } + } + + thread_release_mutex(hdd_audio_mutex); + } + } +} \ No newline at end of file diff --git a/src/floppy/fdd_audio.c b/src/floppy/fdd_audio.c index 8cbebf209..62e1881ef 100644 --- a/src/floppy/fdd_audio.c +++ b/src/floppy/fdd_audio.c @@ -31,6 +31,7 @@ #include <86box/plat.h> #include <86box/path.h> #include <86box/ini.h> +#include <86box/sound_util.h> #ifndef DISABLE_FDD_AUDIO @@ -53,9 +54,6 @@ static multi_seek_state_t seek_state[FDD_NUM][MAX_CONCURRENT_SEEKS] = {}; extern uint64_t motoron[FDD_NUM]; extern char exe_path[2048]; -/* Forward declaration */ -static int16_t *load_wav(const char *filename, int *sample_count); - extern uint8_t *rom; extern uint32_t biosmask; extern uint32_t biosaddr; @@ -412,7 +410,7 @@ load_profile_samples(int profile_id) if (samples->spindlemotor_start.buffer == NULL && config->spindlemotor_start.filename[0]) { strcpy(samples->spindlemotor_start.filename, config->spindlemotor_start.filename); samples->spindlemotor_start.volume = config->spindlemotor_start.volume; - samples->spindlemotor_start.buffer = load_wav(config->spindlemotor_start.filename, + samples->spindlemotor_start.buffer = sound_load_wav(config->spindlemotor_start.filename, &samples->spindlemotor_start.samples); if (samples->spindlemotor_start.buffer) { fdd_log(" Loaded spindlemotor_start: %s (%d samples, volume %.2f)\n", @@ -428,7 +426,7 @@ load_profile_samples(int profile_id) if (samples->spindlemotor_loop.buffer == NULL && config->spindlemotor_loop.filename[0]) { strcpy(samples->spindlemotor_loop.filename, config->spindlemotor_loop.filename); samples->spindlemotor_loop.volume = config->spindlemotor_loop.volume; - samples->spindlemotor_loop.buffer = load_wav(config->spindlemotor_loop.filename, + samples->spindlemotor_loop.buffer = sound_load_wav(config->spindlemotor_loop.filename, &samples->spindlemotor_loop.samples); if (samples->spindlemotor_loop.buffer) { fdd_log(" Loaded spindlemotor_loop: %s (%d samples, volume %.2f)\n", @@ -444,7 +442,7 @@ load_profile_samples(int profile_id) if (samples->spindlemotor_stop.buffer == NULL && config->spindlemotor_stop.filename[0]) { strcpy(samples->spindlemotor_stop.filename, config->spindlemotor_stop.filename); samples->spindlemotor_stop.volume = config->spindlemotor_stop.volume; - samples->spindlemotor_stop.buffer = load_wav(config->spindlemotor_stop.filename, + samples->spindlemotor_stop.buffer = sound_load_wav(config->spindlemotor_stop.filename, &samples->spindlemotor_stop.samples); if (samples->spindlemotor_stop.buffer) { fdd_log(" Loaded spindlemotor_stop: %s (%d samples, volume %.2f)\n", @@ -466,7 +464,7 @@ load_profile_samples(int profile_id) if (samples->seek_up[idx].buffer == NULL && config->seek_up[idx].filename[0]) { strcpy(samples->seek_up[idx].filename, config->seek_up[idx].filename); samples->seek_up[idx].volume = config->seek_up[idx].volume; - samples->seek_up[idx].buffer = load_wav(config->seek_up[idx].filename, + samples->seek_up[idx].buffer = sound_load_wav(config->seek_up[idx].filename, &samples->seek_up[idx].samples); if (samples->seek_up[idx].buffer) { fdd_log(" Loaded seek_up[%d]: %s (%d samples, volume %.2f)\n", @@ -479,7 +477,7 @@ load_profile_samples(int profile_id) if (samples->seek_down[idx].buffer == NULL && config->seek_down[idx].filename[0]) { strcpy(samples->seek_down[idx].filename, config->seek_down[idx].filename); samples->seek_down[idx].volume = config->seek_down[idx].volume; - samples->seek_down[idx].buffer = load_wav(config->seek_down[idx].filename, + samples->seek_down[idx].buffer = sound_load_wav(config->seek_down[idx].filename, &samples->seek_down[idx].samples); if (samples->seek_down[idx].buffer) { fdd_log(" Loaded seek_down[%d]: %s (%d samples, volume %.2f)\n", @@ -493,7 +491,7 @@ load_profile_samples(int profile_id) if (samples->post_seek_up[idx].buffer == NULL) { strcpy(samples->post_seek_up[idx].filename, config->post_seek_up[idx].filename); samples->post_seek_up[idx].volume = config->post_seek_up[idx].volume; - samples->post_seek_up[idx].buffer = load_wav(config->post_seek_up[idx].filename, + samples->post_seek_up[idx].buffer = sound_load_wav(config->post_seek_up[idx].filename, &samples->post_seek_up[idx].samples); if (samples->post_seek_up[idx].buffer) { fdd_log(" Loaded POST seek_up[%d] (%d-track): %s (%d samples, volume %.2f)\n", @@ -507,7 +505,7 @@ load_profile_samples(int profile_id) if (samples->post_seek_down[idx].buffer == NULL) { strcpy(samples->post_seek_down[idx].filename, config->post_seek_down[idx].filename); samples->post_seek_down[idx].volume = config->post_seek_down[idx].volume; - samples->post_seek_down[idx].buffer = load_wav(config->post_seek_down[idx].filename, + samples->post_seek_down[idx].buffer = sound_load_wav(config->post_seek_down[idx].filename, &samples->post_seek_down[idx].samples); if (samples->post_seek_down[idx].buffer) { fdd_log(" Loaded POST seek_down[%d] (%d-track): %s (%d samples, volume %.2f)\n", @@ -529,7 +527,7 @@ load_profile_samples(int profile_id) if (samples->bios_post_seek_up[vendor][idx].buffer == NULL) { strcpy(samples->bios_post_seek_up[vendor][idx].filename, config->bios_post_seek_up[vendor][idx].filename); samples->bios_post_seek_up[vendor][idx].volume = config->bios_post_seek_up[vendor][idx].volume; - samples->bios_post_seek_up[vendor][idx].buffer = load_wav(config->bios_post_seek_up[vendor][idx].filename, + samples->bios_post_seek_up[vendor][idx].buffer = sound_load_wav(config->bios_post_seek_up[vendor][idx].filename, &samples->bios_post_seek_up[vendor][idx].samples); if (samples->bios_post_seek_up[vendor][idx].buffer) { fdd_log(" Loaded %s POST seek_up[%d] (%d-track): %s (%d samples, volume %.2f)\n", @@ -543,7 +541,7 @@ load_profile_samples(int profile_id) if (samples->bios_post_seek_down[vendor][idx].buffer == NULL) { strcpy(samples->bios_post_seek_down[vendor][idx].filename, config->bios_post_seek_down[vendor][idx].filename); samples->bios_post_seek_down[vendor][idx].volume = config->bios_post_seek_down[vendor][idx].volume; - samples->bios_post_seek_down[vendor][idx].buffer = load_wav(config->bios_post_seek_down[vendor][idx].filename, + samples->bios_post_seek_down[vendor][idx].buffer = sound_load_wav(config->bios_post_seek_down[vendor][idx].filename, &samples->bios_post_seek_down[vendor][idx].samples); if (samples->bios_post_seek_down[vendor][idx].buffer) { fdd_log(" Loaded %s POST seek_down[%d] (%d-track): %s (%d samples, volume %.2f)\n", @@ -947,92 +945,6 @@ fdd_audio_play_multi_track_seek(int drive, int from_track, int to_track) drive, slot, sample_to_use->samples); } -static int16_t * -load_wav(const char *filename, int *sample_count) -{ - if ((filename == NULL) || (strlen(filename) == 0)) - return NULL; - - if (strstr(filename, "..") != NULL) - return NULL; - - FILE *f = asset_fopen(filename, "rb"); - if (f == NULL) { - fdd_log("FDD Audio: Failed to open WAV file: %s\n", filename); - return NULL; - } - - wav_header_t hdr; - if (fread(&hdr, sizeof(hdr), 1, f) != 1) { - fdd_log("FDD Audio: Failed to read WAV header from: %s\n", filename); - fclose(f); - return NULL; - } - - if (memcmp(hdr.riff, "RIFF", 4) || memcmp(hdr.wave, "WAVE", 4) || memcmp(hdr.fmt, "fmt ", 4) || memcmp(hdr.data, "data", 4)) { - fdd_log("FDD Audio: Invalid WAV format in file: %s\n", filename); - fclose(f); - return NULL; - } - - /* Accept both mono and stereo, 16-bit PCM */ - if (hdr.audio_format != 1 || hdr.bits_per_sample != 16 || (hdr.num_channels != 1 && hdr.num_channels != 2)) { - fdd_log("FDD Audio: Unsupported WAV format in %s (format: %d, bits: %d, channels: %d)\n", - filename, hdr.audio_format, hdr.bits_per_sample, hdr.num_channels); - fclose(f); - return NULL; - } - - int input_samples = hdr.data_size / 2; /* 2 bytes per sample */ - int16_t *input_data = malloc(hdr.data_size); - if (!input_data) { - fdd_log("FDD Audio: Failed to allocate memory for WAV data: %s\n", filename); - fclose(f); - return NULL; - } - - if (fread(input_data, 1, hdr.data_size, f) != hdr.data_size) { - fdd_log("FDD Audio: Failed to read WAV data from: %s\n", filename); - free(input_data); - fclose(f); - return NULL; - } - fclose(f); - - int16_t *output_data; - int output_samples; - - if (hdr.num_channels == 1) { - /* Convert mono to stereo */ - output_samples = input_samples; /* Number of stereo sample pairs */ - output_data = malloc(input_samples * 2 * sizeof(int16_t)); /* Allocate for stereo */ - if (!output_data) { - fdd_log("FDD Audio: Failed to allocate stereo conversion buffer for: %s\n", filename); - free(input_data); - return NULL; - } - - /* Convert mono to stereo by duplicating each sample */ - for (int i = 0; i < input_samples; i++) { - output_data[i * 2] = input_data[i]; /* Left channel */ - output_data[i * 2 + 1] = input_data[i]; /* Right channel */ - } - - free(input_data); - fdd_log("FDD Audio: Loaded %s (mono->stereo, %d samples)\n", filename, output_samples); - } else { - /* Already stereo */ - output_data = input_data; - output_samples = input_samples / 2; /* Number of stereo sample pairs */ - fdd_log("FDD Audio: Loaded %s (stereo, %d samples)\n", filename, output_samples); - } - - if (sample_count) - *sample_count = output_samples; - - return output_data; -} - void fdd_audio_callback(int16_t *buffer, int length) { diff --git a/src/include/86box/fdd_audio.h b/src/include/86box/fdd_audio.h index 433aeaff8..ac6f225be 100644 --- a/src/include/86box/fdd_audio.h +++ b/src/include/86box/fdd_audio.h @@ -71,23 +71,6 @@ typedef enum { MOTOR_STATE_STOPPING } motor_state_t; -/* WAV header structure */ -typedef struct { - char riff[4]; - uint32_t size; - char wave[4]; - char fmt[4]; - uint32_t fmt_size; - uint16_t audio_format; - uint16_t num_channels; - uint32_t sample_rate; - uint32_t byte_rate; - uint16_t block_align; - uint16_t bits_per_sample; - char data[4]; - uint32_t data_size; -} wav_header_t; - /* Audio sample structure */ typedef struct { char filename[512]; diff --git a/src/include/86box/hdd_audio.h b/src/include/86box/hdd_audio.h new file mode 100644 index 000000000..1e6d7b103 --- /dev/null +++ b/src/include/86box/hdd_audio.h @@ -0,0 +1,6 @@ +#include +#include <86box/hdd.h> + +extern void hdd_audio_init(void); +extern void hdd_audio_callback(int16_t *buffer, int length); +extern void hdd_audio_seek(hard_disk_t *hdd, uint32_t new_cylinder); \ No newline at end of file diff --git a/src/include/86box/sound.h b/src/include/86box/sound.h index e35969ead..f3cbaeea1 100644 --- a/src/include/86box/sound.h +++ b/src/include/86box/sound.h @@ -103,6 +103,9 @@ extern void sound_cd_thread_reset(void); extern void sound_fdd_thread_init(void); extern void sound_fdd_thread_end(void); +extern void sound_hdd_thread_init(void); +extern void sound_hdd_thread_end(void); + extern void closeal(void); extern void inital(void); extern void givealbuffer(const void *buf); @@ -110,6 +113,7 @@ extern void givealbuffer_music(const void *buf); extern void givealbuffer_wt(const void *buf); extern void givealbuffer_cd(const void *buf); extern void givealbuffer_fdd(const void *buf, const uint32_t size); +extern void givealbuffer_hdd(const void *buf, const uint32_t size); #define sb_vibra16c_onboard_relocate_base sb_vibra16s_onboard_relocate_base #define sb_vibra16cl_onboard_relocate_base sb_vibra16s_onboard_relocate_base diff --git a/src/include/86box/sound_util.h b/src/include/86box/sound_util.h new file mode 100644 index 000000000..95e95a360 --- /dev/null +++ b/src/include/86box/sound_util.h @@ -0,0 +1,28 @@ +#ifndef SOUND_UTIL_H +#define SOUND_UTIL_H + +#include + +/* WAV file header structure */ +typedef struct wav_header_t { + char riff[4]; + uint32_t file_size; + char wave[4]; + char fmt[4]; + uint32_t fmt_size; + uint16_t audio_format; + uint16_t num_channels; + uint32_t sample_rate; + uint32_t byte_rate; + uint16_t block_align; + uint16_t bits_per_sample; + char data[4]; + uint32_t data_size; +} wav_header_t; + +/* Load a WAV file and return stereo 16-bit samples + * Returns allocated buffer (caller must free) or NULL on error + * sample_count receives the number of stereo sample pairs */ +int16_t *sound_load_wav(const char *filename, int *sample_count); + +#endif /* SOUND_UTIL_H */ \ No newline at end of file diff --git a/src/sound/CMakeLists.txt b/src/sound/CMakeLists.txt index 2c039fa92..3f6b1ef62 100644 --- a/src/sound/CMakeLists.txt +++ b/src/sound/CMakeLists.txt @@ -54,6 +54,7 @@ add_library(snd OBJECT snd_opl_esfm.c snd_ymf701.c snd_ymf71x.c + sound_util.c ) # TODO: Should platform-specific audio driver be here? diff --git a/src/sound/audio4.c b/src/sound/audio4.c index 060e574e6..25ff5b1c2 100644 --- a/src/sound/audio4.c +++ b/src/sound/audio4.c @@ -21,7 +21,7 @@ #include #include -#include +#include #include #include <86box/86box.h> @@ -37,17 +37,17 @@ #define I_WT 2 #define I_CD 3 #define I_FDD 4 -#define I_MIDI 5 +#define I_HDD 5 +#define I_MIDI 6 -static int audio[6] = {-1, -1, -1, -1, -1, -1}; +static int audio[7] = {-1, -1, -1, -1, -1, -1, -1}; #ifdef USE_NEW_API -static struct audio_swpar info[5]; +static struct audio_swpar info[7]; #else -static audio_info_t info[6]; +static audio_info_t info[7]; #endif -static int freqs[6] = {SOUND_FREQ, MUSIC_FREQ, WT_FREQ, CD_FREQ, SOUND_FREQ, 0}; - +static int freqs[7] = {SOUND_FREQ, MUSIC_FREQ, WT_FREQ, CD_FREQ, SOUND_FREQ, SOUND_FREQ, 0}; void closeal(void) { @@ -173,6 +173,12 @@ givealbuffer_fdd(const void *buf, const uint32_t size) givealbuffer_common(buf, I_FDD, (int) size); } +void +givealbuffer_hdd(const void *buf, const uint32_t size) +{ + givealbuffer_common(buf, I_HDD, (int) size); +} + void givealbuffer_midi(const void *buf, const uint32_t size) { diff --git a/src/sound/openal.c b/src/sound/openal.c index d163150af..598709de9 100644 --- a/src/sound/openal.c +++ b/src/sound/openal.c @@ -40,15 +40,17 @@ #define I_WT 2 #define I_CD 3 #define I_FDD 4 -#define I_MIDI 5 +#define I_HDD 5 +#define I_MIDI 6 ALuint buffers[4]; /* front and back buffers */ ALuint buffers_music[4]; /* front and back buffers */ ALuint buffers_wt[4]; /* front and back buffers */ ALuint buffers_cd[4]; /* front and back buffers */ ALuint buffers_fdd[4]; /* front and back buffers */ +ALuint buffers_hdd[4]; /* front and back buffers */ ALuint buffers_midi[4]; /* front and back buffers */ -static ALuint source[6]; /* audio source - CHANGED FROM 5 TO 6 */ +static ALuint source[7]; /* audio sources */ static int midi_freq = 44100; static int midi_buf_size = 4410; @@ -105,9 +107,10 @@ closeal(void) alSourceStopv(sources, source); alDeleteSources(sources, source); - if (sources >= 6) + if (sources >= 7) alDeleteBuffers(4, buffers_midi); alDeleteBuffers(4, buffers_fdd); + alDeleteBuffers(4, buffers_hdd); alDeleteBuffers(4, buffers_cd); alDeleteBuffers(4, buffers_music); alDeleteBuffers(4, buffers); @@ -126,12 +129,14 @@ inital(void) float *cd_buf = NULL; float *midi_buf = NULL; float *fdd_buf = NULL; + float *hdd_buf = NULL; int16_t *buf_int16 = NULL; int16_t *music_buf_int16 = NULL; int16_t *wt_buf_int16 = NULL; int16_t *cd_buf_int16 = NULL; int16_t *midi_buf_int16 = NULL; int16_t *fdd_buf_int16 = NULL; + int16_t *hdd_buf_int16 = NULL; int init_midi = 0; @@ -146,13 +151,14 @@ inital(void) init_midi = 1; /* If the device is neither none, nor system MIDI, initialize the MIDI buffer and source, otherwise, do not. */ - sources = 5 + !!init_midi; + sources = 6 + !!init_midi; if (sound_is_float) { buf = (float *) calloc((BUFLEN << 1), sizeof(float)); music_buf = (float *) calloc((MUSICBUFLEN << 1), sizeof(float)); wt_buf = (float *) calloc((WTBUFLEN << 1), sizeof(float)); cd_buf = (float *) calloc((CD_BUFLEN << 1), sizeof(float)); fdd_buf = (float *) calloc((BUFLEN << 1), sizeof(float)); + hdd_buf = (float *) calloc((BUFLEN << 1), sizeof(float)); if (init_midi) midi_buf = (float *) calloc(midi_buf_size, sizeof(float)); } else { @@ -161,6 +167,7 @@ inital(void) wt_buf_int16 = (int16_t *) calloc((WTBUFLEN << 1), sizeof(int16_t)); cd_buf_int16 = (int16_t *) calloc((CD_BUFLEN << 1), sizeof(int16_t)); fdd_buf_int16 = (int16_t *) calloc((BUFLEN << 1), sizeof(int16_t)); + hdd_buf_int16 = (int16_t *) calloc((BUFLEN << 1), sizeof(int16_t)); if (init_midi) midi_buf_int16 = (int16_t *) calloc(midi_buf_size, sizeof(int16_t)); } @@ -168,18 +175,17 @@ inital(void) alGenBuffers(4, buffers); alGenBuffers(4, buffers_cd); alGenBuffers(4, buffers_fdd); + alGenBuffers(4, buffers_hdd); alGenBuffers(4, buffers_music); alGenBuffers(4, buffers_wt); if (init_midi) alGenBuffers(4, buffers_midi); - // Create sources: 0=main, 1=music, 2=wt, 3=cd, 4=fdd, 5=midi(optional) - alGenSources(sources, source); - + // Create sources: 0=main, 1=music, 2=wt, 3=cd, 4=fdd, 5=hdd, 6=midi(optional) if (init_midi) - alGenSources(5, source); + alGenSources(7, source); else - alGenSources(4, source); + alGenSources(6, source); alSource3f(source[I_NORMAL], AL_POSITION, 0.0f, 0.0f, 0.0f); alSource3f(source[I_NORMAL], AL_VELOCITY, 0.0f, 0.0f, 0.0f); @@ -210,7 +216,13 @@ inital(void) alSource3f(source[I_FDD], AL_DIRECTION, 0.0f, 0.0f, 0.0f); alSourcef(source[I_FDD], AL_ROLLOFF_FACTOR, 0.0f); alSourcei(source[I_FDD], AL_SOURCE_RELATIVE, AL_TRUE); - + + alSource3f(source[I_HDD], AL_POSITION, 0.0f, 0.0f, 0.0f); + alSource3f(source[I_HDD], AL_VELOCITY, 0.0f, 0.0f, 0.0f); + alSource3f(source[I_HDD], AL_DIRECTION, 0.0f, 0.0f, 0.0f); + alSourcef(source[I_HDD], AL_ROLLOFF_FACTOR, 0.0f); + alSourcei(source[I_HDD], AL_SOURCE_RELATIVE, AL_TRUE); + if (init_midi) { alSource3f(source[I_MIDI], AL_POSITION, 0.0f, 0.0f, 0.0f); alSource3f(source[I_MIDI], AL_VELOCITY, 0.0f, 0.0f, 0.0f); @@ -225,6 +237,7 @@ inital(void) memset(music_buf, 0, MUSICBUFLEN * 2 * sizeof(float)); memset(wt_buf, 0, WTBUFLEN * 2 * sizeof(float)); memset(fdd_buf, 0, BUFLEN * 2 * sizeof(float)); + memset(hdd_buf, 0, BUFLEN * 2 * sizeof(float)); if (init_midi) memset(midi_buf, 0, midi_buf_size * sizeof(float)); } else { @@ -233,6 +246,7 @@ inital(void) memset(music_buf_int16, 0, MUSICBUFLEN * 2 * sizeof(int16_t)); memset(wt_buf_int16, 0, WTBUFLEN * 2 * sizeof(int16_t)); memset(fdd_buf_int16, 0, BUFLEN * 2 * sizeof(int16_t)); + memset(hdd_buf_int16, 0, BUFLEN * 2 * sizeof(int16_t)); if (init_midi) memset(midi_buf_int16, 0, midi_buf_size * sizeof(int16_t)); } @@ -244,6 +258,7 @@ inital(void) alBufferData(buffers_wt[c], AL_FORMAT_STEREO_FLOAT32, wt_buf, WTBUFLEN * 2 * sizeof(float), WT_FREQ); alBufferData(buffers_cd[c], AL_FORMAT_STEREO_FLOAT32, cd_buf, CD_BUFLEN * 2 * sizeof(float), CD_FREQ); alBufferData(buffers_fdd[c], AL_FORMAT_STEREO_FLOAT32, fdd_buf, BUFLEN * 2 * sizeof(float), FREQ); + alBufferData(buffers_hdd[c], AL_FORMAT_STEREO_FLOAT32, hdd_buf, BUFLEN * 2 * sizeof(float), FREQ); if (init_midi) alBufferData(buffers_midi[c], AL_FORMAT_STEREO_FLOAT32, midi_buf, midi_buf_size * (int) sizeof(float), midi_freq); } else { @@ -252,6 +267,7 @@ inital(void) alBufferData(buffers_wt[c], AL_FORMAT_STEREO16, wt_buf_int16, WTBUFLEN * 2 * sizeof(int16_t), WT_FREQ); alBufferData(buffers_cd[c], AL_FORMAT_STEREO16, cd_buf_int16, CD_BUFLEN * 2 * sizeof(int16_t), CD_FREQ); alBufferData(buffers_fdd[c], AL_FORMAT_STEREO16, fdd_buf_int16, BUFLEN * 2 * sizeof(int16_t), FREQ); + alBufferData(buffers_hdd[c], AL_FORMAT_STEREO16, hdd_buf_int16, BUFLEN * 2 * sizeof(int16_t), FREQ); if (init_midi) alBufferData(buffers_midi[c], AL_FORMAT_STEREO16, midi_buf_int16, midi_buf_size * (int) sizeof(int16_t), midi_freq); } @@ -262,6 +278,7 @@ inital(void) alSourceQueueBuffers(source[I_WT], 4, buffers_wt); alSourceQueueBuffers(source[I_CD], 4, buffers_cd); alSourceQueueBuffers(source[I_FDD], 4, buffers_fdd); + alSourceQueueBuffers(source[I_HDD], 4, buffers_hdd); if (init_midi) alSourceQueueBuffers(source[I_MIDI], 4, buffers_midi); alSourcePlay(source[I_NORMAL]); @@ -269,6 +286,7 @@ inital(void) alSourcePlay(source[I_WT]); alSourcePlay(source[I_CD]); alSourcePlay(source[I_FDD]); + alSourcePlay(source[I_HDD]); if (init_midi) alSourcePlay(source[I_MIDI]); @@ -280,6 +298,7 @@ inital(void) free(music_buf); free(buf); free(fdd_buf); + free(hdd_buf); } else { if (init_midi) free(midi_buf_int16); @@ -288,6 +307,7 @@ inital(void) free(music_buf_int16); free(buf_int16); free(fdd_buf_int16); + free(hdd_buf_int16); } initialized = 1; @@ -328,35 +348,41 @@ givealbuffer_common(const void *buf, const uint8_t src, const int size, const in void givealbuffer(const void *buf) { - givealbuffer_common(buf, 0, BUFLEN << 1, FREQ); + givealbuffer_common(buf, I_NORMAL, BUFLEN << 1, FREQ); } void givealbuffer_music(const void *buf) { - givealbuffer_common(buf, 1, MUSICBUFLEN << 1, MUSIC_FREQ); + givealbuffer_common(buf, I_MUSIC, MUSICBUFLEN << 1, MUSIC_FREQ); } void givealbuffer_wt(const void *buf) { - givealbuffer_common(buf, 2, WTBUFLEN << 1, WT_FREQ); + givealbuffer_common(buf, I_WT, WTBUFLEN << 1, WT_FREQ); } void givealbuffer_cd(const void *buf) { - givealbuffer_common(buf, 3, CD_BUFLEN << 1, CD_FREQ); + givealbuffer_common(buf, I_CD, CD_BUFLEN << 1, CD_FREQ); } void givealbuffer_midi(const void *buf, const uint32_t size) { - givealbuffer_common(buf, 5, (int) size, midi_freq); + givealbuffer_common(buf, I_MIDI, (int) size, midi_freq); } void givealbuffer_fdd(const void *buf, const uint32_t size) { - givealbuffer_common(buf, 4, (int) size, FREQ); + givealbuffer_common(buf, I_FDD, (int) size, FREQ); +} + +void +givealbuffer_hdd(const void *buf, const uint32_t size) +{ + givealbuffer_common(buf, I_HDD, (int) size, FREQ); } \ No newline at end of file diff --git a/src/sound/sndio.c b/src/sound/sndio.c index 6363163a2..d572652ae 100644 --- a/src/sound/sndio.c +++ b/src/sound/sndio.c @@ -30,11 +30,11 @@ #define I_CD 3 #define I_MIDI 4 #define I_FDD 5 +#define I_HDD 6 -static struct sio_hdl* audio[6] = {NULL, NULL, NULL, NULL, NULL, NULL}; -static struct sio_par info[6]; -static int freqs[6] = { SOUND_FREQ, MUSIC_FREQ, WT_FREQ, CD_FREQ, SOUND_FREQ, 0 }; - +static struct sio_hdl* audio[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL}; +static struct sio_par info[7]; +static int freqs[7] = { SOUND_FREQ, MUSIC_FREQ, WT_FREQ, CD_FREQ, SOUND_FREQ, SOUND_FREQ, 0 }; void closeal(void) { @@ -153,6 +153,12 @@ givealbuffer_fdd(const void *buf, const uint32_t size) { givealbuffer_common(buf, I_FDD, (int) size); } + +void +givealbuffer_hdd(const void *buf, const uint32_t size) +{ + givealbuffer_common(buf, I_HDD, (int) size); +} void al_set_midi(const int freq, UNUSED(const int buf_size)) diff --git a/src/sound/sound.c b/src/sound/sound.c index a5a7b07ed..cdb3ec51c 100644 --- a/src/sound/sound.c +++ b/src/sound/sound.c @@ -37,6 +37,7 @@ #include <86box/snd_mpu401.h> #include <86box/sound.h> #include <86box/fdd_audio.h> +#include <86box/hdd_audio.h> typedef struct { const device_t *device; @@ -96,6 +97,12 @@ static event_t *sound_fdd_start_event; static volatile int fddaudioon = 0; static int fdd_thread_enable = 0; +static thread_t *sound_hdd_thread_h; +static event_t *sound_hdd_event; +static event_t *sound_hdd_start_event; +static volatile int hddaudioon = 0; +static int hdd_thread_enable = 0; + static void (*filter_cd_audio)(int channel, double *buffer, void *priv) = NULL; static void *filter_cd_audio_p = NULL; @@ -614,6 +621,10 @@ sound_poll(UNUSED(void *priv)) if (fdd_thread_enable) { thread_set_event(sound_fdd_event); } + + if (hdd_thread_enable) { + thread_set_event(sound_hdd_event); + } sound_pos_global = 0; } } @@ -857,3 +868,59 @@ sound_fdd_thread_end(void) } } } + +static void +sound_hdd_thread(UNUSED(void *param)) +{ + thread_set_event(sound_hdd_start_event); + while (hddaudioon) { + thread_wait_event(sound_hdd_event, -1); + thread_reset_event(sound_hdd_event); + + if (!hddaudioon) + break; + + static float hdd_float_buffer[SOUNDBUFLEN * 2]; + memset(hdd_float_buffer, 0, sizeof(hdd_float_buffer)); + hdd_audio_callback((int16_t*)hdd_float_buffer, SOUNDBUFLEN * 2); + givealbuffer_hdd(hdd_float_buffer, SOUNDBUFLEN * 2); + } +} + +void +sound_hdd_thread_init(void) +{ + if (!hddaudioon) { + hddaudioon = 1; + hdd_thread_enable = 1; + sound_hdd_start_event = thread_create_event(); + sound_hdd_event = thread_create_event(); + sound_hdd_thread_h = thread_create(sound_hdd_thread, NULL); + + thread_wait_event(sound_hdd_start_event, -1); + thread_reset_event(sound_hdd_start_event); + } +} + +void +sound_hdd_thread_end(void) +{ + if (hddaudioon) { + hddaudioon = 0; + hdd_thread_enable = 0; + thread_set_event(sound_hdd_event); + thread_wait(sound_hdd_thread_h); + + if (sound_hdd_event) { + thread_destroy_event(sound_hdd_event); + sound_hdd_event = NULL; + } + + sound_hdd_thread_h = NULL; + if (sound_hdd_start_event) { + thread_destroy_event(sound_hdd_start_event); + sound_hdd_start_event = NULL; + } + } +} + diff --git a/src/sound/sound_util.c b/src/sound/sound_util.c new file mode 100644 index 000000000..d6d3e7495 --- /dev/null +++ b/src/sound/sound_util.c @@ -0,0 +1,85 @@ +#include +#include +#include +#include + +#include <86box/86box.h> +#include <86box/mem.h> +#include <86box/rom.h> +#include <86box/plat.h> +#include <86box/sound_util.h> + +int16_t * +sound_load_wav(const char *filename, int *sample_count) +{ + if ((filename == NULL) || (strlen(filename) == 0)) + return NULL; + + if (strstr(filename, "..") != NULL) + return NULL; + + FILE *f = asset_fopen(filename, "rb"); + if (f == NULL) + return NULL; + + wav_header_t hdr; + if (fread(&hdr, sizeof(hdr), 1, f) != 1) { + fclose(f); + return NULL; + } + + if (memcmp(hdr.riff, "RIFF", 4) || memcmp(hdr.wave, "WAVE", 4) || + memcmp(hdr.fmt, "fmt ", 4) || memcmp(hdr.data, "data", 4)) { + fclose(f); + return NULL; + } + + /* Accept both mono and stereo, 16-bit PCM */ + if (hdr.audio_format != 1 || hdr.bits_per_sample != 16 || + (hdr.num_channels != 1 && hdr.num_channels != 2)) { + fclose(f); + return NULL; + } + + int input_samples = hdr.data_size / 2; + int16_t *input_data = malloc(hdr.data_size); + if (!input_data) { + fclose(f); + return NULL; + } + + if (fread(input_data, 1, hdr.data_size, f) != hdr.data_size) { + free(input_data); + fclose(f); + return NULL; + } + fclose(f); + + int16_t *output_data; + int output_samples; + + if (hdr.num_channels == 1) { + /* Convert mono to stereo */ + output_samples = input_samples; + output_data = malloc(input_samples * 2 * sizeof(int16_t)); + if (!output_data) { + free(input_data); + return NULL; + } + + for (int i = 0; i < input_samples; i++) { + output_data[i * 2] = input_data[i]; + output_data[i * 2 + 1] = input_data[i]; + } + + free(input_data); + } else { + output_data = input_data; + output_samples = input_samples / 2; + } + + if (sample_count) + *sample_count = output_samples; + + return output_data; +} \ No newline at end of file diff --git a/src/sound/xaudio2.c b/src/sound/xaudio2.c index 8596c2a49..7833af435 100644 --- a/src/sound/xaudio2.c +++ b/src/sound/xaudio2.c @@ -54,6 +54,7 @@ static IXAudio2SourceVoice *srcvoicewt = NULL; static IXAudio2SourceVoice *srcvoicemidi = NULL; static IXAudio2SourceVoice *srcvoicecd = NULL; static IXAudio2SourceVoice *srcvoicefdd = NULL; +static IXAudio2SourceVoice *srcvoicehdd = NULL; #define FREQ SOUND_FREQ #define BUFLEN SOUNDBUFLEN @@ -184,6 +185,7 @@ inital(void) fmt.nAvgBytesPerSec = fmt.nSamplesPerSec * fmt.nBlockAlign; (void) IXAudio2_CreateSourceVoice(xaudio2, &srcvoicefdd, &fmt, 0, 2.0f, &callbacks, NULL, NULL); + (void) IXAudio2_CreateSourceVoice(xaudio2, &srcvoicehdd, &fmt, 0, 2.0f, &callbacks, NULL, NULL); (void) IXAudio2SourceVoice_SetVolume(srcvoice, 1, XAUDIO2_COMMIT_NOW); (void) IXAudio2SourceVoice_Start(srcvoice, 0, XAUDIO2_COMMIT_NOW); @@ -191,6 +193,7 @@ inital(void) (void) IXAudio2SourceVoice_Start(srcvoicemusic, 0, XAUDIO2_COMMIT_NOW); (void) IXAudio2SourceVoice_Start(srcvoicewt, 0, XAUDIO2_COMMIT_NOW); (void) IXAudio2SourceVoice_Start(srcvoicefdd, 0, XAUDIO2_COMMIT_NOW); + (void) IXAudio2SourceVoice_Start(srcvoicehdd, 0, XAUDIO2_COMMIT_NOW); const char *mdn = midi_out_device_get_internal_name(midi_output_device_current); @@ -223,6 +226,8 @@ closeal(void) (void) IXAudio2SourceVoice_FlushSourceBuffers(srcvoicecd); (void) IXAudio2SourceVoice_Stop(srcvoicefdd, 0, XAUDIO2_COMMIT_NOW); (void) IXAudio2SourceVoice_FlushSourceBuffers(srcvoicefdd); + (void) IXAudio2SourceVoice_Stop(srcvoicehdd, 0, XAUDIO2_COMMIT_NOW); + (void) IXAudio2SourceVoice_FlushSourceBuffers(srcvoicehdd); if (srcvoicemidi) { (void) IXAudio2SourceVoice_Stop(srcvoicemidi, 0, XAUDIO2_COMMIT_NOW); (void) IXAudio2SourceVoice_FlushSourceBuffers(srcvoicemidi); @@ -231,6 +236,7 @@ closeal(void) IXAudio2SourceVoice_DestroyVoice(srcvoicewt); IXAudio2SourceVoice_DestroyVoice(srcvoicecd); IXAudio2SourceVoice_DestroyVoice(srcvoicefdd); + IXAudio2SourceVoice_DestroyVoice(srcvoicehdd); IXAudio2SourceVoice_DestroyVoice(srcvoicemusic); IXAudio2SourceVoice_DestroyVoice(srcvoice); IXAudio2MasteringVoice_DestroyVoice(mastervoice); @@ -239,6 +245,7 @@ closeal(void) srcvoicecd = NULL; srcvoicemidi = NULL; srcvoicefdd = NULL; + srcvoicehdd = NULL; mastervoice = NULL; xaudio2 = NULL; @@ -312,6 +319,18 @@ givealbuffer_fdd(const void *buf, const uint32_t size) givealbuffer_common(buf, srcvoicefdd, size); } +void +givealbuffer_hdd(const void *buf, const uint32_t size) +{ + if (!initialized) + return; + + if (!srcvoicefdd) + return; + + givealbuffer_common(buf, srcvoicehdd, size); +} + void al_set_midi(const int freq, const int buf_size) {