Skip to content
This repository has been archived by the owner on Jan 4, 2023. It is now read-only.

Commit

Permalink
Fixes for Music Chip issues #280, #279, #270, and #272.
Browse files Browse the repository at this point in the history
  • Loading branch information
jessefreeman committed May 25, 2020
1 parent f7548f5 commit 79865cf
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ function ConfigureModal:Open()
-- self.settingSpeenMinInputData.colorOffset = 32
-- self.settingSpeenMinInputData.disabledColorOffset = 34
self.settingSpeenMinInputData.min = 50
self.settingSpeenMinInputData.max = 400
self.settingSpeenMinInputData.max = 350
self.settingSpeenMinInputData.onAction = function(value)

value = tonumber(value)
Expand All @@ -419,7 +419,7 @@ function ConfigureModal:Open()
-- self.settingSpeenMaxInputData.colorOffset = 32
-- self.settingSpeenMaxInputData.disabledColorOffset = 34
self.settingSpeenMaxInputData.min = 50
self.settingSpeenMaxInputData.max = 400
self.settingSpeenMaxInputData.max = 350
self.settingSpeenMaxInputData.onAction = function(value)
-- gameEditor.pcgMaxTempo = tonumber(value)

Expand Down Expand Up @@ -984,8 +984,8 @@ function OnResetConfig()
gameEditor.pcgFunk = 5
gameEditor.pcgLayering = 5

gameEditor.pcgMinTempo = 120
gameEditor.pcgMaxTempo = 320
gameEditor.pcgMinTempo = 50
gameEditor.pcgMaxTempo = 120

gameEditor.scale = 1

Expand Down
15 changes: 13 additions & 2 deletions Disks/PixelVisionOS/System/Tools/MusicTool/code.lua
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ function Init()
-- Force the slider to have a different start value so it can be updated correctly when the first song loads up
songSliderData.value = -1

tempoStepper = editorUI:CreateNumberStepper({x = 120, y = 88}, 24, 0, 60, 480, "top", "Change the length of the pattern.")
tempoStepper = editorUI:CreateNumberStepper({x = 120, y = 88}, 24, 0, 1, 480, "top", "Change the length of the pattern.")
tempoStepper.onInputAction = OnTempoChange

table.insert(disableWhenPlaying, tempoStepper.backButton)
Expand Down Expand Up @@ -803,6 +803,17 @@ function OnSelectSongField(value)

local realIndex = currentSelectedSong - songScrollOffset

if (realIndex < 1 or realIndex > totalSongFields) then
local totalPatterns = #currentSongPatterns - totalSongFields

local scroll = (currentSelectedSong - 1) / totalPatterns
local scrollX = math.floor(scroll * songSliderData.size) + 11
songSliderData.handleX = scrollX
OnSongScroll(scroll)

realIndex = currentSelectedSong - songScrollOffset
end

LoadLoop(tonumber(songInputFields[realIndex].text))

OnSongScroll()
Expand Down Expand Up @@ -1159,7 +1170,7 @@ function Update(timeDelta)
local currentPattern = songData["pattern"]

if((currentSelectedSong - 1) ~= currentPattern and playMode ~= 3) then
OnSelectSongField(currentPattern)
OnSelectSongField(currentPattern - songScrollOffset)
editorUI.refreshTime = 0
end

