-
Notifications
You must be signed in to change notification settings - Fork 4
/
WAVfile.cs
executable file
·81 lines (69 loc) · 1.97 KB
/
WAVfile.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace MarkAble2
{
public class WAVfile
{
private string myFileName = "";
private long myLength = 0;
public WAVfile(string filename)
{
myFileName = filename;
if (File.Exists(myFileName))
{
try
{
var fi = new FileInfo(myFileName);
myLength = fi.Length;
}
catch (Exception)
{
myLength = 0;
}
}
}
public string FileName
{
get { return myFileName; }
set { myFileName = value; }
}
public long Length
{
get
{
return myLength;
}
set { myLength = value; }
}
[DllImport("winmm.dll")]
private static extern uint mciSendString(
string command,
StringBuilder returnValue,
int returnLength,
IntPtr winHandle);
public double DurationInSecs()
{
if (!File.Exists(myFileName))
{
return 0;
}
try
{
StringBuilder lengthBuf = new StringBuilder(32);
mciSendString(string.Format("open \"{0}\" type waveaudio alias wave", myFileName), null, 0, IntPtr.Zero);
mciSendString("status wave length", lengthBuf, lengthBuf.Capacity, IntPtr.Zero);
mciSendString("close wave", null, 0, IntPtr.Zero);
int length = 0;
int.TryParse(lengthBuf.ToString(), out length); //this returns milliseconds
return (double)length / 1000; //makes it seconds
}
catch (Exception)
{
return 0;
}
}
}
}