Skip to content

Commit

Permalink
Engine::Frame is now just Frame
Browse files Browse the repository at this point in the history
Tempo effect now changes speed immediately
remove SyncWorker
  • Loading branch information
stoneface86 committed Jul 7, 2021
1 parent 0fedfa5 commit b394ddc
Show file tree
Hide file tree
Showing 21 changed files with 244 additions and 414 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ if (SOUND_REQUIRED)
# 1. exporting to WAV (wav encoder)
# 2. Ringbuffer, ma_rb
target_compile_definitions(miniaudio PUBLIC
MA_NO_DECODING
MA_NO_MP3
MA_NO_FLAC
MA_NO_GENERATION
MA_NO_DEVICE_IO
MA_NO_THREADING
Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
---

[![build-lib][build-badge]][build-link]
[![tests][tests-badge]][tests-link]
[![Discord](https://img.shields.io/discord/770034905231917066?svg=true)](https://discord.gg/m6wcAK3)


Expand Down Expand Up @@ -49,5 +48,3 @@ This project is licensed under the MIT License - See [LICENSE](LICENSE) for more

[build-badge]: https://github.com/stoneface86/trackerboy/workflows/build/badge.svg
[build-link]: https://github.com/stoneface86/trackerboy/actions?query=workflow%3Abuild
[tests-badge]: https://github.com/stoneface86/trackerboy/workflows/tests/badge.svg
[tests-link]: https://github.com/stoneface86/trackerboy/actions?query=workflow%3Atests
28 changes: 2 additions & 26 deletions include/trackerboy/engine/Engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#pragma once

#include "trackerboy/data/Module.hpp"
#include "trackerboy/engine/Frame.hpp"
#include "trackerboy/engine/IApu.hpp"
#include "trackerboy/engine/MusicRuntime.hpp"
#include "trackerboy/engine/RuntimeContext.hpp"
Expand All @@ -39,28 +40,6 @@ class Engine {

public:

struct Frame {
constexpr Frame() :
halted(false),
startedNewRow(false),
startedNewPattern(false),
time(0),
speed(0),
order(0),
row(0)
{
}

bool halted; // halt status
bool startedNewRow;
bool startedNewPattern;
uint32_t time; // time index
Speed speed; // the current engine speed
int order; // current order index
int row; // current row index
};


Engine(IApu &apu, Module const* mod = nullptr);

Module const* getModule() const;
Expand Down Expand Up @@ -111,15 +90,12 @@ class Engine {
void clearChannel(ChType ch);

IApu &mApu;
//Module &mModule;
Module const* mModule;
std::optional<RuntimeContext> mRc;
std::optional<MusicRuntime> mMusicContext;

//TODO: sfx runtime

// frames elapsed since last reset
uint32_t mTime;
int mTime;

bool mPatternRepeat;

Expand Down
56 changes: 56 additions & 0 deletions include/trackerboy/engine/Frame.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
** Trackerboy - Gameboy / Gameboy Color music tracker
** Copyright (C) 2019-2021 stoneface86
**
** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and associated documentation files (the "Software"), to deal
** in the Software without restriction, including without limitation the rights
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
** copies of the Software, and to permit persons to whom the Software is
** furnished to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice shall be included in all
** copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
** SOFTWARE.
**
*/

#pragma once

#include "trackerboy/trackerboy.hpp"

namespace trackerboy {

//
// Informational struct about the current frame being stepped.
//
struct Frame {

constexpr Frame() :
halted(false),
startedNewRow(false),
startedNewPattern(false),
speed(0),
time(0),
order(0),
row(0)
{
}

bool halted; // halt status
bool startedNewRow;
bool startedNewPattern;
Speed speed; // the current engine speed
int time; // time index
int order; // current order index
int row; // current row index
};

}
16 changes: 2 additions & 14 deletions include/trackerboy/engine/MusicRuntime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#pragma once

#include "trackerboy/data/Song.hpp"
#include "trackerboy/engine/Frame.hpp"
#include "trackerboy/engine/FrequencyControl.hpp"
#include "trackerboy/engine/GlobalState.hpp"
#include "trackerboy/engine/InstrumentRuntime.hpp"
Expand All @@ -27,16 +28,6 @@ class MusicRuntime {
public:
MusicRuntime(Song const& song, int orderNo, int patternRow, bool patternRepeat = false);

int currentOrder() const noexcept;

int currentRow() const noexcept;

Speed currentSpeed() const noexcept;

bool hasNewRow() const noexcept;

bool hasNewPattern() const noexcept;

void halt(RuntimeContext const& rc);

void lock(RuntimeContext const& rc, ChType ch);
Expand All @@ -47,7 +38,7 @@ class MusicRuntime {

void unlock(RuntimeContext const& rc, ChType ch);

bool step(RuntimeContext const& rc);
bool step(RuntimeContext const& rc, Frame &frame);

void repeatPattern(bool repeat);

Expand All @@ -73,9 +64,6 @@ class MusicRuntime {
int mOrderCounter;
int mRowCounter;

bool mHasNewPattern;
bool mHasNewRow;

bool mPatternRepeat;

Timer mTimer;
Expand Down
2 changes: 1 addition & 1 deletion include/trackerboy/export/Player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Player {


Engine &mEngine;
Engine::Frame mLastFrame;
Frame mLastFrame;
bool mPlaying;
ContextVariant mContext;

Expand Down
40 changes: 23 additions & 17 deletions include/trackerboy/trackerboy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,25 @@ enum class Panning : uint8_t {
middle
};

template <typename T>
constexpr T GB_CLOCK_SPEED = T(4194304);

constexpr float GB_FRAMERATE_DMG = 59.7f;
constexpr float GB_FRAMERATE_SGB = 61.1f;

// each channel has 5 registers
constexpr unsigned GB_CHANNEL_REGS = 5;

//
// Maximum frequency setting for channels 1, 2 and 3
//
constexpr uint16_t GB_MAX_FREQUENCY = 2047;

//
// CH3 waveram is 16 bytes
//
constexpr size_t GB_WAVERAM_SIZE = 16;

//
// The speed type determines the tempo during pattern playback. Its unit is
// frames per row in Q4.4 format. Speeds with a fractional component will
Expand All @@ -111,29 +130,16 @@ constexpr float speedToFloat(Speed speed) {
return speed * (1.0f / (1 << SPEED_FRACTION_BITS));
}

constexpr float speedToTempo(float speed, int rowsPerBeat = 4, float framerate = GB_FRAMERATE_DMG) {
return (framerate * 60.0f) / (speed * rowsPerBeat);
}

constexpr size_t TABLE_SIZE = 64;
constexpr size_t MAX_INSTRUMENTS = TABLE_SIZE;
constexpr size_t MAX_WAVEFORMS = TABLE_SIZE;
constexpr size_t MAX_PATTERNS = 256;

template <typename T>
constexpr T GB_CLOCK_SPEED = T(4194304);

constexpr float GB_FRAMERATE_DMG = 59.7f;
constexpr float GB_FRAMERATE_SGB = 61.1f;

// each channel has 5 registers
constexpr unsigned GB_CHANNEL_REGS = 5;

//
// Maximum frequency setting for channels 1, 2 and 3
//
constexpr uint16_t GB_MAX_FREQUENCY = 2047;

//
// CH3 waveram is 16 bytes
//
constexpr size_t GB_WAVERAM_SIZE = 16;



Expand Down
26 changes: 9 additions & 17 deletions libtrackerboy/engine/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ void Engine::setModule(Module const* mod) {

void Engine::reset() {
mMusicContext.reset();
mTime = 0;
}

void Engine::play(int orderNo, int patternRow) {
Expand All @@ -54,6 +53,7 @@ void Engine::play(int orderNo, int patternRow) {
}

mMusicContext.emplace(song, orderNo, patternRow, mPatternRepeat);
mTime = 0;
}
}

Expand Down Expand Up @@ -94,26 +94,19 @@ void Engine::repeatPattern(bool repeat) {
void Engine::step(Frame &frame) {

if (mMusicContext) {
frame.halted = mMusicContext->step(*mRc);
frame.startedNewRow = mMusicContext->hasNewRow();
frame.startedNewPattern = mMusicContext->hasNewPattern();
frame.order = mMusicContext->currentOrder();
frame.row = mMusicContext->currentRow();
frame.speed = mMusicContext->currentSpeed();
frame.time = mTime;
frame.halted = mMusicContext->step(*mRc, frame);

// increment timestamp for next frame
if (!frame.halted) {
++mTime;
}
} else {
// no runtime, do nothing
frame.halted = true;
frame.order = 0;
frame.row = 0;
frame.speed = 0;
}

frame.time = mTime;

// TODO: sound effects

// increment timestamp for next frame
++mTime;

}

void Engine::clearChannel(ChType ch) {
Expand All @@ -137,5 +130,4 @@ void Engine::clearChannel(ChType ch) {
}



}
Loading

0 comments on commit b394ddc

Please sign in to comment.