Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Y0UR-U5ERNAME
Last active February 12, 2024 15:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Y0UR-U5ERNAME/ffcb144afc9e42df96a088fa7433a6ac to your computer and use it in GitHub Desktop.
Save Y0UR-U5ERNAME/ffcb144afc9e42df96a088fa7433a6ac to your computer and use it in GitHub Desktop.
Guide on how to use .wav sound effects in GB Studio 3.0 with GBVM. (Made by NalaFala/Yousurname)

.wav Sound Effects Guide for GB Studio 3

This guide is outdated as of v3.1. You can use the built in function instead. Note that .wav sound effects take a considerable amount of space in the ROM.

Conversion

Once you have your .wav file you want to use ready (4 seconds or less), you have to convert it to 8kHz mono, Unsigned 8-bit PCM (unless it already is in that format) with your preferred audio editor. In Audacity, you can do this with Tracks > Mix Stereo Down to Mono, Tracks > Resample > 8000, and File > Export As WAV and choose "Unsigned 8-bit PCM" where it says "Encoding."

Download the below cvtsample.py file and put it into the same folder as your .wav file. You will need Python to run this. To run it, first cd to the directory where cvtsample.py is in, then use the command py cvtsample.py wav_name.wav where wav_name is the name of your .wav file. A wav_name.c file will be created in this directory.

Insertion

To insert wav_name.c file into your project, open your project in GB Studio and press Game > Advanced > Eject Engine. Once the assets/engine folder has been created, insert wav_name.c into assets/engine/src/data.

Scripting

Now, select the scene where you want to play the .wav sound. Add the event named "GBVM Script." Inside the text box, type the command

VM_WAVE_PLAY             FRAMES, ___bank_wav_name, _wav_name, ___size_wav_name

where FRAMES is the amount of frames you want to play the sound for before it can be overridden. If you want to string together multiple sound effects, add a Wait 3.9 seconds event between them.

# This is a modified version of https://github.com/chrismaltby/gbvm/blob/master/utils/cvtsample.py
#!/usr/bin/env python3
import sys
import wave
from pathlib import Path
def main(argv=None):
argv = argv or sys.argv
if len(argv) < 2:
print("cvtsample.py: no filename; try cvtsample.py --help")
sys.exit(1)
infilename = Path(argv[1])
ident = argv[2] if len(argv) > 2 else str(infilename.with_suffix(''))
if infilename in ('--help', '-h'):
print("usage: cvtsample.py SOURCE [IDENTIFIER]")
return
sHDR = "#pragma bank 255\n\n// Wave file: {:s}\n\nconst void __at(255) __bank_{:s};\nconst unsigned char {:s}[] = {{\n"
sFOOT = "}};\n\nconst void __at(sizeof({:s})) __size_{:s};\n"
sEMIT = "0x{:x}"
sNEW = ",\n"
sNONEW = ","
with wave.open(str(infilename), mode="rb") as f:
p = f.getparams()
if (p.nchannels == 1) and (p.sampwidth == 1) and (p.framerate >= 8000) and (p.framerate <= 8192) and (p.comptype == 'NONE'):
with open(str(infilename.with_suffix('.c')), "w") as d:
data = f.readframes(p.nframes)
c = 0
cnt = 0;
flag = False
d.write(sHDR.format(str(infilename), ident, ident))
for i in range(len(data) - len(data) % 32):
c = ((c << 4) | (data[i] >> 4)) & 0xFF
if flag:
d.write(sEMIT.format(c))
cnt += 1
d.write(sNEW if (cnt % 16 == 0) else sNONEW)
flag = not flag
d.write(sFOOT.format(ident, ident))
else:
print("Invalid WAV file format: must be MONO 8KHz (64KBit) PCM")
if __name__=='__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment