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

Commit

Permalink
Cleaning up the last of the Sfxr audio classes and merging down into …
Browse files Browse the repository at this point in the history
…the base audio classes for the core engine.
  • Loading branch information
jessefreeman committed Mar 20, 2021
1 parent 1ff7f38 commit cfbfcea
Show file tree
Hide file tree
Showing 20 changed files with 221 additions and 172 deletions.
Expand Up @@ -17,6 +17,7 @@ function MusicTool:OnExportSong(songID, showWarning)

gameEditor:ExportSong(self.rootDirectory, songID)
target.onParentClose()

end,
key = Keys.Enter,
tooltip = "Press 'enter' to export the song"
Expand Down Expand Up @@ -60,6 +61,7 @@ function MusicTool:OnExportAllSongs()
installRoot = rootPath
target.onParentClose()

print("Trigger Export")
pixelVisionOS:RegisterUI({name = "UpdateExportLoop"}, "UpdateExport", self)

end,
Expand Down Expand Up @@ -159,6 +161,7 @@ end
-- TODO this needs to be wired up
function MusicTool:UpdateExport()

print("Update Export")
self.installingTime = self.installingTime + editorUI.timeDelta

if(self.installingTime > self.installingDelay) then
Expand Down
2 changes: 1 addition & 1 deletion Disks/PixelVisionOS/System/Tools/SFXTool/code-sfx-tool.lua
Expand Up @@ -137,7 +137,7 @@ function SFXTool:Draw()

-- TODO this is not working
if(gameEditor:IsChannelPlaying(0)) then
DrawMeta(self.powerMetaSpriteId, 16, 88)
DrawMetaSprite(self.powerMetaSpriteId, 16, 88)
end

end
Expand Down
32 changes: 17 additions & 15 deletions Projects/PixelVision8/PixelVision8.CoreDesktop.csproj
Expand Up @@ -126,6 +126,15 @@
<Compile Include="..\..\SDK\Player\Chips\Audio\SoundChannel.MonoGame.cs">
<Link>SDK\Player\Chips\Audio\SoundChannel.MonoGame.cs</Link>
</Compile>
<Compile Include="..\..\SDK\Player\Chips\Audio\SoundChannel.Sfxr.cs">
<Link>SDK\Player\Chips\Audio\SoundChannel.Sfxr.cs</Link>
</Compile>
<Compile Include="..\..\SDK\Player\Chips\Audio\SoundChip.Sfxr.cs">
<Link>SDK\Player\Chips\Audio\SoundChip.Sfxr.cs</Link>
</Compile>
<Compile Include="..\..\SDK\Player\Chips\Audio\WaveType.cs">
<Link>SDK\Player\Chips\Audio\WaveType.cs</Link>
</Compile>
<Compile Include="..\..\SDK\Player\Chips\Game\GameChip.Log.cs">
<Link>SDK\Player\Chips\Game\GameChip.Log.cs</Link>
</Compile>
Expand All @@ -135,6 +144,9 @@
<Compile Include="..\..\SDK\Player\Data\Rectangle.cs">
<Link>SDK\Player\Data\Rectangle.cs</Link>
</Compile>
<Compile Include="..\..\SDK\Player\Data\SoundData.Sfxr.cs">
<Link>SDK\Player\Data\SoundData.Sfxr.cs</Link>
</Compile>
<Compile Include="..\..\SDK\Runner\Parsers\SystemParser.ColorChip.cs">
<Link>SDK\Runner\Parsers\SystemParser.ColorChip.cs</Link>
</Compile>
Expand Down Expand Up @@ -256,21 +268,6 @@
<Compile Include="..\..\SDK\Player\Chips\Audio\MusicChip.cs">
<Link>SDK\Player\Chips\Audio\MusicChip.cs</Link>
</Compile>
<Compile Include="..\..\SDK\Player\Chips\Audio\Sfxr\SfxrParams.cs">
<Link>SDK\Player\Chips\Audio\Sfxr\SfxrParams.cs</Link>
</Compile>
<Compile Include="..\..\SDK\Player\Chips\Audio\Sfxr\SfxrSoundChip.cs">
<Link>SDK\Player\Chips\Audio\Sfxr\SfxrSoundChip.cs</Link>
</Compile>
<Compile Include="..\..\SDK\Player\Chips\Audio\Sfxr\SfxrSynthChannel.cs">
<Link>SDK\Player\Chips\Audio\Sfxr\SfxrSynthChannel.cs</Link>
</Compile>
<Compile Include="..\..\SDK\Player\Chips\Audio\Sfxr\SoundData.Sfxr.cs">
<Link>SDK\Player\Chips\Audio\Sfxr\SoundData.Sfxr.cs</Link>
</Compile>
<Compile Include="..\..\SDK\Player\Chips\Audio\SoundChannel.cs">
<Link>SDK\Player\Chips\Audio\SoundChannel.cs</Link>
</Compile>
<Compile Include="..\..\SDK\Player\Chips\Audio\SoundChip.cs">
<Link>SDK\Player\Chips\Audio\SoundChip.cs</Link>
</Compile>
Expand Down Expand Up @@ -1244,5 +1241,10 @@
<None Remove="..\..\SDK\MonoVision\Platform\Graphics\Effect\Resources\**" />
<None Remove="..\..\SDK\MonoVision\Dependencies\**" />
</ItemGroup>
<ItemGroup>
<Folder Include="..\..\SDK\Player\Chips\Audio\Sfxr">
<Link>SDK\Player\Chips\Audio\Sfxr</Link>
</Folder>
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion SDK/Editor/Audio/SfxrMusicGeneratorChip.cs
Expand Up @@ -282,7 +282,7 @@ public void GenerateSong(PixelVision chips, bool resetInstruments = false)
bool wasFunkyLastNote;

