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

How to play recived voice stream in remote PC?

Apr 19, 2010 at 11:14 PM

i have use a Naudio.dll to capture vioce from sound card. And i use a NEtworkStream to transfer the captured stream via network.

The recived stream am saves to wav file on remote PC (Client Side). I have only one question....

How i Can play recived stream from a network to default sound output using Naudio.dll(http://naudio.codeplex.com/)in online mode?

am writing in Visual Basic. Net (2008). Help me please to do so.

the Server Source Code:

'Its need to Capture Data
Imports Microsoft.DirectX.DirectSound
Imports NAudio
Imports Microsoft.VisualBasic.Devices
Imports System
Imports System.IO
'End Capture Data
'Its NEED for Transform Stream via network
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports Microsoft.VisualBasic
'END TransFormVIA Network
Public Class SOPROForm
    Public StreamSocket As NetworkStream
    Public acepted As Boolean 'When Accepted
    Public TCPSvr As New TcpListener(3344) 'Listener
    Public TCPClient As TcpClient
    Public LittleFIleName As String = Application.ProductName.ToString 'Little File Name
    Public WithEvents StreamWavein As New NAudio.Wave.WaveIn 'Wave in Stream Generates by Naudio
    Public waveInFormat As New NAudio.Wave.WaveFormat(16000, 1) ' Wave In format
    Public Writer As New Wave.WaveFileWriter(Application.StartupPath & "\Waweout.waveout", waveInFormat) 'Wave Stream Writer
    Public OperatorName As String ' Operator Name
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        TCPSvr.Start()
        NotifyIcon1.Icon = New System.Drawing.Icon(Application.StartupPath & "\LAME\NotRec.ico")
        BackgroundWorker1.RunWorkerAsync()
        NotifyIcon1.Text = "СОПРО Client" & vbCrLf & "Нет Активного Прослушивания"
        NotifyIcon1.Visible = True
        OperatorName = GetSetting("SOPRO", "\SETTINGS\", "OperatorName", 0)
        StreamWavein.DeviceNumber = 0
        StreamWavein.WaveFormat = New NAudio.Wave.WaveFormat(16000, 1)
        StreamWavein.StartRecording()

    End Sub
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            NotifyIcon1.Dispose()
            Dim filename As String = Writer.Filename.ToString
            StreamWavein.BufferMillisconds = 20 * 1000
            Writer.Close()
            LittleFIleName = DateAndTime.DateString & "_" & Replace(DateAndTime.TimeOfDay, ":", "-") & ".tmp"
            Dim Filename2 = Application.StartupPath & "\Lame\" & OperatorName & "_" & LittleFIleName
            File.Copy(filename, Filename2)
            StreamWavein.BufferMillisconds = 1000
            StreamWavein.StopRecording()

            Writer.Close()
            End
        Catch ex As Exception
        End Try
    End Sub
    Private Sub StreamWavein_DataAvailable(ByVal sender As Object, ByVal e As NAudio.Wave.WaveInEventArgs) Handles StreamWavein.DataAvailable


        Me.Visible = False
        If TCPSvr.Pending = True Then
            acepted = True
            TCPClient = TCPSvr.AcceptTcpClient
            NotifyIcon1.Icon = New System.Drawing.Icon(Application.StartupPath & "\LAME\Rec.ico")
            NotifyIcon1.Text = "СОПРО Client" & vbCrLf & "Вас Прослушивают"
            Try
                StreamSocket.Close()
            Catch ex As Exception
            End Try
            StreamSocket = TCPClient.GetStream()
        End If
        If acepted = True Then
            Try
                StreamSocket.Write(e.Buffer, 0, e.BytesRecorded)
            Catch ex As Exception
                NotifyIcon1.Icon = New System.Drawing.Icon(Application.StartupPath & "\LAME\NotRec.ico")
                acepted = False
                NotifyIcon1.Text = "СОПРО Client" & vbCrLf & "Нет Соединения"
            End Try

        End If
        Try

            'write captured data to .wav file
            Writer.WriteData(e.Buffer, 0, e.BytesRecorded)
            
            If Writer.Length > 57630600 Then
                Dim filename As String = Writer.Filename.ToString
                StreamWavein.BufferMillisconds = 20 * 1000
                Writer.Close()
                LittleFIleName = DateAndTime.DateString & "_" & Replace(DateAndTime.TimeOfDay, ":", "-") & ".tmp" ' Generate File Name
                Dim Filename2 = Application.StartupPath & "\Lame\" & OperatorName & "_" & LittleFIleName
                File.Copy(filename, Filename2)
                StreamWavein.BufferMillisconds = 100
                Shell(Application.StartupPath & "\Lame\kill.cmd", AppWinStyle.Hide)
            End If
        Catch ex As Exception


        End Try
    End Sub
    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        'FTP Thread
        'позволяет передовать сгенерированные файлы на сервер
start:

        Try
            Dim Password As String = GetSetting("SOPRO", "\SETTINGS\", "SoproP", 0)
            Dim UserName As String = GetSetting("SOPRO", "\SETTINGS\", "USER", 0)
            Dim serverName As String = GetSetting("SOPRO", "\SETTINGS\", "SERVER", 0)
            Dim Filename As String = Dir(Application.StartupPath & "\Lame\Encoded\", FileAttribute.System)
            If Len(Filename) < 5 Then
                System.Threading.Thread.Sleep(1000)
                GoTo start
            End If
            Dim FilenameTemp() As String = Split(Filename, "_")
            Dim FromOperator As String = FilenameTemp(0)
            Dim DateOfFile As String = FilenameTemp(1)
            Dim TiMeofCreat As String = FilenameTemp(2)
            Dim readFile As String = Application.StartupPath & "\Lame\Encoded\" & Filename
            Dim ParceDate() As String = Split(DateOfFile, "-")
            Dim FilenameLast As String = serverName & OperatorName & "/" & ParceDate(2) & "/" & "Month_" & ParceDate(0) & "/" & ParceDate(1) & "/" & Filename
            Dim clsRequest As System.Net.FtpWebRequest = _
                DirectCast(System.Net.WebRequest.Create(FilenameLast.ToString), System.Net.FtpWebRequest)
            clsRequest.KeepAlive = False
            clsRequest.Credentials = New System.Net.NetworkCredential(UserName.ToString, Password.ToString)
            clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
            Dim bFile() As Byte = System.IO.File.ReadAllBytes(Application.StartupPath & "\Lame\Encoded\" & Filename)
            Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream()
            clsStream.Write(bFile, 0, bFile.Length)
            clsStream.Close()
            clsStream.Dispose()
            File.Delete(Application.StartupPath & "\Lame\Encoded\" & Filename.ToString)
            System.Threading.Thread.Sleep(1000)
            GoTo start
        Catch ex As Exception
            System.Threading.Thread.Sleep(1000)
            GoTo start
        End Try
    End Sub
End Class

 

Client Source Code:

'Naudio
Imports Microsoft.DirectX.DirectSound
Imports NAudio
Imports Microsoft.VisualBasic.Devices
Imports System
Imports System.IO
'Listener
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports Microsoft.VisualBasic
Public Class Form1
    Public stoped As Boolean
    Public waveInFormat As New NAudio.Wave.WaveFormat(16000, 1) ' Wave In format
    Public Writer As New Wave.WaveFileWriter(Application.StartupPath & "\Waweout.waveout", waveInFormat) 'Wave Stream Writer
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        stoped = True
        Writer.Close()
       
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBox1.Items.Add("adak")
       
    End Sub
    Private Sub ListBox1_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Button1.Enabled = True
    End Sub
    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Select Case Button1.Text
            Case "Прослушать"
                Label1.Focus()
                Label1.Visible = True
                Dim tempwaveSream As New Wave.WaveFileWriter(Application.StartupPath & "\movies\" & ListBox1.SelectedItem.ToString & "_" & Replace(Date.Now.ToString, ":", "_") & ".wav", waveInFormat) 'Wave Stream Writer
                Label1.Text = "Прослушивание Оператора " & vbCrLf & ListBox1.SelectedItem.ToString
                Writer.Close()
                Writer = tempwaveSream
                ListBox1.Visible = False
                Button1.Text = "Остановить"
                stoped = False
                Dim Message = "HELLO"
                Dim client As New TcpClient("127.0.0.1", 3344)
                Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(Message)
                Dim stream As NetworkStream = client.GetStream()
                stream.Write(data, 0, data.Length)
                data = New [Byte](3200) {}
                Do
                    Try
                        If stoped = True Then
                            stream.Close()
                            client.Close()
                            Exit Sub
                        End If
                        Application.DoEvents()
                        Dim responseData As [String] = [String].Empty
                        stream.Read(data, 0, 3200)
                        Writer.WriteData(data, 0, 3200)

                    Catch ex As Exception
                        MsgBox(ex.Message)
                    End Try
                Loop
            Case Else
                Button1.Text = "Прослушать"
                Label1.Visible = False
                Button1.Top = 242
                stoped = True
                Button1.Enabled = True
                ListBox1.Visible = True
        End Select
    End Sub
End Class

 

 

When i connect to server and begin to recive strea....am save it into Wav file......after winamp can play it. But i cant play a recived stream(in a client side) to default sound output.

the Programming language is Visual Basic .net 2008.

Thanks a Lot

 

Apr 28, 2010 at 12:46 PM

the key to understanding how to use NAudio is to create derived WaveStream classes. You need to derive from WaveStream and in the Read method, return buffered data you have received from the network

Mark

Jul 2, 2010 at 9:47 AM

I written on C#

 public class Waweprov : IWaveProvider, IDisposable
    {
        private readonly WaveFormat _wformat;
        private readonly Queue<byte[]> _input;

        public WaveFormat WaveFormat
        {
            get { return _wformat; }
        }

        public Waweprov(WaveFormat wformat)
        {
            _wformat = wformat;
            _input = new Queue<byte[]>();
        }

        public void Write(byte[] buffer)
        {
            lock (_input)
                _input.Enqueue(buffer);
        }
        public int Read(byte[] buffer, int offset, int count)
        {
            byte[] readbuffer = null;

            lock (_input)
                if (0 < _input.Count)
                    readbuffer = _input.Dequeue();

            if (null != readbuffer)
                Buffer.BlockCopy(readbuffer, 0, buffer, 0, count);

            return null == readbuffer ? count : buffer.Length;
        }

        public void Dispose()
        {
            lock (_input)
                _input.Clear();
        }
    }
Data obtained unevenly at different intervals of time. 
The result can be heard repeating chunks of sound. 
How to stop play or can easily recover blank sample of sound?
Apr 5, 2011 at 3:23 PM

Did you get any success in Visual Basic after all?