Skip to content

Commit

Permalink
add EnableSoundMultiPlay
Browse files Browse the repository at this point in the history
  • Loading branch information
MikiraSora committed Oct 26, 2024
1 parent 2973e00 commit 46e0b6c
Show file tree
Hide file tree
Showing 14 changed files with 248 additions and 135 deletions.
3 changes: 3 additions & 0 deletions OngekiFumenEditor/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
<setting name="SoundVolume" serializeAs="String">
<value>1</value>
</setting>
<setting name="EnableSoundMultiPlay" serializeAs="String">
<value>True</value>
</setting>
</OngekiFumenEditor.Properties.AudioSetting>
<OngekiFumenEditor.Properties.OptionGeneratorToolsSetting>
<setting name="LastLoadedGameFolder" serializeAs="String">
Expand Down
4 changes: 4 additions & 0 deletions OngekiFumenEditor/AppBootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ public async void OnStartupForGUI(object sender, StartupEventArgs e)
{
IsGUIMode = true;

#if DEBUG
ConsoleWindowHelper.SetConsoleWindowVisible(true);
#endif

InitExceptionCatcher();
LogBaseInfos();
InitIPCServer();
Expand Down
61 changes: 53 additions & 8 deletions OngekiFumenEditor/Kernel/Audio/NAudioImpl/NAudioManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Caliburn.Micro;
using ControlzEx.Standard;
using NAudio.CoreAudioApi;
using NAudio.Wave;
using NAudio.Wave.SampleProviders;
Expand All @@ -21,13 +22,17 @@ namespace OngekiFumenEditor.Kernel.Audio.NAudioImpl
public class NAudioManager : PropertyChangedBase, IAudioManager
{
private HashSet<WeakReference<IAudioPlayer>> ownAudioPlayerRefs = new();
private bool enableSoundMultiPlay;
private int targetSampleRate;
private readonly IWavePlayer audioOutputDevice;

private readonly MixingSampleProvider audioMixer;
private readonly MixingSampleProvider soundMixer;
private readonly MixingSampleProvider musicMixer;

private readonly Dictionary<CachedSound, ISampleProvider> cs2providerMap = new();
private readonly Dictionary<ISampleProvider, CachedSound> provider2csMap = new();

private readonly VolumeSampleProvider soundVolumeWrapper;
private readonly VolumeSampleProvider musicVolumeWrapper;

Expand Down Expand Up @@ -65,10 +70,13 @@ public float MusicVolume

public NAudioManager()
{
var audioOutputType = (AudioOutputType)Properties.AudioSetting.Default.AudioOutputType;
targetSampleRate = Properties.AudioSetting.Default.AudioSampleRate;
var audioOutputType = (AudioOutputType)AudioSetting.Default.AudioOutputType;
enableSoundMultiPlay = AudioSetting.Default.EnableSoundMultiPlay;
targetSampleRate = AudioSetting.Default.AudioSampleRate;

Log.LogDebug($"targetSampleRate: {targetSampleRate}");
Log.LogDebug($"audioOutputType: {audioOutputType}");
Log.LogDebug($"enableSoundMultiPlay: {enableSoundMultiPlay}");

try
{
Expand Down Expand Up @@ -96,6 +104,7 @@ public NAudioManager()
//setup sound
soundMixer = new MixingSampleProvider(format);
soundMixer.ReadFully = true;
soundMixer.MixerInputEnded += SoundMixer_MixerInputEnded;
soundVolumeWrapper = new VolumeSampleProvider(soundMixer);
audioMixer.AddMixerInput(soundVolumeWrapper);
SoundVolume = AudioSetting.Default.SoundVolume;
Expand All @@ -110,8 +119,20 @@ public NAudioManager()
Log.LogInfo($"Audio implement will use {GetType()}");
}

private void SoundMixer_MixerInputEnded(object sender, SampleProviderEventArgs e)
{
RemoveSoundMixerInput(e.SampleProvider, false);
}

public void PlaySound(CachedSound sound, float volume, TimeSpan init)
{
if (!enableSoundMultiPlay)
{
//stop previous
if (cs2providerMap.TryGetValue(sound, out var prevProvider))
RemoveSoundMixerInput(prevProvider, true);
}

ISampleProvider provider = new VolumeSampleProvider(new CachedSoundSampleProvider(sound))
{
Volume = volume
Expand All @@ -124,17 +145,36 @@ public void PlaySound(CachedSound sound, float volume, TimeSpan init)
};
}

AddSoundMixerInput(provider);
AddSoundMixerInput(provider, sound);
}

public void AddSoundMixerInput(ISampleProvider input)
public void AddSoundMixerInput(ISampleProvider input, CachedSound cachedSound)
{
if (!enableSoundMultiPlay)
{
cs2providerMap[cachedSound] = input;
provider2csMap[input] = cachedSound;
}

soundMixer.AddMixerInput(input);
}

public void RemoveSoundMixerInput(ISampleProvider input)
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="mixerRemove">mixer是否需要调用RemoveMixerInput()</param>
public void RemoveSoundMixerInput(ISampleProvider input, bool mixerRemove)
{
soundMixer.RemoveMixerInput(input);
if (mixerRemove)
soundMixer.RemoveMixerInput(input);

if (!enableSoundMultiPlay)
{
if (provider2csMap.TryGetValue(input, out var cachedSound))
cs2providerMap.Remove(cachedSound);
provider2csMap.Remove(input);
}
}

public async Task<IAudioPlayer> LoadAudioAsync(string filePath)
Expand Down Expand Up @@ -179,6 +219,11 @@ public void Dispose()

public ILoopHandle PlayLoopSound(CachedSound sound, float volume, TimeSpan init)
{
if (!enableSoundMultiPlay)
{

}

ISampleProvider provider = new LoopableProvider(new CachedSoundSampleProvider(sound));

if (init.TotalMilliseconds != 0)
Expand All @@ -193,7 +238,7 @@ public ILoopHandle PlayLoopSound(CachedSound sound, float volume, TimeSpan init)
handle.Volume = volume;

//add to mixer
AddSoundMixerInput(handle.Provider);
AddSoundMixerInput(handle.Provider, sound);

//Log.LogDebug($"handle hashcode = {handle.GetHashCode()}");
return handle;
Expand All @@ -205,7 +250,7 @@ public void StopLoopSound(ILoopHandle h)
return;

//Log.LogDebug($"handle hashcode = {handle.GetHashCode()}");
RemoveSoundMixerInput(handle.Provider);
RemoveSoundMixerInput(handle.Provider, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ public class CachedSound
public WaveFormat WaveFormat { get; init; }
public TimeSpan Duration { get; init; }

private CachedSound()
{
//no way
}

public CachedSound(ISampleProvider copySourceProvider)
{
AudioData = copySourceProvider.ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ internal class DefaultCommandExecutor : ICommandExecutor
private readonly RootCommand rootCommand;

public DefaultCommandExecutor()
{
{
Log.Instance.RemoveOutput<ConsoleLogOutput>();

rootCommand = new RootCommand("CommandLine for OngekiFumenEditor");
rootCommand.AddCommand(GenerateVerbCommands<GenerateOption>("svg", Resources.ProgramCommandDescriptionSvg, ProcessSvgCommand));
rootCommand.AddCommand(GenerateVerbCommands<FumenConvertOption>("convert", Resources.ProgramCommandConvert, ProcessConvertCommand));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,32 @@
</StackPanel>
</GroupBox>
<GroupBox Margin="5" Header="{markup:Translate [Sound]}">
<Grid Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock VerticalAlignment="Center" Text="{markup:Translate [SoundFolderPath]}" />
<TextBox
Grid.Column="1"
Margin="10,0,10,0"
VerticalAlignment="Center"
Text="{Binding Setting.SoundFolderPath}">
</TextBox>
<Button
Grid.Column="2"
Padding="20,2,20,2"
cal:Message.Attach="OnSoundFolderPathButtonClick()"
Content="{markup:Translate [Browser]}" />
</Grid>
<StackPanel Margin="10">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock VerticalAlignment="Center" Text="{markup:Translate [SoundFolderPath]}" />
<TextBox
Grid.Column="1"
Margin="10,0,10,0"
VerticalAlignment="Center"
Text="{Binding Setting.SoundFolderPath}">
</TextBox>
<Button
Grid.Column="2"
Padding="20,2,20,2"
cal:Message.Attach="OnSoundFolderPathButtonClick()"
Content="{markup:Translate [Browser]}" />
</Grid>

<CheckBox
Margin="0,10,0,0"
Content="Allow one sound to be played multiple times at the same time. (require restart program)"
IsChecked="{Binding Setting.EnableSoundMultiPlay}" />
</StackPanel>
</GroupBox>
<GroupBox Margin="5" Header="{markup:Translate [Visualizer]}">
<StackPanel Margin="10">
Expand Down
Loading

0 comments on commit 46e0b6c

Please sign in to comment.