Send audio c#

koznov123

New member
I try send audio frame in c#. But i can't convert byte array to // The audio data, float[] public IntPtr p_data;


For example.

The first I init audio frame.

Code:
 NDI.NDIlib_audio_frame_t audioFrame = new NDIlib_audio_frame_t()
            {
                sample_rate= 48000,
                no_channels = 2,
                no_samples = 1920,
                timecode = 0,
                p_data = bufferPtrA,
                channel_stride_in_bytes = 7680

            };

Than I read my wave file from disk.(Format: pcm16 )

Code:
byte[] data = File.ReadAllBytes(@"C:\temp\mono.wav");

And convert this to float.

In the end i send this frame.
Code:
NDI.Send.NDIlib_send_send_audio(sendInstancePtr, ref audioFrame);




I try get pcm data and doesn't changes.

Code:
  Double[] data;
            byte[] wave;
            byte[] sR = new byte[4];
            System.IO.FileStream WaveFile = System.IO.File.OpenRead(wavePath);
            wave = new byte[WaveFile.Length];
            data = new Double[(wave.Length - 44) / 4];//shifting the headers out of the PCM data;
            WaveFile.Read(wave, 0, Convert.ToInt32(WaveFile.Length));//read the wave file into the wave variable
                                                                     /***********Converting and PCM accounting***************/
            for (int i = 0; i < data.Length; i += 2)
            {
                data[i] = BitConverter.ToInt16(wave, i) / 32768.0;
            }


            /**************assigning sample rate**********************/
            for (int i = 24; i < 28; i++)
            {
                sR[i - 24] = wave[i];
            }
            SampleRate = BitConverter.ToInt16(sR, 0);
            return data;


Please help. how can I convert bytes array to p_data(float)?
 

koznov123

New member
Maybe this way?


Code:
static double bytesToDouble(byte firstByte, byte secondByte)
        {
            // convert two bytes to one short (little endian)
            short s = (short) ((secondByte << 8) | firstByte);
            // convert to range from -1 to (just below) 1
            return s / 32768.0;
        }
Code:
 public static void openWav(string filename, out double[] left, out double[] right)
        {
            byte[] wav = File.ReadAllBytes(filename);

            // Determine if mono or stereo
            int channels = wav[22];     // Forget byte 23 as 99.999% of WAVs are 1 or 2 channels

            // Get past all the other sub chunks to get to the data subchunk:
            int pos = 12;   // First Subchunk ID from 12 to 16

            // Keep iterating until we find the data chunk (i.e. 64 61 74 61 ...... (i.e. 100 97 116 97 in decimal))
            while (!(wav[pos] == 100 && wav[pos + 1] == 97 && wav[pos + 2] == 116 && wav[pos + 3] == 97))
            {
                pos += 4;
                int chunkSize = wav[pos] + wav[pos + 1] * 256 + wav[pos + 2] * 65536 + wav[pos + 3] * 16777216;
                pos += 4 + chunkSize;
            }
            pos += 8;

            // Pos is now positioned to start of actual sound data.
            int samples = (wav.Length - pos) / 2;     // 2 bytes per sample (16 bit sound mono)
            if (channels == 2) samples /= 2;        // 4 bytes per sample (16 bit stereo)

            // Allocate memory (right will be null if only mono sound)
            left = new double[samples];
            if (channels == 2) right = new double[samples];
            else right = null;

            // Write to double array/s:
            int i = 0;
            while (pos < left.Length)
            {
                left[i] = bytesToDouble(wav[pos], wav[pos + 1]);
                pos += 2;
                if (channels == 2)
                {
                    right[i] = bytesToDouble(wav[pos], wav[pos + 1]);
                    pos += 2;
                }
                i++;
            }
        }



If I use this code. silent. When float convert to Intptr = 0.
Code:
audioFrame.p_data = (IntPtr)f;
 
Last edited:
Top Bottom