Skip to content

Commit

Permalink
Implement ADPCM support for sound and music.
Browse files Browse the repository at this point in the history
Merge pull request #86 from haroldo-ok/adpcm
This fixes #85
  • Loading branch information
haroldo-ok authored Nov 29, 2022
2 parents b9fd7cc + 174efe8 commit 0dad497
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 18 deletions.
22 changes: 15 additions & 7 deletions base/src/vn_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ void VN_init() {
XGM_setForceDelayDMA(TRUE);

VDP_setTextPalette(TEXT_PAL);
VDP_drawText("choice4genesis v0.11.2", 17, 27);
VDP_drawText("choice4genesis v0.12.0", 17, 27);
}


Expand Down Expand Up @@ -245,14 +245,22 @@ void VN_font(const Image *image) {
}


void VN_music(const u8 *music) {
XGM_startPlay(music);
void VN_music(const u8 *music, const u32 length, const u8 driverFlags) {
if (driverFlags == SOUND_ADPCM) {
SND_startPlay_2ADPCM(music, length, SOUND_PCM_CH1, TRUE);
} else {
XGM_startPlay(music);
}
}

void VN_sound(const u8 *sound, const u16 length) {
XGM_stopPlayPCM (SOUND_PCM_CH2);
XGM_setPCM(PCM_CHANNEL, sound, length);
XGM_startPlayPCM(PCM_CHANNEL, 1, SOUND_PCM_CH2);
void VN_sound(const u8 *sound, const u32 length, const u8 driverFlags) {
if (driverFlags == SOUND_ADPCM) {
SND_startPlay_2ADPCM(sound, length, SOUND_PCM_CH2, FALSE);
} else {
XGM_stopPlayPCM (SOUND_PCM_CH2);
XGM_setPCM(PCM_CHANNEL, sound, length);
XGM_startPlayPCM(PCM_CHANNEL, 1, SOUND_PCM_CH2);
}
}

void VN_stop(const u8 flags) {
Expand Down
7 changes: 5 additions & 2 deletions base/src/vn_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
#define LAYER_BACKGROUND (2)
#define LAYER_WINDOW (4)

#define SOUND_XGM (1)
#define SOUND_ADPCM (2)

extern void VN_init();

extern void VN_background(const Image *image);
extern void VN_image(const Image *image, const u8 flags);
extern void VN_imageAt(u16 x, u16 y);
extern void VN_font(const Image *image);

extern void VN_music(const u8 *music);
extern void VN_sound(const u8 *sound, const u16 length);
extern void VN_music(const u8 *music, const u32 length, const u8 driverFlags);
extern void VN_sound(const u8 *sound, const u32 length, const u8 driverFlags);
extern void VN_stop(const u8 flags);

extern void VN_textStart();
Expand Down
Binary file added examples/test/project/greeting_3_karen.wav
Binary file not shown.
Binary file added examples/test/project/illurock.wav
Binary file not shown.
10 changes: 8 additions & 2 deletions examples/test/project/startup.choice
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,11 @@
* flush
* background "Red echidna.jpg"
Showing a red echidna
# Go to another scene
* goto_scene test
# More options...
* choice
# Play ADPCM music
* music "illurock.wav", adpcm
# Play ADPCM sound
* sound "greeting_3_karen.wav", adpcm
# Go to another scene
* goto_scene test
21 changes: 17 additions & 4 deletions generator/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,31 @@ const COMMAND_GENERATORS = {

'music': (entity, context) => {
const musicFileName = getFileNameConstant(entity, entity.params.positional.fileName, context, 'Music filename');
const flags = entity.params.flags || {};
const isAdpcm = Object.entries(flags).filter(([k, v]) => v).find(([name, v]) => name.toUpperCase() === 'ADPCM');

const resType = isAdpcm ? 'WAV' : 'XGM';
const driver = isAdpcm ? '2ADPCM' : 'APLIB';
const driverFlag = isAdpcm ? 'SOUND_ADPCM' : 'SOUND_XGM';

const musicVariable = addResource(context.res.music, musicFileName, musicVariable =>
`XGM ${musicVariable} "../project/${musicFileName}" APLIB`);
`${resType} ${musicVariable} "../project/${musicFileName}" ${driver}`);

return `VN_music(${musicVariable});`;
return `VN_music(${musicVariable}, sizeof(${musicVariable}), ${driverFlag});`;
},

'sound': (entity, context) => {
const soundFileName = getFileNameConstant(entity, entity.params.positional.fileName, context, 'Sound filename');
const flags = entity.params.flags || {};
const isAdpcm = Object.entries(flags).filter(([k, v]) => v).find(([name, v]) => name.toUpperCase() === 'ADPCM');

const driver = isAdpcm ? '2ADPCM' : 'XGM';
const driverFlag = isAdpcm ? 'SOUND_ADPCM' : 'SOUND_XGM';

const soundVariable = addResource(context.res.music, soundFileName, soundVariable =>
`WAV ${soundVariable} "../project/${soundFileName}" XGM`);
`WAV ${soundVariable} "../project/${soundFileName}" ${driver}`);

return `VN_sound(${soundVariable}, sizeof(${soundVariable}));`;
return `VN_sound(${soundVariable}, sizeof(${soundVariable}), ${driverFlag});`;
},

'stop': (entity, context) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "choice4genesis",
"version": "0.11.2",
"version": "0.12.0",
"description": "A ChoiceScript clone that generates SGDK-compatible C source for the Sega Genesis ",
"main": "index.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions parser/syntax-full.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ const COMMANDS = {
'image': { positional: ['fileName'], named: { 'at': ['x', 'y'] }, flags: ['foreground', 'background'] },
'font': { positional: ['fileName'] },

'music': { positional: ['fileName'] },
'sound': { positional: ['fileName'] },
'music': { positional: ['fileName'], flags: ['adpcm'] },
'sound': { positional: ['fileName'], flags: ['adpcm'] },
'stop': { flags: ['music', 'sound'] },

'window': { named: { 'from': ['x', 'y'], 'to': ['x', 'y'], 'size': ['w', 'h'] }, flags: ['borderless', 'withborder', 'default'] },
Expand Down

0 comments on commit 0dad497

Please sign in to comment.