Expand Down
2 changes: 1 addition & 1 deletion Disks/RunnerTools/BootTool/music.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"songs": [
{
"songName": "PV8",
"speedInBPM": 320,
"speedInBPM": 160,
"tracks":
[
{
Expand Down
4 changes: 2 additions & 2 deletions SDK/Editor/Audio/SfxrMusicGeneratorChip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@ public enum Scale
public int pcgLayering = 5;

//public int pcgLoopsToCreate = 4;
public int pcgMaxTempo = 320;
public int pcgMaxTempo = 120;

public int pcgMinTempo = 120;
public int pcgMinTempo = 50;
private int pcgPreviousNote; // phrasing reacts to previous notes

//private int pcgPreviousProgressionIndex = 0; // phrasing reacts to previous chords in progression
Expand Down
21 changes: 6 additions & 15 deletions SDK/Editor/Exporters/SongExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ public class SongExporter : AbstractExporter

// to avoid clipping, all tracks are a bit quieter since we ADD values - set to 1f for full mix
public float mixdownTrackVolume = 0.6f;
public float note_tick_s = 30.0f / 120.0f; // (30.0f/120.0f) = 120BPM eighth notes
public float note_tick_s = 15.0f / 120.0f; // (15.0f/120.0f) = 120BPM sixteenth notes
public float note_tick_s_odd;
private RawAudioData result;


public float
swing_rhythm_factor = 0.7f; //1.0f;//0.66666f; // how much "shuffle" - turnaround on the offbeat triplet
public float swing_rhythm_factor = 1.0f; //0.7f;//0.66666f; // how much "shuffle" - turnaround on the offbeat triplet

private RawAudioData[] trackresult;

Expand Down Expand Up @@ -89,7 +88,7 @@ public void ExportSong()
var totalNotesInSong = ncount; // total musical notes in song loops x notes

// TODO needed to increase this number to match the speed better but this should be test out more.
note_tick_s = 45.0f / songData.speedInBPM; // (30.0f/120.0f) = 120BPM eighth notes [tempo]
note_tick_s = 15.0f / songData.speedInBPM; // (30.0f/120.0f) = 120BPM eighth notes [tempo]
note_tick_s_odd = note_tick_s * swing_rhythm_factor; // small beat

var notedatalength = (int) (preRenderBitrate * 2f * note_tick_s_odd); // one note worth of stereo audio (x2)
Expand Down Expand Up @@ -138,26 +137,18 @@ public void ExportSong()
// generate one note worth of audio data
if (gotANote > 0 && instrument[tracknum] != null)
{
// FIXME TODO HACK: prerendered notes seem pitch shifted too high
// manually fudged - WHY WHY WHY - no idea why this works, but it does.
// this is the most heinous hack I've ever implemented and it hurts so bad
// it isn't worth anything: this is duct tape solution
var noteFUDGE = 1; //- 12; // shift how many semitones
var freqFUDGE = 0.0025f; //0.005f; // slightly off without this offset

//instrument[tracknum].Reset(true); // seems to do nothing

// doing this for every note played is insane:
// try a fresh new instrument (RAM and GC spammy)
//instrument[tracknum] = new SfxrSynth(); // shouldn't be required
instrument[tracknum] = new SfxrSynth(); // shouldn't be required, but for some reason it is
// set the params to current track's instrument string
instrument[tracknum].parameters
.SetSettingsString(soundChip.ReadSound(songData.tracks[tracknum].sfxID).param);
instrument[tracknum].parameters.SetSettingsString(soundChip.ReadSound(songData.tracks[tracknum].sfxID).param);


// pitch shift the sound to the correct musical note frequency
instrument[tracknum].parameters.startFrequency =
noteStartFrequency[gotANote + noteFUDGE] + freqFUDGE;
instrument[tracknum].parameters.startFrequency = noteStartFrequency[gotANote];


// maybe this will help
Expand Down
70 changes: 45 additions & 25 deletions SDK/Engine/Chips/Audio/MusicChip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ public class MusicChip : AbstractChip, IUpdate
public bool loopSong;
public int maxNoteNum = 127; // how many notes in these arrays below
public int maxTracks = 5; // max number of instruments playing notes
public float nextBeatTimestamp;

public float[] noteHZ; // a lookup table of all musical notes in Hz

public float[] noteStartFrequency; // same, but for sfxr frequency 0..1 range
protected float noteTickS = 30.0f / 120.0f; // (30.0f/120.0f) = 120BPM eighth notes
protected float noteTickSEven;
protected float noteTickSOdd;

protected float swingRhythmFactor = 1.0f;//0.7f;
protected float noteTickS = 15.0f / 120.0f; // (15.0f/120.0f) = 120BPM sixteenth notes
protected float noteTickSOdd; // small beat
protected float noteTickSEven; // long beat

public int preRenderBitrate = 44100; //48000; // should be 44100; FIXME TODO EXPERIMENTING W BUGFIX

Expand All @@ -73,9 +75,10 @@ public class MusicChip : AbstractChip, IUpdate
public SongData[] songs = new SongData[1];
// protected int songLoopCount = 0;

protected float swingRhythmFactor = 0.7f;

protected float time;
protected float time = 0;
public float nextBeatTimestamp = 0;
public const float maxDelta = 10; // This is so high to keep things accurate. Lower if songs are "skipping" / playing many beats all at once
public const float tickOffset = -0.0105f; // Set up to account for extra delay

public TrackerData[] trackerDataCollection = new TrackerData[0];
// public int tracksPerLoop = 8;
Expand Down Expand Up @@ -200,7 +203,7 @@ public TrackerData ActiveTrackerData
/// <param name="timeDelta"></param>
public void Update(int timeDelta)
{
// Need to conver the time to a float
// Need to convert the time to a float
time += timeDelta / 1000f;

songData["playing"] = Convert.ToInt32(songCurrentlyPlaying);
Expand All @@ -210,10 +213,18 @@ public void Update(int timeDelta)
{
if (time >= nextBeatTimestamp)
{
nextBeatTimestamp = noteTickSOdd;//(SequencerBeatNumber % 2 == 1 ? : noteTickSEven);

float delta = time - nextBeatTimestamp;
// If the time between when the note is supposed to be played and when it would be delayed to is too large, reset it.
if (delta > maxDelta)
{
nextBeatTimestamp = time + noteTickS;//(SequencerBeatNumber % 2 == 1 ? noteTickSOdd : noteTickSEven);
}
else
{
nextBeatTimestamp += noteTickS;
}
OnBeat();
time = 0;
// time = 0;
}

// If song is playing, update songData values
Expand Down Expand Up @@ -319,7 +330,7 @@ public void LoadPattern(int id)
public void PlayPatterns(int[] ids, bool loop = false, int startAt = 0, int? endAt = null)
{
// Create a new song data object for the pattern IDs
currentSong = new SongData {patterns = ids, start = startAt};
currentSong = new SongData { patterns = ids, start = startAt };

// Set the end of the song if a value has been supplied
if (endAt.HasValue) currentSong.end = endAt.Value;
Expand Down Expand Up @@ -367,31 +378,28 @@ protected void OnBeat()
// return;
// }
// }
var nextPattern = currentSong.NextPattern();


// Look to see if the next pattern is -1 and if looping is false
if (loopSong == false)
// Look to see if the next pattern is 0 (start) and if looping is false
if (loopSong == false && currentSong.currentPos == 0)
{
// Console.WriteLine("End of song " + loopSong + " " + songCurrentlyPlaying);
//
// if (loopSong == false)
// {
// Console.WriteLine("Stop song");
// Stop the song and return
songCurrentlyPlaying = false;
RewindSong();
nextBeatTimestamp = time;
return;
// }

//RewindSong();

}

// Console.WriteLine("Load new pattern");

// RewindSong();

// Get the next pattern
var nextPattern = currentSong.NextPattern();



// Load the next song in the playlist
LoadPattern(nextPattern);
Expand Down Expand Up @@ -428,7 +436,7 @@ protected void OnBeat()

public void UpdateNoteTickLengths()
{
noteTickS = 45.0f / ActiveTrackerData.speedInBPM; // (30.0f/120.0f) = 120BPM eighth notes [tempo]
noteTickS = 15.0f / ActiveTrackerData.speedInBPM + tickOffset; // (30.0f/120.0f) = 120BPM eighth notes [tempo]
noteTickSOdd = noteTickS * swingRhythmFactor; // small beat
noteTickSEven = noteTickS * 2 - noteTickSOdd; // long beat
}
Expand Down Expand Up @@ -458,9 +466,18 @@ public void StopSong()
/// Toggles the current playback state of the sequencer. If the song
/// is playing it will pause, if it is paused it will play
/// </summary>
public void PauseSong() // unused
public void PauseSong()
{
songCurrentlyPlaying = !songCurrentlyPlaying;
if (songCurrentlyPlaying)
{
songCurrentlyPlaying = false;
}
else
{
songCurrentlyPlaying = true;
// Reset next beat timestamp
nextBeatTimestamp = time;
}
}

protected void UpdateMusicNotes()
Expand Down Expand Up @@ -500,6 +517,9 @@ public void PlaySong(SongData songData, bool loop = false, int seekTo = 0)
// Seek to the pattern just before the desired id since we call NextPattern below
currentSong.SeekTo(seekTo - 1);

// Reset next beat timestamp
nextBeatTimestamp = time;

// Save the loop value
loopSong = loop;

Expand Down
2 changes: 1 addition & 1 deletion SDK/Engine/Data/TrackerData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public int totalNotes
public int speedInBPM
{
get => _speedInBPM;
set => _speedInBPM = MathHelper.Clamp(value, 60, 480);
set => _speedInBPM = MathHelper.Clamp(value, 1, 480);
}

/// <summary>
Expand Down

0 comments on commit 79865cf

Please sign in to comment.