Skip to content

Commit

Permalink
Merge pull request #872 from oxygen-dioxide/playsample
Browse files Browse the repository at this point in the history
Fix openutau crashing when the sample isn't a valid audio file
  • Loading branch information
stakira authored Oct 10, 2023
2 parents e443f17 + 4838d4f commit 6897d0e
Showing 1 changed file with 40 additions and 31 deletions.
71 changes: 40 additions & 31 deletions OpenUtau/Views/SingersDialog.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,43 +230,52 @@ void OnOpenReadme(object sender, RoutedEventArgs e) {
}
}

string? FindSample(USinger singer){
var sample = singer.Sample;
if(sample!=null && File.Exists(sample)){
return sample;
} else if (singer.SingerType == USingerType.Classic) {
var path = singer.Location;
if(!Directory.Exists(path)){
return null;
}
string[] files = Directory.EnumerateFiles(path, "*.wav", SearchOption.AllDirectories)
.Union(Directory.EnumerateFiles(path, "*.mp3", SearchOption.AllDirectories))
.Union(Directory.EnumerateFiles(path, "*.flac", SearchOption.AllDirectories))
.Union(Directory.EnumerateFiles(path, "*.aiff", SearchOption.AllDirectories))
.Union(Directory.EnumerateFiles(path, "*.ogg", SearchOption.AllDirectories))
.Union(Directory.EnumerateFiles(path, "*.opus", SearchOption.AllDirectories))
.ToArray();
if(files.Length==0){
return null;
}
Random rnd = new Random(Guid.NewGuid().GetHashCode());
int choice = rnd.Next(0, files.Length - 1);
string soundFile = files[choice];
return soundFile;
}
return null;
}

public void OnPlaySample(object sender, RoutedEventArgs e) {
var viewModel = (DataContext as SingersViewModel)!;
var playBack = PlaybackManager.Inst.AudioOutput;
var playbackState = playBack.PlaybackState;
if (viewModel.Singer != null) {
var sample = viewModel.Singer.Sample;
if (sample != null && File.Exists(sample)) {
var playSample = Wave.OpenFile(sample);
playBack.Init(playSample.ToSampleProvider());
playBack.Play();
if (playbackState == PlaybackState.Playing) {
playBack.Stop();
}
} else if (viewModel.Singer.SingerType == USingerType.Classic) {
var path = viewModel.Singer.Location;
if(!Directory.Exists(path)){
return;
}
string[] files = Directory.EnumerateFiles(path, "*.wav", SearchOption.AllDirectories)
.Union(Directory.EnumerateFiles(path, "*.mp3", SearchOption.AllDirectories))
.Union(Directory.EnumerateFiles(path, "*.flac", SearchOption.AllDirectories))
.Union(Directory.EnumerateFiles(path, "*.aiff", SearchOption.AllDirectories))
.Union(Directory.EnumerateFiles(path, "*.ogg", SearchOption.AllDirectories))
.Union(Directory.EnumerateFiles(path, "*.opus", SearchOption.AllDirectories))
.ToArray();
if(files.Length==0){
return;
}
Random rnd = new Random(Guid.NewGuid().GetHashCode());
int choice = rnd.Next(0, files.Length - 1);
string soundFile = files[choice];
var playSound = Wave.OpenFile(soundFile);
var sample = FindSample(viewModel.Singer);
if(sample == null){
return;
}
try{
var playSound = Wave.OpenFile(sample);
playBack.Init(playSound.ToSampleProvider());
playBack.Play();
if (playbackState == PlaybackState.Playing) {
playBack.Stop();
}
} catch (Exception ex) {
Log.Error(ex, $"Failed to load sample {sample}.");
return;
}
playBack.Play();
if (playbackState == PlaybackState.Playing) {
playBack.Stop();
}
}
}
Expand Down

0 comments on commit 6897d0e

Please sign in to comment.