How can I have code running while using sampled audio playback in GBDK c?
Asked Answered
A

1

6

I am making a game for the GameBoy in GBDK, and I'm trying to add sounds to the game. GBDK has a function that plays sounds from an array of values, the only problem is that while its playing the sound the rest of the script freezes. Is there a way I can get them to run at the same time?

Affinity answered 3/10, 2014 at 19:4 Comment(5)
I don't know "gbdk". Does it support multi-threading?Misconstruction
I have no clue I would assume not, but I only started learning about it like 2 weeks ago so maybeAffinity
@SteveEmmerson Nope - GBDK is a C compiler for the Nintendo Gameboy, and that doesn't have multi threading.Monolithic
@isaactschampl Pretty sure that the sampled audio playback (which is what you are using) does not support doing other things at the same time - The sampled playback actually uses a lot of the CPU. You could try using other audio playback methods though, but obviously they won't have the same effects.Monolithic
Which begs the question, how does a gameboy play both the game and sound at the same time?Aarau
M
2

There is no way to have code running while using sampled audio playback. This is due to the fact that it actually uses full CPU to preform this playback. If you want to use regular sound effects, you'll either need to pause the game while they play, or use a different method. I'll try to summarize using the other playback method below, but it is kind of complicated and I'm no expert.


Using "normal" sound effects

This is kind of WIP - I'm not too experienced with it but it should let you get started.

To use sound effects, you need to write to GameBoy audio registers. This is found in GBDK's hardware.h, which is automatically included with references to gb\gb.h. But (of course) the registers don't have any documentation. This information is found on the GB Cribsheet. There's also this sound documentation file (unfortunately it behaves weirdly on windows encodings - Open with something other than notepad), along with some other information found on the Devrs.com sound documentation.

Working off of GBSOUND.TXT:

The addresses through which the sound channels can be accessed are:
$Addresses: (Description), (Register shorthand)
$FF10 -- $FF14: Channel 1, Referred to as NR10-NR14
$FF15 is unused, was probably going to be a sweep reg for channel 2
originally
$FF16 -- $FF19: Channel 2, Referred to as NR21-NR24
$FF1A -- $FF1E: Channel 3, Referred to as NR30-NR34
$FF1F is unused, was probably going to be a sweep reg for channel 4
originally
$FF20 -- $FF23: Channel 4, Referred to as NR41-NR44
$FF24 controls the Vin status and volume, Referred to as NR50
$FF25 selects which output each channel goes to, Referred to as NR51
$FF26 is the status register, and also controls the sound circuit's power.
Referred to as NR52
$FF27 -- $FF2F are unused.
$FF30 -- $FF3F is the load register space for the 4-bit samples for channel
3

In GBDK, the registers are named NR10_REG, NR11_REG, NR12_REG, ect.

Also, try looking at the example program sound.c, which doesn't compile for me unfortuantely. I might edit this to include more info.


To answer @franklin's question:

Which begs the question, how does a gameboy play both the game and sound at the same time?

They usually don't do that with sample playback. For instance, if you look at Pokémon Yellow, Pikachu's cry is done with sample playback. But while that is playing, nothing else is done. On the other hand, things like normal background music are done using the other audio hardware (sorry, not very detailed wiki link). Similarly, while move sound effects are done with the noise channel (used for the sample playback as well), they aren't actually sampled audio. As such, the game can continue running.

Monolithic answered 6/10, 2014 at 17:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.