/*---------------------------------------------------------------------------* Project: MIDI adaptor sample demo File: synth.c Copyright (C)1998-2007 Nintendo All Rights Reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Log: synth.c,v $ Revision 1.2 2007/04/17 08:23:52 hiratsu Refactoring. Removed unused functions and variables. Revision 1.1 2007/04/13 08:39:10 hiratsu Initial check-in. (Most of codes are derived from syndemo.c) $NoKeywords: $ *---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------* This program shows how to set up and use the AX *---------------------------------------------------------------------------*/ #include "synth.h" #include #include #include #include #include static MEMHeapHandle hExpHeap; SYNSYNTH synth; /*---------------------------------------------------------------------------* *---------------------------------------------------------------------------*/ static void callbackDropVoice(void *p) { AXVPB *pvpb = p; OSReport("PVPB %.8x is being dropped context %d\n", pvpb, pvpb->userContext); } /*---------------------------------------------------------------------------* *---------------------------------------------------------------------------*/ static void callbackAudioFrame(void) { // run the synth SYNRunAudioFrame(); // tell the mixer to update settings MIXUpdateSettings(); } /*---------------------------------------------------------------------------* *---------------------------------------------------------------------------*/ void programChange(u8 prog) { u8 ch[3]; int old; ASSERTMSG(0<=prog && prog<128, "Out of range. (Illegal program number.)"); ch[0] = 0xC0; ch[1] = prog; OSReport("program: %d\n", prog); old = OSDisableInterrupts(); SYNMidiInput(&synth, ch); OSRestoreInterrupts(old); } /*---------------------------------------------------------------------------* *---------------------------------------------------------------------------*/ void input(u8 *ch) { int old = OSDisableInterrupts(); SYNMidiInput(&synth, ch); OSRestoreInterrupts(old); } /*---------------------------------------------------------------------------* *---------------------------------------------------------------------------*/ static void* LoadFileIntoRam(char *path) { DVDFileInfo handle; u32 round_length; s32 read_length; void *buffer; // Open File if (!DVDOpen(path, &handle)) { OSReport("WARNING! Failed to open %s\n", path); return NULL; } // Make sure file length is not 0 if (DVDGetLength(&handle) == 0) { OSReport("WARNING! File length is 0\n"); return NULL; } round_length = OSRoundUp32B(DVDGetLength(&handle)); buffer = MEMAllocFromExpHeapEx(hExpHeap, round_length, 32); // Make sure we got a buffer if (buffer == NULL) { OSReport("WARNING! Unable to allocate buffer\n"); return NULL; } // Read Files read_length = DVDRead(&handle, buffer, (s32)(round_length), 0); // Make sure we read the file correctly if (read_length <= 0) { OSReport("WARNING! File lenght is wrong\n"); return NULL; } return buffer; } /*---------------------------------------------------------------------------* *---------------------------------------------------------------------------*/ void initialize(void) { u8 *wavetable; void *arenaMem2Lo; void *arenaMem2Hi; u8 *samples; void *axBuffer; void *mixBuffer; void *synBuffer; arenaMem2Lo = OSGetMEM2ArenaLo(); arenaMem2Hi = OSGetMEM2ArenaHi(); hExpHeap = MEMCreateExpHeap(arenaMem2Lo, (u32)arenaMem2Hi - (u32) arenaMem2Lo); AIInit(NULL); axBuffer = MEMAllocFromExpHeapEx(hExpHeap, AXGetMemorySize(AX_MAX_VOICES), 32); mixBuffer = MEMAllocFromExpHeap(hExpHeap, MIXGetMemorySize(AX_MAX_VOICES)); synBuffer = MEMAllocFromExpHeap(hExpHeap, SYNGetMemorySize(AX_MAX_VOICES)); AXInitSpecifyMem(AX_MAX_VOICES, axBuffer); MIXInitSpecifyMem(mixBuffer); SYNInitSpecifyMem(synBuffer); wavetable = LoadFileIntoRam("/axdemo/synth/gm16pcm.wt"); samples = LoadFileIntoRam("/axdemo/synth/gm16pcm.pcm"); // register user callback for audio frames notification AXRegisterCallback(&callbackAudioFrame); // initialize synth SYNInitSynth( &synth, // pointer to synth object wavetable, // pointer to wavetable samples, // pointer to samples NULL, // pointer to zero buffer (no need) 16, // priority for voice allocation 15, // priority for notes 1 // priority for notes that have been released ); return; // finalize process SYNQuit(); MIXQuit(); AXQuit(); if (wavetable) { MEMFreeToExpHeap(hExpHeap, wavetable); } if (samples) { MEMFreeToExpHeap(hExpHeap, samples); } if (axBuffer) { MEMFreeToExpHeap(hExpHeap, axBuffer); } if (mixBuffer) { MEMFreeToExpHeap(hExpHeap, mixBuffer); } if (synBuffer) { MEMFreeToExpHeap(hExpHeap, synBuffer); } }