forked from dzamkov/MD-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMP3AudioFeed.cs
107 lines (95 loc) · 2.89 KB
/
MP3AudioFeed.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
using System;
using System.Collections.Generic;
using System.IO;
using OpenTK.Audio.OpenAL;
using Mp3Sharp;
namespace MD
{
/// <summary>
/// An audio feed from a mp3 file.
/// </summary>
public class MP3AudioFeed : AudioFeed
{
public MP3AudioFeed(string File)
{
this._Stream = new Mp3Stream(File);
this._Init();
}
public MP3AudioFeed(Stream Source)
{
this._Stream = new Mp3Stream(Source);
this._Init();
}
private void _Init()
{
// Read some samples to force initialization
byte[] samp = new byte[100];
this._Stream.Read(samp, 0, samp.Length);
this._Stream.Seek(0, SeekOrigin.Begin);
}
/// <summary>
/// Brings the feed back to its begining.
/// </summary>
public void Reset()
{
this._Stream.Seek(0, SeekOrigin.Begin);
}
/// <summary>
/// Copies this mp3 stream to an audio source with the specified chunk size.
/// </summary>
public MemoryAudioSource Copy(int ChunkSize, int MaxSize)
{
this.Reset();
List<byte[]> chunks = new List<byte[]>();
int len = 0;
int bps = this.BytesPerSample;
while (this._Stream.Position < this._Stream.Length && len < MaxSize)
{
byte[] chunk = new byte[ChunkSize * bps];
len += this._Stream.Read(chunk, 0, ChunkSize * bps) / bps;
chunks.Add(chunk);
}
return new MemoryAudioSource(this.SampleRate, this.Format, chunks, ChunkSize, len > MaxSize ? MaxSize : len);
}
/// <summary>
/// Copies all mp3 data to an audio source.
/// </summary>
public MemoryAudioSource Copy(int ChunkSize)
{
return this.Copy(ChunkSize, int.MaxValue);
}
public override int Read(int Amount, byte[] Output, int Offset)
{
int bps = this.BytesPerSample;
if (this._Stream.Position < this._Stream.Length)
{
int amount = 0;
amount = this._Stream.Read(Output, Offset, Amount * bps);
return amount / bps;
}
else
{
for (int t = 0; t < Amount * bps; t++)
{
Output[Offset + t] = 0;
}
return Amount;
}
}
public override int SampleRate
{
get
{
return this._Stream.Frequency;
}
}
public override ALFormat Format
{
get
{
return this._Stream.Format == SoundFormat.Pcm16BitMono ? ALFormat.Mono16 : ALFormat.Stereo16;
}
}
private Mp3Stream _Stream;
}
}