sound tryout and failure

This commit is contained in:
wgroeneveld 2018-08-07 09:23:00 +02:00
parent 32f62cadb0
commit eac770bf6c
6 changed files with 1826 additions and 2 deletions

View File

@ -8,6 +8,7 @@ SET(CMAKE_EXE_LINKER_FLAGS "${BASE_CMAKE_LINK_FLAGS} -mthumb-interwork -mthumb -
project(gba-sprite-engine)
SET(CMAKE_CXX_STANDARD 11)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-narrowing")
set(CMAKE_VERBOSE_MAKEFILE on)
include_directories(src)

View File

@ -2,7 +2,7 @@ SET(CMAKE_C_COMPILER arm-none-eabi-gcc)
SET(CMAKE_CXX_COMPILER arm-none-eabi-g++)
set_property(SOURCE engine/gba/sin_lut.s PROPERTY LANGUAGE C)
add_executable(${PROJECT_NAME}.elf main.cpp engine/sprites/sprite_manager.cpp engine/sprites/sprite_manager.h engine/gba/tonc_memmap.h engine/gba/tonc_core.h engine/gba/tonc_memdef.h engine/gba/tonc_types.h engine/sprites/sprite.cpp engine/sprites/sprite.h kul.h engine/palette/palette_manager.cpp engine/palette/palette_manager.h engine/palette/combined_palette.cpp engine/palette/combined_palette.h engine/allocator.cpp engine/allocator.h engine/gba/tonc_oam.h engine/gba/tonc_math.h engine/gba/sin_lut.s engine/Scene.cpp engine/Scene.h engine/sprites/sprite_builder.cpp engine/sprites/sprite_builder.h engine/sprites/affine_sprite.cpp engine/sprites/affine_sprite.h flying_stuff_scene.cpp flying_stuff_scene.h engine/gba_engine.cpp engine/gba_engine.h engine/background/text_stream.cpp engine/background/text_stream.h engine/background/background.cpp engine/background/background.h engine/background/text.h sample_start_scene.cpp sample_start_scene.h engine/effects/fade_out_scene.cpp engine/effects/fade_out_scene.h engine/gba/tonc_core_stub.h engine/effects/scene_effect.h lama.h)
add_executable(${PROJECT_NAME}.elf main.cpp engine/sprites/sprite_manager.cpp engine/sprites/sprite_manager.h engine/gba/tonc_memmap.h engine/gba/tonc_core.h engine/gba/tonc_memdef.h engine/gba/tonc_types.h engine/sprites/sprite.cpp engine/sprites/sprite.h kul.h engine/palette/palette_manager.cpp engine/palette/palette_manager.h engine/palette/combined_palette.cpp engine/palette/combined_palette.h engine/allocator.cpp engine/allocator.h engine/gba/tonc_oam.h engine/gba/tonc_math.h engine/gba/sin_lut.s engine/Scene.cpp engine/Scene.h engine/sprites/sprite_builder.cpp engine/sprites/sprite_builder.h engine/sprites/affine_sprite.cpp engine/sprites/affine_sprite.h flying_stuff_scene.cpp flying_stuff_scene.h engine/gba_engine.cpp engine/gba_engine.h engine/background/text_stream.cpp engine/background/text_stream.h engine/background/background.cpp engine/background/background.h engine/background/text.h sample_start_scene.cpp sample_start_scene.h engine/effects/fade_out_scene.cpp engine/effects/fade_out_scene.h engine/gba/tonc_core_stub.h engine/effects/scene_effect.h lama.h sample_sound.h)
add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
COMMAND ${CMAKE_OBJCOPY} -v -O binary ${PROJECT_NAME}.elf ${PROJECT_NAME}.gba

View File

@ -7,6 +7,28 @@
#include "gba_engine.h"
#include "allocator.h"
int x = 0;
void onvblank() {
REG_IME = 0;
unsigned short tempInterruptState = REG_IF;
if((REG_IF & INTERRUPT_VBLANK) == INTERRUPT_VBLANK) {
// if vblanks_remaning is zero: stop
//*sound_control &= ~(SOUND_B_RIGHT_CHANNEL | SOUND_B_LEFT_CHANNEL | SOUND_B_FIFO_RESET);
//*dma2_control = 0;
// else counter--
// OR: to restart sound (channelA)
x++;
}
//TextStream::instance() << x;
REG_IF = tempInterruptState;
REG_IME = 1;
}
void GBAEngine::update() {
vsync();
@ -33,8 +55,56 @@ u16 GBAEngine::readKeys() {
return ~REG_KEYS & KEY_ANY;
}
SoundControl GBAEngine::channelAControl() {
return {
&REG_DMA1CNT,
&REG_DMA1SAD,
&REG_DMA1DAD,
&REG_FIFOA,
SDS_AR | SDS_AL | SDS_ARESET
};
}
SoundControl GBAEngine::channelBControl() {
return {
&REG_DMA2CNT,
&REG_DMA2SAD,
&REG_DMA2DAD,
&REG_FIFOB,
SDS_BR | SDS_BL | SDS_BRESET
};
}
void GBAEngine::queueSound(const s8 *data, int totalSamples, int sampleRate, SoundControl control) {
REG_TM0CNT = 0;
*(control.DMAControl) = 0; // reset previous sound
REG_SNDDSCNT |= control.ControlFlags; // output to both sides, reset fifo
REG_SNDSTAT = SSTAT_ENABLE; // enable all sound
*(control.DMASourceAddress) = (u32) data;
*(control.DMADestinationAddress) = *(control.FiFoBuffer);
*(control.DMAControl) = DMA_DST_FIXED | DMA_REPEAT | DMA_32 | DMA_SYNC_TO_TIMER | DMA_ENABLE;
u16 ticksPerSample = CLOCK / sampleRate; // divide the clock (ticks/second) by the sample rate (samples/second)
REG_TM0D = OVERFLOW_16_BIT_VALUE - ticksPerSample;
// channel_a_vblanks_remaining = total_samples * ticks_per_sample * (1.0 / CYCLES_PER_BLANK);
REG_TM0CNT = TM_ENABLE | TM_FREQ_1; // enable timer - dma auto-syncs to this thanks to DMA_SYNC_TO_TIMER
}
GBAEngine::GBAEngine() {
// setup screen control flags
REG_DISPCNT = DCNT_MODE0 | DCNT_OBJ | DCNT_OBJ_1D | DCNT_BG0 | DCNT_BG1 | DCNT_BG2 | DCNT_BG3;
// setup interrupt control flags for vblank IRQing
REG_DISPSTAT |= DISPLAY_INTERRUPT_VBLANK_ENABLE;
REG_IE |= INTERRUPT_VBLANK;
*IRQ_CALLBACK = (u32) &onvblank;
//REG_IME = 1;
REG_SNDDSCNT = 0;
Allocator::free();
}

View File

@ -8,9 +8,33 @@
#include <engine/sprites/sprite_manager.h>
#include <engine/gba/tonc_memmap.h>
#include <engine/gba/tonc_memdef.h>
#include <engine/effects/scene_effect.h>
#include "Scene.h"
#define CLOCK 16777216
#define CYCLES_PER_BLANK 280806
#define OVERFLOW_16_BIT_VALUE 65536
#define DISPLAY_INTERRUPT_VBLANK_ENABLE 0x08
#define INTERRUPT_VBLANK 0x1
#define DMA_SYNC_TO_TIMER 0x30000000
#define IRQ_CALLBACK ((volatile unsigned int*) 0x3007FFC)
enum SoundChannel {
ChannelA, ChannelB
};
struct SoundControl {
vu32* DMAControl; // ex. &REG_DMA1CNT
vu32* DMASourceAddress; // ex. &REG_DMA1SAD
vu32* DMADestinationAddress; // ex. &REG_DMA1DAD
vu32* FiFoBuffer; // ex. &REG_FIFOA
u16 ControlFlags;
};
void onvblank();
class GBAEngine {
private:
// WHY raw pointers? the engine does the transition and cleanup work itself
@ -24,7 +48,14 @@ private:
while (REG_VCOUNT >= 160);
while (REG_VCOUNT < 160);
}
SoundControl channelAControl();
SoundControl channelBControl();
SoundControl soundControl(SoundChannel channel) {
return channel == ChannelA ? channelAControl() : channelBControl();
};
void cleanupPreviousScene();
void queueSound(const s8* data, int totalSamples, int sampleRate, SoundControl control);
public:
GBAEngine();
@ -32,6 +63,13 @@ public:
void setScene(Scene* scene);
void transitionIntoScene(Scene* scene, SceneEffect* effect);
void queueMusic(const s8* data, int totalSamples) {
queueSound(data, totalSamples, 16000, soundControl(ChannelA));
}
void queueSound(const s8* data, int totalSamples) {
queueSound(data, totalSamples, 16000, soundControl(ChannelB));
}
u16 readKeys();
void update();
void delay(int times) {

View File

@ -5,7 +5,7 @@
#include <engine/palette/palette_manager.h>
#include <engine/allocator.h>
#include "kul.h"
#include "sample_sound.h"
#include "sample_start_scene.h"
#include "flying_stuff_scene.h"
@ -21,6 +21,8 @@ int main() {
startScene->setEngineForSceneSwitching(engine);
engine->setScene(startScene);
engine->queueSound(zelda_secret_16K_mono, zelda_secret_16K_mono_bytes);
while (true) {
engine->update();
engine->delay(1000);

1713
src/sample_sound.h Normal file

File diff suppressed because it is too large Load Diff