PDA

View Full Version : C# Audio play



koznov123
08-24-2016, 12:14 AM
I use example "Managed_NDI_Receive_Example" from Ndi SDK. I have a few question. but one question is very important.

How play sound when I receive it?


I try convert to byte array and play. But it doesn't work.



// audio is beyond the scope of this example
case NDI.NDIlib_frame_type_e.NDIlib_frame_type_audio:

int stride1 = (int)audioFrame.channel_stride_in_bytes;
int channels1 = (int)audioFrame.no_channels;
int bufferSize1 = channels1 * stride1;


Byte[] buffer1 = new Byte[bufferSize1];
Marshal.Copy(audioFrame.p_data, buffer1, 0, bufferSize1);

double[] values = new double[bytes.Length / 8];
Buffer.BlockCopy(bytes, 0, values, 0, values.Length * 8);

Play(values, yres1, 1000, 10,16383);

// free frames that were received
NDI.Receive.NDIlib_recv_free_audio(_recvInstancePt r, ref audioFrame);
break;








public static void Play(double[] bytes, int samplesNo, UInt16 frequency, int msDuration, UInt16 volume = 16383)
{
var mStrm = new MemoryStream();
BinaryWriter writer = new BinaryWriter(mStrm);

const double TAU = 2 * Math.PI;
int formatChunkSize = 16;
int headerSize = 8;
short formatType = 1;
short tracks = 1;
int samplesPerSecond = 48000;
short bitsPerSample = 16;
short frameSize = (short)(tracks * ((bitsPerSample + 7) / 8));
int bytesPerSecond = samplesPerSecond * frameSize;
int waveSize = 4;
int samples = (int)((decimal)samplesPerSecond * msDuration / 1000);
int dataChunkSize = samples * frameSize;
int fileSize = waveSize + headerSize + formatChunkSize + headerSize + dataChunkSize;
// var encoding = new System.Text.UTF8Encoding();
writer.Write(0x46464952); // = encoding.GetBytes("RIFF")
writer.Write(fileSize);
writer.Write(0x45564157); // = encoding.GetBytes("WAVE")
writer.Write(0x20746D66); // = encoding.GetBytes("fmt ")
writer.Write(formatChunkSize);
writer.Write(formatType);
writer.Write(tracks);
writer.Write(samplesPerSecond);
writer.Write(bytesPerSecond);
writer.Write(frameSize);
writer.Write(bitsPerSample);
writer.Write(0x61746164); // = encoding.GetBytes("data")
writer.Write(dataChunkSize);
{
double theta = frequency * TAU / (double)samplesPerSecond;
// 'volume' is UInt16 with range 0 thru Uint16.MaxValue ( = 65 535)
// we need 'amp' to have the range of 0 thru Int16.MaxValue ( = 32 767)
double amp = volume >> 2; // so we simply set amp = volume / 2
for (int step = 0; step < samplesNo; step++)
{
//short s = (short)(amp * Math.Sin(theta * (double)step));
short s = (short)bytes[step];
writer.Write(s);
}
}

mStrm.Seek(0, SeekOrigin.Begin);
new System.Media.SoundPlayer(mStrm).Play();
writer.Close();
mStrm.Close();
} // public static void Play(UInt16 frequency, int msDuration, UInt16 volume = 16383)





But it doen't work. I hear only snap or clack(some noise). How to play sound?

John Perkins
01-24-2017, 04:50 PM
I just added audio to the receive example, so the next SDK update will show you how to handle it.

The lack of a standard .Net audio API made managed examples difficult, so we are now including the NAudio library with the SDK.

Please note that we will not be supporting NAudio itself. For NAudio questions, please go through their normal channels.

You can use any audio library you wish, but the license and ability to simply drop it into the folder made NAudio convenient for us to include.

Also notice that there are quite a few changes to the audio interop classes in the new version.