-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPreviewWindow.xaml.cs
150 lines (132 loc) · 4.58 KB
/
PreviewWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using NAudio.Wave;
using System.IO;
using System.Threading.Tasks;
namespace NoSilence
{
public partial class PreviewWindow : Window
{
private string filePath;
private float[] audioSamples;
private string ffmpegPath;
private int zoomLevel = 1;
private WaveOutEvent outputDevice;
private AudioFileReader audioFileReader;
public PreviewWindow(string filePath, string ffmpegPath)
{
InitializeComponent();
this.filePath = filePath;
this.ffmpegPath = ffmpegPath; // Store the FFmpeg path
LoadWaveform();
}
private async void LoadWaveform()
{
if (!File.Exists(filePath))
{
MessageBox.Show("File does not exist. Please check the path.");
return;
}
try
{
await Task.Run(() => LoadAudioData());
DrawWaveform();
}
catch (Exception ex)
{
MessageBox.Show($"Error loading waveform: {ex.Message}");
}
}
private void LoadAudioData()
{
using (var reader = new AudioFileReader(filePath))
{
var samples = new float[reader.WaveFormat.SampleRate * reader.WaveFormat.Channels];
int read;
var totalSamples = new MemoryStream();
while ((read = reader.Read(samples, 0, samples.Length)) > 0)
{
var byteBuffer = new byte[read * sizeof(float)];
Buffer.BlockCopy(samples, 0, byteBuffer, 0, byteBuffer.Length);
totalSamples.Write(byteBuffer, 0, byteBuffer.Length);
}
audioSamples = new float[totalSamples.Length / sizeof(float)];
totalSamples.Position = 0;
var byteBufferAll = totalSamples.ToArray();
Buffer.BlockCopy(byteBufferAll, 0, audioSamples, 0, byteBufferAll.Length);
}
}
private void DrawWaveform()
{
WaveformCanvas.Children.Clear();
double midY = WaveformCanvas.ActualHeight / 2;
double width = WaveformCanvas.ActualWidth;
int samplesPerPixel = (audioSamples.Length / zoomLevel) / (int)width;
if (samplesPerPixel == 0) samplesPerPixel = 1;
for (int x = 0; x < width; x++)
{
int start = x * samplesPerPixel;
int end = (x + 1) * samplesPerPixel;
if (end >= audioSamples.Length) break;
float min = float.MaxValue;
float max = float.MinValue;
for (int n = start; n < end; n++)
{
var val = audioSamples[n];
if (val < min) min = val;
if (val > max) max = val;
}
var line = new Line
{
X1 = x,
X2 = x,
Y1 = midY - (min * midY),
Y2 = midY - (max * midY),
Stroke = Brushes.LightBlue
};
WaveformCanvas.Children.Add(line);
}
}
private void Play_Click(object sender, RoutedEventArgs e)
{
if (outputDevice == null)
{
outputDevice = new WaveOutEvent();
audioFileReader = new AudioFileReader(filePath);
outputDevice.Init(audioFileReader);
}
outputDevice.Play();
}
private void Pause_Click(object sender, RoutedEventArgs e)
{
outputDevice?.Pause();
}
private void Stop_Click(object sender, RoutedEventArgs e)
{
outputDevice?.Stop();
if (audioFileReader != null)
{
audioFileReader.Position = 0; // Reset position
}
}
private void ZoomIn_Click(object sender, RoutedEventArgs e)
{
zoomLevel = Math.Min(zoomLevel + 1, 10);
DrawWaveform();
}
private void ZoomOut_Click(object sender, RoutedEventArgs e)
{
zoomLevel = Math.Max(zoomLevel - 1, 1);
DrawWaveform();
}
protected override void OnClosed(EventArgs e)
{
outputDevice?.Dispose();
audioFileReader?.Dispose();
base.OnClosed(e);
}
}
}