Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i want to record users speech from mic automatically for 15 seconds after a certain if statement has been called and store that recorded .wav file in c:

till now i have read and implemented MSDN record function using buttons.

but i dont want button i want it to be automatic.

http://msdn.microsoft.com/en-us/library/ff827802.aspx[^]

from above link you can see how they record but using buttons .

can anyone share a tip or an idea on how to make it automatically? record without buttons?


Thank you friends
Posted

1 solution

Call micStart with your required duration (ie 15 seconds) in your if statement
Wave header class courtesy of
http://www.codeproject.com/Members/JoelIvoryJohnson

[^]
From his article Writing a Proper Wave File[^]

class MicrophoneRec {

    private Timer timer = null;
    private Microphone mic = Microphone.Default;
    List<byte[]> byteList = new List<byte[]>();
    private string outPutFile = @"D:\TempDir\test.wav";

    public void micStart(int durationToRecordSec) {

        if (mic != null) {
            timer = new Timer();
            timer.Interval = (durationToRecordSec * 1000);
            timer.Elapsed += new ElapsedEventHandler(t_Elapsed);

            mic.BufferDuration = TimeSpan.FromMilliseconds(100);
            mic.BufferReady += new EventHandler<EventArgs>(mic_BufferReady);
            timer.Start();
            mic.Start();
        }
    }

    void mic_BufferReady(object sender, EventArgs e) {

        byte[] data = new byte[mic.GetSampleSizeInBytes(mic.BufferDuration)];
        mic.GetData(data);
        byteList.Add(data);
    }

    void t_Elapsed(object sender, ElapsedEventArgs e) {

        mic.Stop();
        timer.Stop();
        byte[] buffer = new byte[byteList.Select(x => x.Length).Sum()];
        int offset = 0;
        foreach (var buff in byteList) {
            System.Buffer.BlockCopy(buff, 0, buffer, offset, buff.Length);
            offset += buff.Length;
        }

        using (FileStream fs = new FileStream(outPutFile, FileMode.Create)) {
            WaveHeader.WriteHeader(fs, buffer.Length, 1, mic.SampleRate);
            fs.Write(buffer, 0, buffer.Length);
        }
    }
}


C#
class WaveHeader {

    static byte[] RIFF_HEADER =  new byte[] { 0x52, 0x49, 0x46, 0x46 };
    static byte[] FORMAT_WAVE =  new byte[] { 0x57, 0x41, 0x56, 0x45 };
    static byte[] FORMAT_TAG =   new byte[] { 0x66, 0x6d, 0x74, 0x20 };
    static byte[] AUDIO_FORMAT = new byte[] { 0x01, 0x00 };
    static byte[] SUBCHUNK_ID =  new byte[] { 0x64, 0x61, 0x74, 0x61 };
    private const int BYTES_PER_SAMPLE = 2;

    public static void WriteHeader(System.IO.Stream targetStream,
         int byteStreamSize, int channelCount, int sampleRate) {

        int byteRate = sampleRate * channelCount * BYTES_PER_SAMPLE;
        int blockAlign = channelCount * BYTES_PER_SAMPLE;

        targetStream.Write(RIFF_HEADER, 0, RIFF_HEADER.Length);
        targetStream.Write(PackageInt(byteStreamSize + 42, 4), 0, 4);

        targetStream.Write(FORMAT_WAVE, 0, FORMAT_WAVE.Length);
        targetStream.Write(FORMAT_TAG, 0, FORMAT_TAG.Length);
        targetStream.Write(PackageInt(16, 4), 0, 4);//Subchunk1Size

        targetStream.Write(AUDIO_FORMAT, 0, AUDIO_FORMAT.Length);//AudioFormat
        targetStream.Write(PackageInt(channelCount, 2), 0, 2);
        targetStream.Write(PackageInt(sampleRate, 4), 0, 4);
        targetStream.Write(PackageInt(byteRate, 4), 0, 4);
        targetStream.Write(PackageInt(blockAlign, 2), 0, 2);
        targetStream.Write(PackageInt(BYTES_PER_SAMPLE * 8), 0, 2);
        targetStream.Write(SUBCHUNK_ID, 0, SUBCHUNK_ID.Length);
        targetStream.Write(PackageInt(byteStreamSize, 4), 0, 4);
    }

    static byte[] PackageInt(int source, int length = 2) {
        if ((length != 2) && (length != 4))
            throw new ArgumentException("length must be either 2 or 4", "length");
        var retVal = new byte[length];
        retVal[0] = (byte)(source & 0xFF);
        retVal[1] = (byte)((source >> 8) & 0xFF);
        if (length == 4) {
            retVal[2] = (byte)((source >> 0x10) & 0xFF);
            retVal[3] = (byte)((source >> 0x18) & 0xFF);
        }
        return retVal;
    }
}
 
Share this answer
 
Comments
martinjam3s 14-May-13 11:25am    
Thank you i will try it out.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900