110,538
社区成员
发帖
与我相关
我的任务
分享
public void Process(byte[] buffer, int offset, int count)
{
BinaryReader From = new BinaryReader(new MemoryStream(buffer, offset, count));
BinaryWriter To = new BinaryWriter(new MemoryStream(buffer, offset, count));
int Samples = count / m_Format.nBlockAlign;
lock (this)
{
switch (m_Format.wBitsPerSample)
{
case 8:
if (m_Format.nChannels == 1) // mono
{
for (int i = 0; i < Samples; i++)
{
float x = (From.ReadByte() - 128.0f) / 128.0f;
float y = ProcessMono(x);
To.Write(ToByte(y));
}
}
else // stereo
{
for (int i = 0; i < Samples; i++)
{
float xyl = (From.ReadByte() - 128.0f) / 128.0f;
float xyr = (From.ReadByte() - 128.0f) / 128.0f;
ProcessStereo(ref xyl, ref xyr);
To.Write(ToByte(xyl));
To.Write(ToByte(xyr));
}
}
break;
case 16:
if (m_Format.nChannels == 1) // mono
{
for (int i = 0; i < Samples; i++)
{
float x = From.ReadInt16() / 32768.0f;
float y = ProcessMono(x);
To.Write(ToShort(y));
}
}
else // stereo
{
for (int i = 0; i < Samples; i++)
{
float xyl = From.ReadInt16() / 32768.0f;
float xyr = From.ReadInt16() / 32768.0f;
ProcessStereo(ref xyl, ref xyr);
To.Write(ToShort(xyl));
To.Write(ToShort(xyr));
}
}
break;
}
}
}
private byte ToByte(float x)
{
x = x * 128.0f + 128.0f;
return x > byte.MaxValue ? byte.MaxValue : x < byte.MinValue ? byte.MinValue : (byte)x;
}
private short ToShort(float x)
{
x = x * 32768.0f;
return x > short.MaxValue ? short.MaxValue : x < short.MinValue ? short.MinValue : (short)x;
}
private float m_Volume = 1.0f;
public float Volume
{
get { return m_Volume; }
set
{
lock (this)
m_Volume = value;
}
}
public bool Connect(WaveFormat format)
{
return true;
}
protected float ProcessMono(float x)
{
return x * m_Volume;
}
protected void ProcessStereo(ref float left, ref float right)
{
left = left * m_Volume;
right = right * m_Volume;
}
private void Filler(IntPtr data, int size)
{
byte[] b = new byte[size];
if (m_AudioStream!= null)
{
int pos = 0;
while (pos < size)
{
int toget = size - pos;
int got = m_AudioStream.Read(b, pos, toget);
if (got < toget)
// m_AudioStream.Position = 0; // loop if the file ends
{
playState = PlayState.Stopped;
return;
}
pos += got;
}
Process(b, 0, b.Length);
}
else
{
for (int i = 0; i < b.Length; i++)
b[i] = 0;
}
System.Runtime.InteropServices.Marshal.Copy(b, 0, data, size);
}