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

Commit

Permalink
Cleaning up sound chip to better support wav samples.
Browse files Browse the repository at this point in the history
  • Loading branch information
jessefreeman committed Feb 5, 2021
1 parent 4d6b4e3 commit 8a7eb2e
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 266 deletions.
2 changes: 0 additions & 2 deletions PixelVision8.CoreDesktop.csproj
Expand Up @@ -72,8 +72,6 @@
<Compile Include="SDK\Editor\Services\ScreenshotService.cs" />
<Compile Include="SDK\Editor\Services\WorkspaceServicePlus.cs" />
<Compile Include="SDK\Player\Chips\AbstractChip.cs" />
<Compile Include="SDK\Player\Chips\Audio\IChannel.cs" />
<Compile Include="SDK\Player\Chips\Audio\ISoundChip.cs" />
<Compile Include="SDK\Player\Chips\Audio\MusicChip.cs" />
<Compile Include="SDK\Player\Chips\Audio\Sfxr\SfxrParams.cs" />
<Compile Include="SDK\Player\Chips\Audio\Sfxr\SfxrSoundChip.cs" />
Expand Down
6 changes: 3 additions & 3 deletions SDK/Editor/Editors/GameEditor.cs
Expand Up @@ -1486,9 +1486,9 @@ public int TotalSounds(int? total = null)
/// <returns></returns>
public int TotalChannels(int? total = null)
{
if (total.HasValue) soundChip.totalChannels = total.Value;
if (total.HasValue) soundChip.TotalChannels = total.Value;

return soundChip.totalChannels;
return soundChip.TotalChannels;
}

/// <summary>
Expand Down Expand Up @@ -1569,7 +1569,7 @@ public string SoundLabel(int index, string value = null)
{
soundChip.UpdateLabel(index, value);

soundChip.RefreshSamples();
// soundChip.RefreshSamples();
}

return soundChip.ReadLabel(index);
Expand Down
2 changes: 1 addition & 1 deletion SDK/Editor/Exporters/SongExporter.cs
Expand Up @@ -45,7 +45,7 @@ public float

// int songdataCurrentPos = 0;

public SongExporter(string path, MusicChip musicChip, ISoundChip soundChip, int[] patterns) : base(path)
public SongExporter(string path, MusicChip musicChip, SoundChip soundChip, int[] patterns) : base(path)
{
// Rebuild the path by adding the active song name and wav extension
fileName = path;
Expand Down
4 changes: 2 additions & 2 deletions SDK/Editor/Exporters/SystemExporter.cs
Expand Up @@ -336,7 +336,7 @@ private void SerializeSoundChip(SfxrSoundChip soundChip)
JsonUtil.GetLineBreak(sb, 1);

sb.Append("\"totalChannels\":");
sb.Append(soundChip.totalChannels);
sb.Append(soundChip.TotalChannels);
sb.Append(",");
JsonUtil.GetLineBreak(sb, 1);

Expand All @@ -347,7 +347,7 @@ private void SerializeSoundChip(SfxrSoundChip soundChip)

sb.Append("\"channelTypes\":[");

var total = soundChip.totalChannels;
var total = soundChip.TotalChannels;

for (var i = 0; i < total; i++)
{
Expand Down
2 changes: 1 addition & 1 deletion SDK/Editor/Services/GameDataExportService.cs
Expand Up @@ -110,7 +110,7 @@ public void ExportGame(string path, PixelVision engine, FileFlags fileFlags, boo
StartExport(useSteps);
}

public void ExportSong(string path, MusicChip musicChip, ISoundChip soundChip, int[] patterns)
public void ExportSong(string path, MusicChip musicChip, SoundChip soundChip, int[] patterns)
{
Restart();

Expand Down
45 changes: 0 additions & 45 deletions SDK/Player/Chips/Audio/IChannel.cs

This file was deleted.

63 changes: 0 additions & 63 deletions SDK/Player/Chips/Audio/ISoundChip.cs

This file was deleted.

12 changes: 6 additions & 6 deletions SDK/Player/Chips/Audio/MusicChip.cs
Expand Up @@ -24,10 +24,10 @@

namespace PixelVision8.Player
{
public partial interface IPlayerChips
{
public MusicChip MusicChip { get; set; }
}
// public partial interface IPlayerChips
// {
// public MusicChip MusicChip { get; set; }
// }

/// <summary>
/// The MusicChpip is a sequencer for playing back ISoundData. It
Expand Down Expand Up @@ -172,7 +172,7 @@ public int totalNotes
}
}

public int totalTracks => SoundChip.totalChannels;
public int totalTracks => SoundChip.TotalChannels;
// {
// get => _totalTracks;
// set
Expand Down Expand Up @@ -200,7 +200,7 @@ public TrackerData ActiveTrackerData
}
}

