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

Commit

Permalink
Creating new SoundChip and moving Sfxr logic over to a special chip.
Browse files Browse the repository at this point in the history
  • Loading branch information
jessefreeman committed Oct 4, 2020
1 parent b173829 commit dfd5f79
Show file tree
Hide file tree
Showing 25 changed files with 574 additions and 192 deletions.
6 changes: 3 additions & 3 deletions Disks/PixelVisionOS/System/Tools/WorkspaceTool/saves.json
Expand Up @@ -3,10 +3,10 @@
"GameChip":
{
"savedData":{
"sessionID": "202010040931447638",
"lastPath": "/Workspace/Projects/SaveTest/",
"selection": "0",
"scrollPos": "0",
"selection": "6",
"lastPath": "/Workspace/Demos/ControllerDemo/"
"sessionID": "202010041020342055"
}
}
}
3 changes: 2 additions & 1 deletion SDK/Editor/Audio/SfxrMusicGeneratorChip.cs
Expand Up @@ -22,6 +22,7 @@
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using PixelVision8.Engine;
using PixelVision8.Engine.Chips;

namespace PixelVision8.Runner.Chips
{
Expand Down Expand Up @@ -283,7 +284,7 @@ public void GenerateSong(IEngine chips)
bool wasFunkyLastNote;

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

var activeTrackerData = musicChip.ActiveTrackerData;

Expand Down
33 changes: 18 additions & 15 deletions SDK/Editor/Editors/GameEditor.cs
Expand Up @@ -64,7 +64,7 @@ public class GameEditor
protected DesktopRunner runner;

protected IServiceLocator serviceManager;
private SoundChip soundChip;
private SfxrSoundChip soundChip;
private SpriteChip spriteChip;
private PixelVisionEngine targetGame;
private TilemapChip tilemapChip;
Expand Down Expand Up @@ -99,7 +99,7 @@ public virtual List<string> DefaultChips
typeof(FontChip).FullName,
typeof(ControllerChip).FullName,
typeof(DisplayChip).FullName,
typeof(SoundChip).FullName,
typeof(SfxrSoundChip).FullName,
typeof(MusicChip).FullName,
typeof(LuaGameChip).FullName
};
Expand Down Expand Up @@ -159,17 +159,17 @@ public bool Load(string path, SaveFlags[] flags)
try
{
targetGame = new PixelVisionEngine(serviceManager, DefaultChips.ToArray());

// targetGame.Init();

runner.ParseFiles(files, targetGame, saveFlags);
}
catch
catch(Exception e)
{
// Console.WriteLine("Game Editor Load Error:\n"+e.Message);
Console.WriteLine("Game Editor Load Error:\n"+e.Message);

return false;
}


Reset();


Expand All @@ -184,7 +184,7 @@ public void Reset()
// targetGame = new PixelVisionEngine(null, null, runner.defaultChips.ToArray());

// Configure the game editor now that all the chips have been loaded
gameChip = targetGame.GameChip;
gameChip = targetGame.GameChip as GameChip;

// Since the game is not attached to a runner it will throw an error when trying to load lua serivce
try
Expand All @@ -209,7 +209,7 @@ public void Reset()
spriteChip = targetGame.SpriteChip;
fontChip = targetGame.FontChip;
tilemapChip = targetGame.TilemapChip;
soundChip = targetGame.SoundChip;
soundChip = targetGame.SoundChip as SfxrSoundChip;
musicChip = targetGame.MusicChip; // TODO need to create a SfxrMusicChip

// Console.WriteLine("MC Tracks " + musicChip.totalTracks);
Expand Down Expand Up @@ -1136,7 +1136,7 @@ public bool LoadFont(string path)

targetGame = new PixelVisionEngine(serviceManager, chips)
{
ColorChip = {unique = true}, FontChip = {unique = false, pages = 1}, Name = path
FontChip = {unique = false, pages = 1}, Name = path
};

var pngReader = new PNGReader(imageBytes, targetGame.ColorChip.maskColor)
Expand Down Expand Up @@ -1252,7 +1252,10 @@ public void WriteMetadata(string key, string value)

