This project has moved and is read-only. For the latest updates, please go here.

Newbie: Kinect service ==>Byte array to speaker

Oct 16, 2012 at 10:06 AM

Hi,

I am a newbie and  so my knoweldge on sur the language (english and C#) are not perferct, that why i need lot of explanation.


Here is the problem:
I used the kinet service with success for transmitte the video between two PC on the network (WPF application with visual studio 2010).

For  audio, the server (pc where is plug the kinect) transmit byte array of sound data over the  network.

On the client side there is a event called "Audio frame ready" and really don't know what to put in there for make it working.

I tried different code found over the discussion in Naudio with no success

Here is the code in the client side:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using NetComm;
using Coding4Fun.Kinect.KinectService.Common;
using Coding4Fun.Kinect.KinectService.WpfClient;
using System.IO;
using NAudio.Wave;
using NAudio.CoreAudioApi;
using System.Threading;
using System.Media;


namespace clientcommres
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    /// 
    public partial class MainWindow : Window
    {

        WaveFormat toto;
        MemoryStream memoryStream;
        
        string ConvertBytesToString(byte[] bytes)
        {
            return ASCIIEncoding.ASCII.GetString(bytes);
        }

        byte[] ConvertStringToBytes(string str)
        {
            return ASCIIEncoding.ASCII.GetBytes(str);
        }

         SoundPlayer myPlayer = new SoundPlayer("myFile.wav");
        FileStream fs = File.Create("myfile.wav");
        
        NetComm.Client client = new NetComm.Client();


        public MainWindow()
        {
            InitializeComponent();


            client.Connected += new Client.ConnectedEventHandler(client_Connected);
            client.Disconnected += new Client.DisconnectedEventHandler(client_Disconnected);
            client.DataReceived += new Client.DataReceivedEventHandler(client_DataReceived);
           
            toto =new WaveFormat(16000,4);
            
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
           

          
           //connect to the host 
            client.Connect("192.168.0.23",2200, "Jack");
            ColorClient colorClient = new ColorClient();
            Coding4Fun.Kinect.KinectService.WpfClient.AudioClient audioClient = new Coding4Fun.Kinect.KinectService.WpfClient.AudioClient();

            colorClient.ColorFrameReady += clientColorFrameReady;
            audioClient.AudioFrameReady += audioClient_AudioFrameReady;
            colorClient.Connect("192.168.0.23", 4530);
            audioClient.Connect("192.168.0.23", 4533);
           
        }

        void audioClient_AudioFrameReady(object sender, AudioFrameReadyEventArgs e)
        {


            memoryStream = new MemoryStream(e.AudioFrame.AudioData);

            //memoryStream.Position = 0;
            RawSourceWaveStream waveStream = new RawSourceWaveStream(memoryStream, toto);
            WaveStream conversionStream = WaveFormatConversionStream.CreatePcmStream(waveStream);
            using (WaveStream blockAlignedStream = new BlockAlignReductionStream(conversionStream))
            {
                using (WaveOut waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
                {
                    WaveChannel32 activestream = new WaveChannel32(blockAlignedStream);
                    waveOut.Init(activestream);
                    waveOut.Play();
                    while (waveOut.PlaybackState == PlaybackState.Playing)
                    {
                        System.Threading.Thread.Sleep(100);
                    }
                }


            }
        }



        void clientColorFrameReady(object sender, ColorFrameReadyEventArgs e)
        {
            // Video is a WPF or Windows Phone Image control
            image1.Source = e.ColorFrame.BitmapImage;
        }

        void client_Connected()
        {
            Log.AppendText("Connected successfully!" +
            Environment.NewLine); //Updates the log with the current connection state
        }

        void client_Disconnected()
        {
            Log.AppendText("Disconnected from host!" +
            Environment.NewLine); //Updates the log with the current connection state
        }

        void client_DataReceived(byte[] Data, string ID)
        {
            Log.AppendText(ID + ": " + ConvertBytesToString(Data) + Environment.NewLine); //Updates the log with the current connection state
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            client.SendData(ConvertStringToBytes(ChatMessage.Text));
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (client.isConnected) client.Disconnect(); //Disconnects if the 
            //client is connected, closing the communication thread
        }
        
       
    }
}

 

 

 

Oct 17, 2012 at 1:40 PM

to play audio recieved over the network with NAudio, you create a BufferedWaveProvider, and add bytes to that as they become available. Then just pass the buffered wave provider into an instance of WaveOutEvent (please avoid using WaveOut with function callbacks)

Oct 25, 2012 at 9:31 AM

Thank you Mark for your reponse it's working with he following code:

WaveOut wo = new WaveOut();
WaveFormat wf = new WaveFormat(16000,1);
BufferedWaveProvider mybufferwp = null;


 audioClient.AudioFrameReady += audioClient_AudioFrameReady;

void audioClient_AudioFrameReady(object sender, AudioFrameReadyEventArgs e)
        {
            
            if (mybufferwp != null  )
            {
                mybufferwp.AddSamples(e.AudioFrame.AudioData, 0, e.AudioFrame.AudioData.Length);
            }
        }

 private void ecoute_Click(object sender, RoutedEventArgs e)
        {
            mybufferwp = new BufferedWaveProvider(wf);
            mybufferwp.BufferDuration = TimeSpan.FromMinutes(5);
            wo.Init(mybufferwp);
            wo.Play();
        }
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (client.isConnected) client.Disconnect(); //Disconnects if the 
            //client is connected, closing the communication thread
            if (wo.PlaybackState == PlaybackState.Playing)
            {
                wo.Stop();
                wo.Dispose();
                wo = null;
              
            }
        }

Oct 25, 2012 at 9:33 AM

great! glad you got it working.