using NAudio.Wave; using NLayer.NAudioSupport; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Input; using System.Windows.Threading; using static QuikDawEditor.EDITING.StaticProperties; using static QuikDawEditor.EDITING.MiscMethods; using QuikDawEditor.MiscClasses; namespace QuikDawEditor; public partial class MixdownsWindow : Window { public string RelRes() { ReleaseResources(); return "Resources released"; } DispatcherTimer mixdownPlayTimer = new DispatcherTimer(); public EditingControl mainEditor; public ObservableCollection MixdownItems { get; set; } = new ObservableCollection(); public MixdownsWindow() { InitializeComponent(); GetMixdownFilesList(); mixdownPlayTimer.Interval = new TimeSpan(0, 0, 1); mixdownPlayTimer.Tick += DTimer_Tick; } private void DTimer_Tick(object sender, EventArgs e) { MixdownFileItem selMDI = MixdownItems.FirstOrDefault(mdi => mdi.IsSelected); if (selMDI != null) selMDI.Position = mixdownPlayer.Position; } public void GetMixdownFilesList() { List mixdownFiles = new List(); MixdownItems.Clear(); CoverDP.Visibility = Visibility.Visible; coverMessageText = "Getting mixdown files . . ."; Task.Run(() => { foreach (string f in Directory.GetFiles(ProjectMixdownDirectory)) { if (f.EndsWith(".mp3")) { var builder = new Mp3FileReaderBase.FrameDecompressorBuilder(wf => new Mp3FrameDecompressor(wf)); Mp3FileReaderBase mp3frb = new Mp3FileReaderBase(f, builder); MixdownFileItem newmdi = new MixdownFileItem() { fileName = Path.GetFileName(f), msecLength = Convert.ToDouble(mp3frb.TotalTime.TotalMilliseconds) }; mp3frb.Close(); mixdownFiles.Add(newmdi); } } //MessageBox.Show("mixdownfiecount=" + mixdownFiles.Count.ToString()); Dispatcher.InvokeAsync(() => { foreach (MixdownFileItem md in mixdownFiles) MixdownItems.Add(md); CoverDP.Visibility = Visibility.Hidden; mixdownPlayTimer.Start(); }); }).Wait(); } bool isPlaying = false; private void PlayButton_Click(object sender, RoutedEventArgs e) { MixdownFileItem thisMDI = (MixdownFileItem)((Button)sender).DataContext; Button thisBut = (Button)sender; if (isPlaying) { mixdownPlayer.Pause(); thisMDI.Position = mixdownPlayer.Position; thisMDI.State = "Paused"; } else { mixdownPlayTimer.Start(); mixdownPlayer.Play(); mixdownPlayer.Position = thisMDI.Position; thisMDI.State = "Playing"; } isPlaying = !isPlaying; } private void StopButton_Click(object sender, RoutedEventArgs e) { mixdownPlayer.Stop(); mixdownPlayTimer.Stop(); MixdownFileItem selMDI = MixdownItems.FirstOrDefault(mdi => mdi.IsSelected); if (selMDI != null) { selMDI.Position = TimeSpan.FromMilliseconds(0); selMDI.State = "Stopped"; } isPlaying = false; } private void MixdownFileItem_PreviewMouseDown(object sender, MouseButtonEventArgs e) { mixdownPlayTimer.Stop(); MixdownFileItem thisMDI = (MixdownFileItem)((Grid)sender).DataContext; MixdownFileItem selMDI = MixdownItems.FirstOrDefault(mdi => mdi.IsSelected); if (selMDI != null) { selMDI.Position = mixdownPlayer.Position; selMDI.State = "Paused"; } foreach (MixdownFileItem mdi in MixdownItems) mdi.IsSelected = false; thisMDI.IsSelected = true; mixdownPlayer.Pause(); isPlaying = false; //MessageBox.Show(mixdownDirectory + "/" + thisMDI.fileName); mixdownPlayer.Source = new Uri(ProjectMixdownDirectory + "\\" + thisMDI.fileName); mixdownPlayer.Play(); mixdownPlayer.Position = thisMDI.Position; mixdownPlayer.Pause(); } private void Thumb_DragDelta(object sender, DragDeltaEventArgs e) { Thumb thisThumb = (Thumb)sender; MixdownFileItem thisMDI = (MixdownFileItem)thisThumb.DataContext; double msecPerPix = thisMDI.msecLength / thisMDI.containerPixelWidth; thisMDI.Position = TimeSpan.FromMilliseconds(thisMDI.Position.TotalMilliseconds + e.HorizontalChange * msecPerPix); mixdownPlayer.Position = thisMDI.Position; } private void mixWin_Closing(object sender, System.ComponentModel.CancelEventArgs e) { mixdownPlayTimer.Stop(); ReleasePlayerSource(); } private void ReleasePlayerSource() { mixdownPlayer.Stop(); mixdownPlayer.ClearValue(MediaElement.SourceProperty); mixdownPlayer.Source = null; } private void PositionGrid_SizeChanged(object sender, SizeChangedEventArgs e) { foreach (MixdownFileItem mdi in MixdownItems) mdi.containerPixelWidth = e.NewSize.Width; //MessageBox.Show(e.NewSize.Width.ToString()); } private void RenameFileMenuItem_Click(object sender, RoutedEventArgs e) { ReleasePlayerSource(); MenuItem mitem = (MenuItem)sender; MixdownFileItem mdi = (MixdownFileItem)mitem.DataContext; InputBox inpbox = new InputBox("New mixdown file name", mdi.fileName) { Owner = this } ; //inpbox.InputTB.Text = mdi.fileName; inpbox.InputTB.SelectAll(); inpbox.ShowDialog(); if (inpbox.Result == "Cancel") return; string invalid = new string(System.IO.Path.GetInvalidFileNameChars()); if (Regex.IsMatch(inpbox.TextBoxText, "[" + invalid + "]")) { MessageBox.Show("Invalid characters in new file name"); return; } if (!inpbox.TextBoxText.EndsWith(".mp3")) { MessageBox.Show("Name must end in \".mp3\""); return; } if (inpbox.TextBoxText.Replace(".mp3", "") == "") { MessageBox.Show("Name cannot be empty"); return; } ChangeMixdownFileName(mdi.fileName, inpbox.TextBoxText); } private void DeleteFileMenuItem_Click(object sender, RoutedEventArgs e) { MenuItem mitem = (MenuItem)sender; MixdownFileItem mdi = (MixdownFileItem)mitem.DataContext; if (MessageBox.Show("Delete this mixdown file?:\n" + mdi.fileName, "Delete file", MessageBoxButton.YesNo) == MessageBoxResult.No) return; ReleasePlayerSource(); DeleteMixdownFileName(mdi.fileName); } private void CoverDP_PreviewMouseDown(object sender, MouseButtonEventArgs e) { UrlMessageTB.Text = ""; MessageDP.Visibility = Visibility.Hidden; } private void MasterFileMenuItem_Click(object sender, RoutedEventArgs e) { } private void CreateMixdownBut_Click(object sender, RoutedEventArgs e) { bool mixSelection = (bool)MixdownSelectionCB.IsChecked; string selected = mixSelection ? "SELECTION of " : ""; string title = mixSelection ? "Mixdown selection" : "Mixdown full project"; double selStart = editingProject.LoopRangeLeftMs; double selEnd = editingProject.LoopRangeLeftMs + editingProject.LoopRangeWidthMs ; string rangeString = new TimeSpan(0, 0, 0, 0, (int)selStart).ToString().Substring(0, 8) + " - " + new TimeSpan(0, 0, 0, 0, (int)selEnd).ToString().Substring(0, 8); string selString = mixSelection ? "_(SELECTION " + rangeString.Replace(':', '.') + ")_" : "_"; string promptString = "Mixdown this " + selected + "project to file?"; string mixdownFileName = editingProject.ProjectName + selString + "Mixdown [" + DateTime.UtcNow.ToString("yyyy-MM-dd HH-mm-ss") + "].mp3"; InputBox inputBox = new InputBox(promptString, mixdownFileName) { Owner = this }; inputBox.Title = title; inputBox.ShowDialog(); if (inputBox.Result == "Cancel") return; inputBox.InputTB.Text = inputBox.InputTB.Text.Replace("..", "."); mixdownFileName = inputBox.InputTB.Text.EndsWith(".mp3") ? inputBox.InputTB.Text : inputBox.InputTB.Text + ".mp3"; //MessageBox.Show(mixdownFileName); MixdownProjectToFile(mixSelection, selStart, selEnd, mixdownFileName); } private void mixWin_Activated(object sender, EventArgs e) { MixdownSelectionCB.IsEnabled = editingProject.LoopRangeSelected; MixdownSelectionCB.IsChecked = editingProject.LoopRangeSelected; } }