public string Sound(int id, string data = null)
{
return gameChip.Sound(id, data);
if (data != null) soundChip.UpdateSound(id, data);
//
return soundChip.ReadSound(id).param;
// return gameChip.Sound(id, data);
}

/// <summary>
Expand Down Expand Up @@ -1502,7 +1505,7 @@ public int TotalChannels(int? total = null)
public void GenerateSound(int index, int template)
{
// Create a tmp synth parameter
var settings = new SfxrSynth().parameters;
var settings = new SfxrSynthChannel().parameters;

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

gameChip.Sound(index, settings.GetSettingsString());
soundChip.UpdateSound(index, settings.GetSettingsString());
}

/// <summary>
Expand All @@ -1557,7 +1560,7 @@ public void NewSound(int id)
{
// var settings = new SfxrSynth().parameters;
// TODO I don't like that this is a static value on the SoundData class
gameChip.Sound(id, SoundData.DEFAULT_SOUND_PARAM); //settings.GetSettingsString());
((SfxrSoundChip)soundChip).UpdateSound(id, SoundData.DEFAULT_SOUND_PARAM); //settings.GetSettingsString());
}

/// <summary>
Expand Down Expand Up @@ -1757,7 +1760,7 @@ public void PreviewInstrument(int id)
// Just need to get a reference to any track setting for this data
var soundData = songGenerator.trackSettings[0].ReadInstrumentSoundData(id);

if (soundData != null) soundChip.PlayRawSound(soundData);
if (soundData != null) soundChip .PlayRawSound(soundData);
}

/// <summary>
Expand Down Expand Up @@ -2300,7 +2303,7 @@ public void ExportSFX(int id, WorkspacePath workspacePath)
workspacePath = workspacePath.AppendFile(sfx.name + ".wav");

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

Expand Down
10 changes: 5 additions & 5 deletions SDK/Editor/Exporters/SongExporter.cs
Expand Up @@ -29,7 +29,7 @@ public class SongExporter : AbstractExporter
private readonly MusicChip musicChip;

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

// to avoid clipping, all tracks are a bit quieter since we ADD values - set to 1f for full mix
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;
this.soundChip = soundChip as SfxrSoundChip;

this.patterns = patterns;
}
Expand Down Expand Up @@ -111,15 +111,15 @@ public void ExportSong()
preRenderBitrate);
}

var instrument = new SfxrSynth[songData.totalTracks];
var instrument = new SfxrSynthChannel[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 SfxrSynth();
if (instrument[tracknum] == null) instrument[tracknum] = new SfxrSynthChannel();

var songdataCurrentPos = newStartPos;
trackresult[tracknum].Resize(newLength);
Expand All @@ -142,7 +142,7 @@ public void ExportSong()

// 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, but for some reason it is
instrument[tracknum] = new SfxrSynthChannel(); // 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);

Expand Down
3 changes: 2 additions & 1 deletion SDK/Editor/Exporters/SoundExporter.cs
Expand Up @@ -20,6 +20,7 @@

using System.Text;
using PixelVision8.Engine;
using PixelVision8.Engine.Chips;
using PixelVision8.Runner.Utils;

namespace PixelVision8.Runner.Exporters
Expand Down Expand Up @@ -52,7 +53,7 @@ public override void CalculateSteps()

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

sb.Append("\"version\":\"v2\",");
JsonUtil.GetLineBreak(sb, 1);
Expand Down
2 changes: 1 addition & 1 deletion SDK/Editor/Exporters/SystemExporter.cs
Expand Up @@ -352,7 +352,7 @@ private void SerializeSoundChip(SoundChip soundChip)
{
// Console.WriteLine("Channel "+i +" type "+soundChip.ChannelType(i));

sb.Append((int) soundChip.ChannelType(i));
sb.Append((int) ((SfxrSoundChip)soundChip).ChannelType(i));
if (i < total - 1) sb.Append(",");
}

Expand Down
8 changes: 0 additions & 8 deletions SDK/Engine/Chips/Audio/IChannel.cs
Expand Up @@ -42,13 +42,5 @@ public interface IChannel
/// </summary>
void Stop();

/// <summary>
/// Handles setting a wave type to the channel. This will
/// lock the channel when manually set using this API. To
/// unlock, pass in WaveShape.None.
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
WaveType ChannelType(WaveType? type);
}
}
63 changes: 63 additions & 0 deletions SDK/Engine/Chips/Audio/ISoundChip.cs
@@ -0,0 +1,63 @@
//
// Copyright (c) Jesse Freeman, Pixel Vision 8. All rights reserved.
//
// Licensed under the Microsoft Public License (MS-PL) except for a few
// portions of the code. See LICENSE file in the project root for full
// license information. Third-party libraries used by Pixel Vision 8 are
// under their own licenses. Please refer to those libraries for details
// on the license they use.
//
// Contributors
// --------------------------------------------------------
// This is the official list of Pixel Vision 8 contributors:
//
// Jesse Freeman - @JesseFreeman
// Christina-Antoinette Neofotistou @CastPixel
// Christer Kaitila - @McFunkypants
// Pedro Medeiros - @saint11
// Shawn Rakowski - @shwany
//

