Skip to content

Commit

Permalink
SerialSoundPlayer works with SonicBoom.
Browse files Browse the repository at this point in the history
Required implenting playback-complete and loop-playback in SonicBoom sound player. But it works.
Temporarily reference SonicBoom from source.
  • Loading branch information
nightblade9 committed Dec 31, 2024
1 parent 7868b8b commit 59cfc1f
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 10 deletions.
1 change: 1 addition & 0 deletions source/TextBlade.Core/IO/SerialSoundPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ private void PlayNext()
return;
}

_soundPlayer.Stop();
_soundPlayer.Load(_audiosToPlay[_currentAudioId]);
_soundPlayer.Play();
}
Expand Down
3 changes: 2 additions & 1 deletion source/TextBlade.Core/Services/ISoundPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

public interface ISoundPlayer : IDisposable
{
public event Action? OnPlaybackComplete;
public Action? OnPlaybackComplete { get; set; }
public bool LoopPlayback { get; set; }
void Load(string audioFile);
void Play();
void Stop();
Expand Down
3 changes: 2 additions & 1 deletion source/TextBlade.Core/Services/NullSoundPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ namespace TextBlade.Core.Services
{
public sealed class NullSoundPlayer : ISoundPlayer
{
public event Action? OnPlaybackComplete;
public Action? OnPlaybackComplete { get; set; }
public bool LoopPlayback { get; set; }

public void Dispose()
{
Expand Down
7 changes: 3 additions & 4 deletions source/TextBlade.Platform.Windows/Audio/NAudioSoundPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ namespace TextBlade.Platform.Windows.Audio;

public class NAudioSoundPlayer : ISoundPlayer
{
public event Action? OnPlaybackComplete;
public Action? OnPlaybackComplete { get; set; }
public bool LoopPlayback { get; set; }

private const string SupportedAudioExtension = "ogg";
private const float VolumeMultiplier = 0.5f;
Expand All @@ -23,7 +24,7 @@ public void Load(string audioFile)

_waveOut.PlaybackStopped += (sender, stoppedArgs) =>
{
if (!ShouldLoopPlayback)
if (!LoopPlayback)
{
OnPlaybackComplete?.Invoke();
return;
Expand All @@ -39,8 +40,6 @@ public void Load(string audioFile)
public void Play() => _waveOut?.Play();
public void Stop() =>_waveOut?.Stop();

private bool ShouldLoopPlayback => OnPlaybackComplete == null;

protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
Expand Down
31 changes: 29 additions & 2 deletions source/TextBlade.Platform.Windows/Audio/SonicBoomSoundPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,47 @@ namespace TextBlade.Platform.Windows.Audio;

public class SonicBoomSoundPlayer : ISoundPlayer
{
public event Action? OnPlaybackComplete;
public Action? OnPlaybackComplete
{
get => _onPlaybackComplete;
set
{
_onPlaybackComplete = value;
if (_audioPlayer != null)
{
_audioPlayer.OnPlaybackComplete += value;
}
}
}

public bool LoopPlayback
{
get => _loopPlayback;
set
{
_loopPlayback = value;
if (_audioPlayer != null)
{
_audioPlayer.LoopPlayback = value;
}
}
}

private const string SupportedAudioExtension = "ogg";
private const float VolumeMultiplier = 0.5f;

private AudioPlayer? _audioPlayer;

Check failure on line 37 in source/TextBlade.Platform.Windows/Audio/SonicBoomSoundPlayer.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'AudioPlayer' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 37 in source/TextBlade.Platform.Windows/Audio/SonicBoomSoundPlayer.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'AudioPlayer' could not be found (are you missing a using directive or an assembly reference?)
private bool _loopPlayback;
private Action? _onPlaybackComplete;
private bool _disposedValue;

public void Load(string audioFile)
{
_audioPlayer = new AudioPlayer();
_audioPlayer.Load($"{audioFile}.{SupportedAudioExtension}");
_audioPlayer.Volume = VolumeMultiplier;
_audioPlayer.LoopPlayback = true;
_audioPlayer.LoopPlayback = LoopPlayback;
_audioPlayer.OnPlaybackComplete += OnPlaybackComplete;
}

public void Play() => _audioPlayer?.Play();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@

<ItemGroup>
<ProjectReference Include="..\TextBlade.Core\TextBlade.Core.csproj" />
<ProjectReference Include="..\..\..\SonicBoom\SonicBoom\SonicBoom.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="NAudio" />
<PackageReference Include="NAudio.Vorbis" />
<PackageReference Include="Spectre.Console" />
</ItemGroup>

Expand Down
5 changes: 5 additions & 0 deletions source/global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"msbuild-sdks": {
"MSBuild.Sdk.Extras": "3.0.44"
}
}

0 comments on commit 59cfc1f

Please sign in to comment.