var musicChip = chips.MusicChip;
var soundChip = chips.SoundChip as SfxrSoundChip;
var soundChip = chips.SoundChip;

var activeTrackerData = musicChip.ActiveTrackerData;

Expand Down
22 changes: 11 additions & 11 deletions SDK/Editor/Editors/GameEditor.cs
Expand Up @@ -58,7 +58,7 @@ public class GameEditor
protected DesktopRunner runner;

protected IServiceLocator serviceManager;
private SfxrSoundChip soundChip;
private SoundChip soundChip;
private SpriteChip spriteChip;
private PixelVision targetGame;
private TilemapChip tilemapChip;
Expand Down Expand Up @@ -93,7 +93,7 @@ public virtual List<string> DefaultChips
typeof(FontChip).FullName,
typeof(ControllerChip).FullName,
typeof(DisplayChip).FullName,
typeof(SfxrSoundChip).FullName,
typeof(SoundChip).FullName,
typeof(MusicChip).FullName,
typeof(LuaGameChip).FullName
};
Expand Down Expand Up @@ -206,7 +206,7 @@ public void Reset()
spriteChip = targetGame.SpriteChip;
fontChip = targetGame.FontChip;
tilemapChip = targetGame.TilemapChip;
soundChip = targetGame.SoundChip as SfxrSoundChip;
soundChip = targetGame.SoundChip;
musicChip = targetGame.MusicChip; // TODO need to create a SfxrMusicChip

// Console.WriteLine("MC Tracks " + musicChip.totalTracks);
Expand Down Expand Up @@ -1498,7 +1498,7 @@ public int TotalChannels(int? total = null)
public void GenerateSound(int index, int template)
{
// Create a tmp synth parameter
var settings = new SfxrSynthChannel().parameters;
var settings = new SoundChannel().parameters;

// Apply sound template
switch (template)
Expand Down Expand Up @@ -1529,7 +1529,7 @@ public void GenerateSound(int index, int template)
break;
}

soundChip.UpdateSound(index, settings.GetSettingsString());
soundChip.UpdateSound(index, settings.param);
}