protected ISoundChip SoundChip => Player.SoundChip;
protected SoundChip SoundChip => Player.SoundChip;

/// <summary>
/// Updates the sequencer if it is in playback mode. This will
Expand Down
10 changes: 8 additions & 2 deletions SDK/Player/Chips/Audio/Sfxr/SfxrSoundChip.cs
Expand Up @@ -48,12 +48,12 @@ public virtual void UpdateSound(int index, string param)
/// create new sound instances that implement the ISoundData interface.
/// </summary>
/// <returns></returns>
public override IChannel CreateSoundChannel()
public override SoundChannel CreateSoundChannel()
{
return new SfxrSynthChannel();
}

public override SoundData CreateSoundData(string name)
public override SoundData CreateSoundData(string name, byte[] bytes = null)
{
return new SfxSoundData(name);
}
Expand All @@ -71,6 +71,12 @@ public override SoundData CreateSoundData(string name)
/// </returns>
public SfxSoundData ReadSound(int id)
{
if (id < 0 || id >= TotalSounds)
return null;

if (Sounds[id] == null)
Sounds[id] = CreateSoundData("Untitled" + id.ToString("D2"));

return Sounds[id] as SfxSoundData;
}

Expand Down
29 changes: 7 additions & 22 deletions SDK/Player/Chips/Audio/Sfxr/SfxrSynthChannel.cs
Expand Up @@ -35,8 +35,8 @@ public class SfxrSynthChannel : SoundChannel

// private int _overtones; // Minimum frequency before stopping

private readonly Dictionary<string, SoundEffectInstance> wavCache =
new Dictionary<string, SoundEffectInstance>();
// private readonly Dictionary<string, SoundEffectInstance> wavCache =
// new Dictionary<string, SoundEffectInstance>();

private float _changeAmount; // Amount to change the note by

Expand Down Expand Up @@ -171,6 +171,8 @@ public override void Play(SoundData soundData, float? frequency = null)
// Stop any playing sound
Stop();

// TODO this logic isn't working correctly. Need to double check the cache

// Clear the last sound instance
_soundInstance = null;

Expand Down Expand Up @@ -411,13 +413,12 @@ public void CacheSound()
{
Stop();


var paramKey = parameters.GetSettingsString();


if (wavCache.ContainsKey(paramKey))
if (SoundInstanceCache.ContainsKey(paramKey))
{
_soundInstance = wavCache[paramKey];
_soundInstance = SoundInstanceCache[paramKey];
}
else
{
Expand All @@ -442,7 +443,7 @@ public void CacheSound()
_soundInstance = soundEffect.CreateInstance();
}

wavCache[paramKey] = _soundInstance;
SoundInstanceCache[paramKey] = _soundInstance;
}
}

Expand Down Expand Up @@ -881,21 +882,5 @@ private bool SynthWave(float[] __buffer, int __bufferPos, uint __length)
return false;
}

/// <summary>
/// Returns a random value: 0 <= n < 1
/// </summary>
/// <returns></returns>
// private float parameters.GetRandom()
// {
// // We can't use Unity's Random.value because it cannot be called from a separate thread
// // (We get the error "get_value can only be called from the main thread" when this is called to generate the sound data)
// return (float) (_random.NextDouble() % 1);
// }
public void Dispose()
{
_soundInstance?.Dispose();

foreach (var wav in wavCache) wav.Value?.Dispose();
}
}
}

0 comments on commit 8a7eb2e

Please sign in to comment.