Skip to content

Enhance sfx_handler to accomodate more formats #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docs/demo.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
* 1: Fist
* 3: Shotgun

### Music and sound effect(sfx)
Support music and sound effects.

## Quake
**source**: [quake-embedded](https://github.com/sysprog21/quake-embedded/)

Expand Down Expand Up @@ -47,6 +50,9 @@ make

You may use the mouse to adjust the pitch and yaw angle

### Music and sound effect(sfx)
Support sound effects but not music currently because Quake needs a CD-ROM and the extracted pak file doesn't contain any music or bgm-related files.

### Limitations
* Mouse wheel input is not supported
* All sound functions are not implemented
* Music related functions in Quake are not implemented
2 changes: 1 addition & 1 deletion docs/syscall.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ The request is similar to how music is managed earlier in the description and su
Music data is defined in a structure called `musicinfo_t`. The SDL2_mixer library is used by the emulator to play music using the fields `data` and `size` in the structure.

#### Sound Effect(sfx)
`sfxinfo_t` is a structure that defines sound effect data and size. The `data` and `size` fields of the structure are used to play sound effect with the SDL2_mixer library.
`sfxinfo_t` is a structure that defines sound effect data and size. The `data` and `size` fields of the structure are used to play sound effect with the SDL2_mixer library. Currently, support sound effect of [Doom's WAV](https://doomwiki.org/wiki/Sound) format and [normal WAV](https://en.wikipedia.org/wiki/WAV) format(includes [RIFF header](https://en.wikipedia.org/wiki/Resource_Interchange_File_Format)).

### `setup_audio` - setup or shutdown sound system

Expand Down
23 changes: 15 additions & 8 deletions src/syscall_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -578,12 +578,16 @@ static void *sfx_handler(void *arg)
sound_t *sfx = (sound_t *) arg;
uint8_t *ptr = sfx->data;

/* parsing sound */
ptr += 2; /* skip format */
ptr += 2; /* skip sample rate since SAMPLE_RATE is defined */
nr_sfx_samples = *(uint32_t *) ptr;
ptr += 4;
ptr += 4; /* skip pad bytes */
if (*ptr & 0x3) { /* Doom WAV format */
ptr += 2; /* skip format */
ptr += 2; /* skip sample rate since SAMPLE_RATE is defined */
nr_sfx_samples = *(uint32_t *) ptr;
ptr += 4;
ptr += 4; /* skip pad bytes */
} else { /* Normal WAV format*/
ptr += 44; /* skip RIFF header */
nr_sfx_samples = sfx->size - 44;
}

memcpy(sfx_samples, ptr, sizeof(uint8_t) * nr_sfx_samples);
sfx_chunk = Mix_QuickLoad_RAW(sfx_samples, nr_sfx_samples);
Expand All @@ -594,8 +598,11 @@ static void *sfx_handler(void *arg)
if (chan == -1)
return NULL;

/* multiplied by 8 because sfx->volume's max is 15 */
Mix_Volume(chan, sfx->volume * 8);
if (*ptr & 0x3) /* Doom, multiplied by 8 because sfx->volume's max is 15 */
Mix_Volume(chan, sfx->volume * 8);
else /* Quake, + 1 mod by 128 because sfx->volume's max is 255 and
Mix_Volume's max is 128 */
Mix_Volume(chan, (sfx->volume + 1) % 128);

return NULL;
}
Expand Down