/// <summary>
Expand All @@ -1538,10 +1538,10 @@ public void GenerateSound(int index, int template)
/// <param name="id"></param>
public void Mutate(int id)
{
var param = new SfxrParams();
param.SetSettingsString(soundChip.ReadSound(id).param);
param.Mutate();
soundChip.UpdateSound(id, param.GetSettingsString());
var data = new SoundData("Untitled", soundChip.ReadSound(id).param);
// param.SetSettingsString();
data.Mutate();
soundChip.UpdateSound(id, data.param);
// soundChip.ReadSound(id).Mutate();
}

Expand Down Expand Up @@ -2297,8 +2297,8 @@ public void ExportSFX(int id, WorkspacePath workspacePath)
workspacePath = workspacePath.AppendFile(sfx.name + ".wav");

// TODO need to wire this up
var synth = new SfxrSynthChannel();
synth.parameters.SetSettingsString(sfx.param);
var synth = new SoundChannel();
synth.parameters.param = sfx.param;
// var stream = workspace.CreateFile(path);

var files = new Dictionary<string, byte[]>
Expand Down
4 changes: 2 additions & 2 deletions SDK/Editor/Exporters/SfxrSoundExporter.cs
Expand Up @@ -29,13 +29,13 @@ public class SfxrSoundExporter : AbstractExporter
{
private readonly PixelVision targetEngine;
private StringBuilder sb;
private SfxrSoundChip _sfxrSoundChip;
private SoundChip _sfxrSoundChip;

public SfxrSoundExporter(string fileName, PixelVision targetEngine) : base(fileName)
{
this.targetEngine = targetEngine;

_sfxrSoundChip = targetEngine.SoundChip as SfxrSoundChip;
_sfxrSoundChip = targetEngine.SoundChip;

// CalculateSteps();
}
Expand Down
29 changes: 13 additions & 16 deletions SDK/Editor/Exporters/SongExporter.cs
Expand Up @@ -28,20 +28,20 @@ public class SongExporter : AbstractExporter
private readonly MusicChip musicChip;

private readonly int[] patterns;
private readonly SfxrSoundChip soundChip;
private readonly SoundChip soundChip;
private int currentPattern;

// 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 = 15.0f / 120.0f; // (15.0f/120.0f) = 120BPM sixteenth notes
public float note_tick_s_odd;
private SfxrSynthChannel result;
private SoundChannel result;


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

private SfxrSynthChannel[] trackresult;
private SoundChannel[] trackresult;

// int songdataCurrentPos = 0;

Expand All @@ -52,7 +52,7 @@ public SongExporter(string path, MusicChip musicChip, SoundChip soundChip, int[]

// Save references to the currentc chips
this.musicChip = musicChip;
this.soundChip = soundChip as SfxrSoundChip;
this.soundChip = soundChip;

this.patterns = patterns;
}
Expand Down Expand Up @@ -104,29 +104,28 @@ public void ExportSong()
if (trackresult == null)
{
// all the tracks we need - an array of audioclips that will be merged into result
trackresult = new SfxrSynthChannel[tcount];
trackresult = new SoundChannel[tcount];

for (var i = 0; i < tcount; i++)
trackresult[i] = new SfxrSynthChannel(0, 1,
trackresult[i] = new SoundChannel(0, 1,
preRenderBitrate);
}

var instrument = new SfxrSynthChannel[songData.totalTracks];
var instrument = new SoundChannel[songData.totalTracks];

var newStartPos = trackresult[0].samples / 2;
var newLength = trackresult[0].samples + songdatalength;


for (tracknum = 0; tracknum < tcount; tracknum++)
{
if (instrument[tracknum] == null) instrument[tracknum] = new SfxrSynthChannel();
if (instrument[tracknum] == null) instrument[tracknum] = new SoundChannel();

var songdataCurrentPos = newStartPos;
trackresult[tracknum].Resize(newLength);

// set the params to current track's instrument string
instrument[tracknum].parameters
.SetSettingsString(soundChip.ReadSound(songData.tracks[tracknum].sfxID).param);
instrument[tracknum].parameters.param = soundChip.ReadSound(songData.tracks[tracknum].sfxID).param;

// Loop through all of the notes in the track
for (notenum = 0; notenum < ncount; notenum++)
Expand All @@ -141,11 +140,9 @@ public void ExportSong()

// doing this for every note played is insane:
// try a fresh new instrument (RAM and GC spammy)
instrument[tracknum] =
new SfxrSynthChannel(); // shouldn't be required, but for some reason it is
instrument[tracknum] = new SoundChannel(); // 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.param = soundChip.ReadSound(songData.tracks[tracknum].sfxID).param;


// pitch shift the sound to the correct musical note frequency
Expand Down Expand Up @@ -210,7 +207,7 @@ private void CreateSongByteData()
}

// pre-rendered waveforms - OPTIMIZATION - SLOW SLOW SLOW - FIXME
public SfxrSynthChannel MixdownAudioClips(params SfxrSynthChannel[] clips)
public SoundChannel MixdownAudioClips(params SoundChannel[] clips)
{
if (clips == null || clips.Length == 0) return null;

Expand Down Expand Up @@ -254,7 +251,7 @@ public SfxrSynthChannel MixdownAudioClips(params SfxrSynthChannel[] clips)
// stereo
//AudioClip result = AudioClip.Create("MixdownSTEREO", length / 2, 2, preRenderBitrate, false);
// mono
var result = new SfxrSynthChannel(length / 2, 1, preRenderBitrate);
var result = new SoundChannel(length / 2, 1, preRenderBitrate);
result.SetData(data); // TODO: we can get a warning here: data too large to fit: discarded x samples
// the truncation can happen with a large sustain of a note that could go on after the song is over
// one solution is to pad the end with 4sec of 0000s then maybe search and TRIM
Expand Down
2 changes: 1 addition & 1 deletion SDK/Editor/Exporters/SoundExporter.cs
Expand Up @@ -53,7 +53,7 @@ public override void CalculateSteps()

private void SaveGameData()
{
var soundChip = _targetPlay.SoundChip as SfxrSoundChip;
var soundChip = _targetPlay.SoundChip;

sb.Append("\"version\":\"v2\",");
JsonUtil.GetLineBreak(sb, 1);
Expand Down
4 changes: 2 additions & 2 deletions SDK/Editor/Exporters/SystemExporter.cs
Expand Up @@ -72,7 +72,7 @@ public override void CalculateSteps()

// Serialize Sound
if (engine.SoundChip != null /* && engine.SoundChip.export*/)
Steps.Add(delegate { SerializeSoundChip(engine.SoundChip as SfxrSoundChip); });
Steps.Add(delegate { SerializeSoundChip(engine.SoundChip); });

// Serialize Sprite
if (engine.SpriteChip != null)
Expand Down Expand Up @@ -327,7 +327,7 @@ private void SerializeMusicChip(MusicChip musicChip)
CurrentStep++;
}

private void SerializeSoundChip(SfxrSoundChip soundChip)
private void SerializeSoundChip(SoundChip soundChip)
{
JsonUtil.GetLineBreak(sb);
sb.Append("\"SoundChip\":");
Expand Down
2 changes: 1 addition & 1 deletion SDK/Lua/Chips/Game/LuaGameChip.Sound.cs
Expand Up @@ -17,7 +17,7 @@ public void RegisterSound()
LuaScript.Globals["PlaySound"] = new Action<int, int>(PlaySound);
LuaScript.Globals["StopSound"] = new Action<int>(StopSound);
LuaScript.Globals["PlayRawSound"] =
new Action<string, int, float>(((SfxrSoundChip) SoundChip).PlayRawSound);
new Action<string, int, float>(SoundChip.PlayRawSound);

LuaScript.Globals["IsChannelPlaying"] = new Func<int, bool>(IsChannelPlaying);
LuaScript.Globals["PlayPattern"] = new Action<int, bool>(PlayPattern);
Expand Down
36 changes: 0 additions & 36 deletions SDK/Player/Chips/Audio/Sfxr/SoundData.Sfxr.cs

This file was deleted.

0 comments on commit cfbfcea

Please sign in to comment.