stream audio file & send via serial port!!!!!

anow i can record , save as a file and play sound by Waves class (i use C#.NET) . but i don't know how to stream it and send it via serial port of bluetooth. i found that serialport class has a write method ,but i'm not sure that can i write sound via serial port .

can you advice me

this is my code.

using System;

using System.Drawing;

using System.Collections;

using System.Windows.Forms;

using System.Data;

using System.IO;

namespace MWORecord

{

/// <summary>

/// Summary description for MWORecord.

/// </summary>

public class MWORecord : System.Windows.Forms.Form

{

private System.Windows.Forms.Button btnPlay;

private System.Windows.Forms.Timer timer;

private System.Windows.Forms.Button btnRecord;

private System.Windows.Forms.MainMenu mainMenu1;

private System.IO.Ports.SerialPort serialPort1;

private System.ComponentModel.IContainer components;

private System.IO.Ports.SerialPort serialPort2;

private System.Windows.Forms.Label lbFileName;

// private string strPath = "My Documents";

public MWORecord()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.components = new System.ComponentModel.Container();

this.mainMenu1 = new System.Windows.Forms.MainMenu();

this.btnPlay = new System.Windows.Forms.Button();

this.timer = new System.Windows.Forms.Timer();

this.btnRecord = new System.Windows.Forms.Button();

this.lbFileName = new System.Windows.Forms.Label();

this.serialPort1 = new System.IO.Ports.SerialPort(this.components);

this.serialPort2 = new System.IO.Ports.SerialPort(this.components);

this.SuspendLayout();

//

// btnPlay

//

this.btnPlay.Location = new System.Drawing.Point(88, 48);

this.btnPlay.Name = "btnPlay";

this.btnPlay.Size = new System.Drawing.Size(72, 20);

this.btnPlay.TabIndex = 2;

this.btnPlay.Text = "Play Wave";

this.btnPlay.Click += new System.EventHandler(this.btnPlay_Click);

//

// btnRecord

//

this.btnRecord.Location = new System.Drawing.Point(8, 48);

this.btnRecord.Name = "btnRecord";

this.btnRecord.Size = new System.Drawing.Size(72, 20);

this.btnRecord.TabIndex = 1;

this.btnRecord.Text = "Record";

this.btnRecord.Click += new System.EventHandler(this.btnRecord_Click);

//

// lbFileName

//

this.lbFileName.Location = new System.Drawing.Point(8, 104);

this.lbFileName.Name = "lbFileName";

this.lbFileName.Size = new System.Drawing.Size(184, 20);

//

// serialPort1

//

this.serialPort1.PortName = "COM5";

//

// serialPort2

//

this.serialPort2.PortName = "COM8";

//

// MWORecord

//

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;

this.ClientSize = new System.Drawing.Size(240, 268);

this.Controls.Add(this.lbFileName);

this.Controls.Add(this.btnRecord);

this.Controls.Add(this.btnPlay);

this.Menu = this.mainMenu1;

this.Name = "MWORecord";

this.Text = "Record This";

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

static void Main()

{

Application.Run(new MWORecord());

}

private void btnPlay_Click(object sender, System.EventArgs e)

{

string sWaveName = "wave1.wav";

Waves.WaveOut wo = null;


try

{

wo = new Waves.WaveOut();

uint numDevices = wo.NumDevices();

if (numDevices < 1)

{

MessageBox.Show("FAILURE: No valid sound drivers detected");

return;

}

// MessageBox.Show(string.Format("{0} device{1} detected:", numDevices, numDevices != 1 "s" : ""));

for (uint i = 0; i < numDevices; i++)

{

string prodName = "";

if (Waves.Wave.MMSYSERR.NOERROR != wo.GetDeviceName(i, ref prodName))

{

MessageBox.Show(string.Format(" {0}: Failed to get name", i));

}

}

string fileName = @"My Documents\wave1.wav"; // Path.Combine(strPath, sWaveName);

if (Waves.Wave.MMSYSERR.NOERROR != wo.Play(fileName, 512*1024, 0xffff, 0xffff))

{

MessageBox.Show("FAILURE: Failed to play " + sWaveName);

}

}

finally

{

// if (wo != null) // if the wav is stopped during play you need to dispose here.

// wo.Dispose();

}


}

private void btnRecord_Click(object sender, System.EventArgs e)

{


Waves.WaveIn wi = null;

wi = new Waves.WaveIn();


//The NumDevices method provides the number of available audio output devices

uint numDevices = wi.NumDevices();

if (numDevices < 1)

{

MessageBox.Show("FAILURE: No valid sound drivers detected");

return;

}


for (uint i = 0; i < numDevices; i++)

{

string prodName = "";


if (Waves.Wave.MMSYSERR.NOERROR != wi.GetDeviceName(i, ref prodName))

MessageBox.Show(string.Format(" {0}: Failed to get name", i));

}


//The Preload function is the first step in starting a recording. This method must be called to initialize the buffers and sound system before recording. Recording is started in two steps so as to alleviate any lag in the starting of recording due to initialization. This method opens the audio device and allocates, prepares, and adds buffers to the recording queue.


if (Waves.Wave.MMSYSERR.NOERROR != wi.Preload(5000,256*1024))

{

MessageBox.Show("FAILURE: Failed to preload buffers");

}


//The Start method begins recording. Preload must be called prior to Start or it will return with an error.

if (Waves.Wave.MMSYSERR.NOERROR != wi.Start())

{


MessageBox.Show("FAILURE: Failed to start recording");

}


MessageBox.Show("Recording.......\nClose to Stop Recording");

//The Stop method stops recording and leaves the state of the record buffers so they can be saved

wi.Stop();


string fileName = @"My Documents\wave1.wav"; // Path.Combine(strPath, "wave1.wav");

try

{

if (Waves.Wave.MMSYSERR.NOERROR != wi.Save(fileName))

MessageBox.Show("FAILURE: Failed to save file");

}

catch

{

MessageBox.Show("bad news in attempting to Save. filename = " + fileName);

}

lbFileName.Text = fileName;

//Dispose stops playback and cleans up an allocated resources.

wi.Dispose();

}

}

}



Answer this question

stream audio file & send via serial port!!!!!

  • tarjan bruce

    Can we use namespace:wave to record and then write it to serial port

    or we should use other file format

    and if use others, how can we record sound into that format

    can you give me some refference or some example code,plz


  • Alessandro Camargo

    i try to stream "audio" file and send via serial port of 2 ppc. i use Namespaces "Wave" (in MSDN library) to record audio file . but i have some problems about how to stream audio and send it via serial. can anyone help me is it possible for this project (i use C# on visual studio 2005 and hp ipaq hx2400series) pls...
  • rKarthik

    anow i can record , save as a file and play sound by Waves class (i use C#.NET) . but i don't know how to stream it and send it via serial port of bluetooth. i found that serialport class has a write method ,but i'm not sure that can i write sound via serial port .

    can you advice me

    this is my code.

    using System;

    using System.Drawing;

    using System.Collections;

    using System.Windows.Forms;

    using System.Data;

    using System.IO;

    namespace MWORecord

    {

    /// <summary>

    /// Summary description for MWORecord.

    /// </summary>

    public class MWORecord : System.Windows.Forms.Form

    {

    private System.Windows.Forms.Button btnPlay;

    private System.Windows.Forms.Timer timer;

    private System.Windows.Forms.Button btnRecord;

    private System.Windows.Forms.MainMenu mainMenu1;

    private System.IO.Ports.SerialPort serialPort1;

    private System.ComponentModel.IContainer components;

    private System.IO.Ports.SerialPort serialPort2;

    private System.Windows.Forms.Label lbFileName;

    // private string strPath = "My Documents";

    public MWORecord()

    {

    //

    // Required for Windows Form Designer support

    //

    InitializeComponent();

    //

    // TODO: Add any constructor code after InitializeComponent call

    //

    }

    /// <summary>

    /// Clean up any resources being used.

    /// </summary>

    protected override void Dispose( bool disposing )

    {

    base.Dispose( disposing );

    }

    #region Windows Form Designer generated code

    /// <summary>

    /// Required method for Designer support - do not modify

    /// the contents of this method with the code editor.

    /// </summary>

    private void InitializeComponent()

    {

    this.components = new System.ComponentModel.Container();

    this.mainMenu1 = new System.Windows.Forms.MainMenu();

    this.btnPlay = new System.Windows.Forms.Button();

    this.timer = new System.Windows.Forms.Timer();

    this.btnRecord = new System.Windows.Forms.Button();

    this.lbFileName = new System.Windows.Forms.Label();

    this.serialPort1 = new System.IO.Ports.SerialPort(this.components);

    this.serialPort2 = new System.IO.Ports.SerialPort(this.components);

    this.SuspendLayout();

    //

    // btnPlay

    //

    this.btnPlay.Location = new System.Drawing.Point(88, 48);

    this.btnPlay.Name = "btnPlay";

    this.btnPlay.Size = new System.Drawing.Size(72, 20);

    this.btnPlay.TabIndex = 2;

    this.btnPlay.Text = "Play Wave";

    this.btnPlay.Click += new System.EventHandler(this.btnPlay_Click);

    //

    // btnRecord

    //

    this.btnRecord.Location = new System.Drawing.Point(8, 48);

    this.btnRecord.Name = "btnRecord";

    this.btnRecord.Size = new System.Drawing.Size(72, 20);

    this.btnRecord.TabIndex = 1;

    this.btnRecord.Text = "Record";

    this.btnRecord.Click += new System.EventHandler(this.btnRecord_Click);

    //

    // lbFileName

    //

    this.lbFileName.Location = new System.Drawing.Point(8, 104);

    this.lbFileName.Name = "lbFileName";

    this.lbFileName.Size = new System.Drawing.Size(184, 20);

    //

    // serialPort1

    //

    this.serialPort1.PortName = "COM5";

    //

    // serialPort2

    //

    this.serialPort2.PortName = "COM8";

    //

    // MWORecord

    //

    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;

    this.ClientSize = new System.Drawing.Size(240, 268);

    this.Controls.Add(this.lbFileName);

    this.Controls.Add(this.btnRecord);

    this.Controls.Add(this.btnPlay);

    this.Menu = this.mainMenu1;

    this.Name = "MWORecord";

    this.Text = "Record This";

    this.ResumeLayout(false);

    }

    #endregion

    /// <summary>

    /// The main entry point for the application.

    /// </summary>

    static void Main()

    {

    Application.Run(new MWORecord());

    }

    private void btnPlay_Click(object sender, System.EventArgs e)

    {

    string sWaveName = "wave1.wav";

    Waves.WaveOut wo = null;


    try

    {

    wo = new Waves.WaveOut();

    uint numDevices = wo.NumDevices();

    if (numDevices < 1)

    {

    MessageBox.Show("FAILURE: No valid sound drivers detected");

    return;

    }

    // MessageBox.Show(string.Format("{0} device{1} detected:", numDevices, numDevices != 1 "s" : ""));

    for (uint i = 0; i < numDevices; i++)

    {

    string prodName = "";

    if (Waves.Wave.MMSYSERR.NOERROR != wo.GetDeviceName(i, ref prodName))

    {

    MessageBox.Show(string.Format(" {0}: Failed to get name", i));

    }

    }

    string fileName = @"My Documents\wave1.wav"; // Path.Combine(strPath, sWaveName);

    if (Waves.Wave.MMSYSERR.NOERROR != wo.Play(fileName, 512*1024, 0xffff, 0xffff))

    {

    MessageBox.Show("FAILURE: Failed to play " + sWaveName);

    }

    }

    finally

    {

    // if (wo != null) // if the wav is stopped during play you need to dispose here.

    // wo.Dispose();

    }


    }

    private void btnRecord_Click(object sender, System.EventArgs e)

    {


    Waves.WaveIn wi = null;

    wi = new Waves.WaveIn();


    //The NumDevices method provides the number of available audio output devices

    uint numDevices = wi.NumDevices();

    if (numDevices < 1)

    {

    MessageBox.Show("FAILURE: No valid sound drivers detected");

    return;

    }


    for (uint i = 0; i < numDevices; i++)

    {

    string prodName = "";


    if (Waves.Wave.MMSYSERR.NOERROR != wi.GetDeviceName(i, ref prodName))

    MessageBox.Show(string.Format(" {0}: Failed to get name", i));

    }


    //The Preload function is the first step in starting a recording. This method must be called to initialize the buffers and sound system before recording. Recording is started in two steps so as to alleviate any lag in the starting of recording due to initialization. This method opens the audio device and allocates, prepares, and adds buffers to the recording queue.


    if (Waves.Wave.MMSYSERR.NOERROR != wi.Preload(5000,256*1024))

    {

    MessageBox.Show("FAILURE: Failed to preload buffers");

    }


    //The Start method begins recording. Preload must be called prior to Start or it will return with an error.

    if (Waves.Wave.MMSYSERR.NOERROR != wi.Start())

    {


    MessageBox.Show("FAILURE: Failed to start recording");

    }


    MessageBox.Show("Recording.......\nClose to Stop Recording");

    //The Stop method stops recording and leaves the state of the record buffers so they can be saved

    wi.Stop();


    string fileName = @"My Documents\wave1.wav"; // Path.Combine(strPath, "wave1.wav");

    try

    {

    if (Waves.Wave.MMSYSERR.NOERROR != wi.Save(fileName))

    MessageBox.Show("FAILURE: Failed to save file");

    }

    catch

    {

    MessageBox.Show("bad news in attempting to Save. filename = " + fileName);

    }

    lbFileName.Text = fileName;

    //Dispose stops playback and cleans up an allocated resources.

    wi.Dispose();

    }

    }

    }


  • Urban Terror target

    Hi,

    This subject was very interesting. I'll try to stream audio from SmartPhone to a PC.

    Are we oblige to use Wave to recording In absolute, i want to send data in G711 format.

    It's very difficult to find a solution for streaming audio from Smartphone microphone ...

    Thanks in advance,

    French Developer.

    C# , VS 2005



  • JoshKorn

    Hi,

    I have been doing the same project as yours and I am currently stuck at the streaming. Have you guys succeeded in doing the streaming Is it ok for you to help me I need help urgently.

    Thanks,

    Winson



  • Zero_

    Please do not cross post... Merging...

    As to your "some problems", what problems are they exactly

    You’d need to do something like this to send:

    port.Write(myByteArrayWithPCMDataToSend, …)

    And something like this to receive:

    port.Read(myByteArrayWithPCMDataToReceive,..)

    You’d need at least two buffers, one would be recorded to, abother one will be send via serial port. Same on receiving size – one receiving next PCM chunk from serial port, another one is playing. Swap them as needed.

     



  • stream audio file & send via serial port!!!!!