-
Notifications
You must be signed in to change notification settings - Fork 1
/
StreamPlayer.cs
210 lines (191 loc) · 6.28 KB
/
StreamPlayer.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using NAudio.Wave;
using NAudio.CoreAudioApi;
namespace FastResampler
{
public class StreamPlayer
{
private MemoryStream ms = new MemoryStream();
private BinaryWriter bw;
private WaveStream wavStream;
private long startTime; //单位:0.001s
private Thread TaskThread;
public WaveOut waveOut;
public bool Loaded = false;
public bool FullLoaded = false;
public long Length = 0;
public int FrameLength = 1024 * 100;
private long nowSeek;
private byte[] FillWaveData;
private bool autoStart;
public NAudio.Wave.PlaybackState PlaybackState
{
get
{
return waveOut.PlaybackState;
}
}
public long streamPos
{
get
{
return ms.Position;
}
}
public StreamPlayer()
{
FillWaveData = new byte[FrameLength];
for (int i = 0; i < FrameLength; i++)
{
FillWaveData[i] = 0;
}
bw = new BinaryWriter(ms);
waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback());
}
public long Push(byte[] wavData, int length = -1)
{
if (length == -1)
{
length = wavData.Length;
}
lock (ms)
{
nowSeek = ms.Position;
ms.Seek(Length, SeekOrigin.Begin);
ms.Write(wavData, 0, length);
Length += wavData.Length;
ms.Seek(nowSeek, SeekOrigin.Begin);
}
if (autoStart)
{
this.DoPlay();
}
return Length;
}
public long[] parseRiffHeader(byte[] wavData)
{
long[] ret = new long[2];
string RIFF = System.Text.Encoding.Default.GetString(wavData, 0, 4);
if (RIFF == "RIFF")
{
}
else
{
ret[0] = 0;
ret[1] = 0;
}
return ret;
}
private void Task()
{
if (!FullLoaded && Length - ms.Position < FrameLength)
{
lock (ms)
{
nowSeek = ms.Position;
ms.Seek(Length, SeekOrigin.Begin);
ms.Write(FillWaveData, 0, FrameLength);
ms.Seek(nowSeek, SeekOrigin.Begin);
}
}
}
private void ThreadRun()
{
while (this.PlaybackState == PlaybackState.Playing)
{
this.Task();
Thread.Sleep(100);
}
}
private void DoPlay()
{
if (!Loaded)
{
this.InitWaveStream();
Loaded = true;
}
waveOut.Play();
TaskThread = new Thread(new ThreadStart(this.ThreadRun));
TaskThread.Start();
startTime = Convert.ToInt64(time());
}
public bool Play()
{
if (Length > 1024)
{
this.DoPlay();
return true;
}
else
{
autoStart = true;
return false;
}
}
public void Pause()
{
waveOut.Pause();
}
public void Stop()
{
waveOut.Stop();
}
public void Resume()
{
waveOut.Resume();
}
private double time()
{
TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1);
return ts.TotalMilliseconds;
}
private byte[] modifyRiffHeader(byte[] header, int length, int channels, int fs, int nbit)
{
int samples = length * channels * fs * nbit / 8;
BitConverter.GetBytes(Convert.ToInt32(samples * 2 + 36)).CopyTo(header, 4);
BitConverter.GetBytes(Convert.ToInt32(samples)).CopyTo(header, 40);
return header;
}
private byte[] makeRiffHeader(int samples, int channels, int fs, int nbit)
{
byte[] header = new byte[44];
System.Text.Encoding.Default.GetBytes("RIFF").CopyTo(header, 0);
BitConverter.GetBytes(Convert.ToInt32(samples * 2 + 36)).CopyTo(header, 4);
System.Text.Encoding.Default.GetBytes("WAVE").CopyTo(header, 8);
System.Text.Encoding.Default.GetBytes("fmt ").CopyTo(header, 12);
BitConverter.GetBytes(Convert.ToInt32(16)).CopyTo(header, 16);
BitConverter.GetBytes(Convert.ToInt16(1)).CopyTo(header, 20);
BitConverter.GetBytes(Convert.ToUInt16(channels)).CopyTo(header, 22);
BitConverter.GetBytes(Convert.ToUInt32(fs)).CopyTo(header, 24);
BitConverter.GetBytes(Convert.ToUInt32(fs * nbit / 8)).CopyTo(header, 28);
BitConverter.GetBytes(Convert.ToUInt16(nbit / 8)).CopyTo(header, 32);
BitConverter.GetBytes(Convert.ToUInt16(nbit)).CopyTo(header, 34);
System.Text.Encoding.Default.GetBytes("data").CopyTo(header, 36);
BitConverter.GetBytes(Convert.ToInt32(samples)).CopyTo(header, 40);
return header;
}
private void InitWaveStream()
{
ms.Seek(0, SeekOrigin.Begin);
BinaryReader binaryReader = new BinaryReader(ms);
//WaveFormat format = new WaveFormat(binaryReader);
byte[] tempHeader = new byte[44];
ms.Seek(0, SeekOrigin.Begin);
ms.Read(tempHeader, 0, Convert.ToInt32(44));
//byte[] header = modifyRiffHeader(tempHeader, 3600, format.Channels, format.SampleRate, format.BitsPerSample);
byte[] header = makeRiffHeader(360 * 1 * 44100 * 16 / 8, 1, 44100, 16);
tempHeader = null;
ms.Seek(0, SeekOrigin.Begin);
ms.Write(header, 0, header.Length);
header = null;
ms.Seek(0, SeekOrigin.Begin);
this.wavStream = new WaveFileReader(ms);
waveOut.Init(wavStream);
}
}
}