-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Play Sound instruction and trigger
- Loading branch information
Showing
13 changed files
with
679 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#region "copyright" | ||
|
||
/* | ||
Copyright Dale Ghent <[email protected]> and contributors | ||
This Source Code Form is subject to the terms of the Mozilla Public | ||
License, v. 2.0. If a copy of the MPL was not distributed with this | ||
file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
*/ | ||
|
||
#endregion "copyright" | ||
|
||
using NetCoreAudio; | ||
using Newtonsoft.Json; | ||
using NINA.Core.Model; | ||
using NINA.Core.Utility; | ||
using NINA.Sequencer.SequenceItem; | ||
using NINA.Sequencer.Validations; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.Composition; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Windows.Input; | ||
|
||
namespace DaleGhent.NINA.GroundStation.PlaySound { | ||
|
||
[ExportMetadata("Name", "Play Sound")] | ||
[ExportMetadata("Description", "Plays the specified audio file")] | ||
[ExportMetadata("Icon", "PlaySoundSVG")] | ||
[ExportMetadata("Category", "Ground Station")] | ||
[Export(typeof(ISequenceItem))] | ||
[JsonObject(MemberSerialization.OptIn)] | ||
public class PlaySound : SequenceItem, IValidatable { | ||
private string soundFile = string.Empty; | ||
private bool waitUntilFinished = true; | ||
|
||
[ImportingConstructor] | ||
public PlaySound() { | ||
SoundFile = Properties.Settings.Default.PlaySoundDefaultFile; | ||
SelectSoundFileCommand = new RelayCommand(OpenSelectSoundFileDialog); | ||
|
||
Validate(); | ||
} | ||
|
||
public PlaySound(PlaySound copyMe) : this() { | ||
CopyMetaData(copyMe); | ||
} | ||
|
||
public override object Clone() { | ||
return new PlaySound(this) { | ||
SoundFile = SoundFile, | ||
WaitUntilFinished = WaitUntilFinished, | ||
}; | ||
} | ||
|
||
[JsonProperty] | ||
public string SoundFile { | ||
get => soundFile; | ||
set { | ||
soundFile = value; | ||
Validate(); | ||
RaisePropertyChanged(); | ||
RaisePropertyChanged(nameof(SoundFileShort)); | ||
} | ||
} | ||
|
||
[JsonProperty] | ||
public bool WaitUntilFinished { | ||
get => waitUntilFinished; | ||
set { | ||
waitUntilFinished = value; | ||
RaisePropertyChanged(); | ||
} | ||
} | ||
|
||
public string SoundFileShort => Path.GetFileName(soundFile); | ||
|
||
public override async Task Execute(IProgress<ApplicationStatus> progress, CancellationToken ct) { | ||
var player = new Player(); | ||
|
||
if (waitUntilFinished) { | ||
await player.Play(soundFile); | ||
|
||
do { | ||
await Task.Delay(250, ct); | ||
} while (player.Playing); | ||
} else { | ||
await player.Play(soundFile); | ||
} | ||
|
||
return; | ||
} | ||
|
||
public IList<string> Issues { get; set; } = new List<string>(); | ||
|
||
public bool Validate() { | ||
var i = new List<string>(); | ||
|
||
if (string.IsNullOrEmpty(soundFile) || string.IsNullOrWhiteSpace(soundFile)) { | ||
i.Add("Sound file has not been specified"); | ||
} else { | ||
if (!File.Exists(soundFile)) { | ||
i.Add("Sound file does not exist"); | ||
} | ||
} | ||
|
||
if (i != Issues) { | ||
Issues = i; | ||
RaisePropertyChanged(nameof(Issues)); | ||
} | ||
|
||
return i.Count == 0; | ||
} | ||
|
||
public override string ToString() { | ||
return $"Category: {Category}, Item: {nameof(PlaySound)}, SoundFile: {soundFile}, WaitUntilFinished: {WaitUntilFinished}"; | ||
} | ||
|
||
internal void OpenSelectSoundFileDialog(object obj) { | ||
Microsoft.Win32.OpenFileDialog dialog = new() { | ||
FileName = string.Empty, | ||
Filter = PlaySoundCommon.FileTypeFilter, | ||
}; | ||
|
||
if (dialog.ShowDialog() == true) { | ||
SoundFile = dialog.FileName; | ||
} | ||
} | ||
|
||
public ICommand SelectSoundFileCommand { get; private set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#region "copyright" | ||
|
||
/* | ||
Copyright Dale Ghent <[email protected]> | ||
This Source Code Form is subject to the terms of the Mozilla Public | ||
License, v. 2.0. If a copy of the MPL was not distributed with this | ||
file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
*/ | ||
|
||
#endregion "copyright" | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace DaleGhent.NINA.GroundStation.PlaySound | ||
{ | ||
public class PlaySoundCommon { | ||
|
||
public static string FileTypeFilter = "Audio files|*.wav;*.aiff;*.aif;*.mp3|All files|*.*"; | ||
} | ||
} |
Oops, something went wrong.