namespace PixelVision8.Engine.Chips
{

public interface ISoundChip
{
/// <summary>
/// The total number of <see cref="Channels" /> available for playing
/// back sounds.
/// </summary>
int totalChannels { get; set; }

/// <summary>
/// The total number of <see cref="Sounds" /> in the collection.
/// </summary>
int TotalSounds { get; set; }

/// <summary>
/// This method plays back a sound on a specific channel. The
/// SoundChip has a limit of active <see cref="SfxrSoundChip.Channels" />
/// so playing a sound effect while another was is playing on the same
/// <paramref name="channel" /> will cancel it out and replace with the
/// new sound.
/// </summary>
/// <param name="index">
/// The ID of the sound in the SoundCollection.
/// </param>
/// <param name="channel">
/// The channel the sound should play back on.
/// </param>
void PlaySound(int index, int channelID = 0, float? frequency = null);

bool IsChannelPlaying(int channelID);
void StopSound(int channel);
void Shutdown();
void AddSample(string name, byte[] bytes);

/// <summary>
/// Goes through the sounds and the sound bank and adds the sample byte data
/// </summary>
void RefreshSamples();

}
}
9 changes: 4 additions & 5 deletions SDK/Engine/Chips/Audio/MusicChip.cs
@@ -1,4 +1,4 @@
//
//
// Copyright (c) Jesse Freeman, Pixel Vision 8. All rights reserved.
//
// Licensed under the Microsoft Public License (MS-PL) except for a few
Expand All @@ -22,8 +22,6 @@
using System.Collections.Generic;
using Microsoft.Xna.Framework;

//using UnityEngine;

namespace PixelVision8.Engine.Chips
{
/// <summary>
Expand Down Expand Up @@ -194,7 +192,7 @@ public TrackerData ActiveTrackerData
}
}

protected SoundChip SoundChip => engine.SoundChip;
protected ISoundChip SoundChip => engine.SoundChip;

/// <summary>
/// Updates the sequencer if it is in playback mode. This will
Expand Down Expand Up @@ -255,7 +253,7 @@ public virtual TrackerData CreateNewTrackerData(string name, int tracks = 5)
/// </summary>
public override void Configure()
{
engine.MusicChip = this;
((PixelVisionEngine)engine).MusicChip = this;

//engine.chipManager.AddToUpdateList(this);

Expand Down Expand Up @@ -532,5 +530,6 @@ public void PlaySong(SongData songData, bool loop = false, int seekTo = 0)
// else
songCurrentlyPlaying = true;
}

}
}
2 changes: 1 addition & 1 deletion SDK/Engine/Chips/Audio/Sfxr/SfxrParams.cs
@@ -1,4 +1,4 @@
// SfxrSynth
// SfxrSynth
//
// Copyright 2010 Thomas Vian
// Copyright 2013 Zeh Fernando
Expand Down

0 comments on commit dfd5f79

Please sign in to comment.