Results 1 to 23 of 23

Thread: creating wav files.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 1999
    Location
    u.s.a
    Posts
    127
    I made my last post very clumsy.
    I'll try again.
    How can I craete a wav file through code?
    Thanks.
    Dan.

  2. #2
    Hyperactive Member Juan Carlos Rey's Avatar
    Join Date
    Aug 1999
    Location
    Mendoza, Argentina
    Posts
    301

    Try this:

    This code will produce about 5-6 seconds of a pure 440 Hz tone (a boring "A" note).

    Use a Text.Box and a Command.Button:

    Code:
    Private Sub Command1_Click()
    Text1.Text = ""
    Open "c:\sinus.wav" For Output As #1
    Print #1, "RIFF";
    Print #1, Chr(40); Chr(0); Chr(1); Chr(0);
    Print #1, "WAVE";
    Print #1, "fmt ";
    Print #1, Chr(16); Chr(0); Chr(0); Chr(0);
    Print #1, Chr(1); Chr(0); Chr(1); Chr(0);
    Print #1, Chr(17); Chr(43); Chr(0); Chr(0);
    Print #1, Chr(17); Chr(43); Chr(0); Chr(0);
    Print #1, Chr(1); Chr(0); Chr(8); Chr(0);
    Print #1, "data";
    Print #1, Chr(4); Chr(0); Chr(1); Chr(0);
    For i = 1 To 65536
    sinus = (Sin(i / 25 * 6.2832)) '440 Hz
    sinus = CInt(sinus * 127) + 128
    Print #1, Chr(sinus);
    Next
    
    Close
    Text1.Text = "Ready!"
    End Sub
    Re-post if ya need more help.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 1999
    Location
    u.s.a
    Posts
    127
    Thanks,it is almost what I need.
    I need to create a totaly empty .wav file ,so I guess there is not much to change in the code you sent me.
    Maybe there is an easier solution for my problem:

    I am using the multimedia control to record .wav files.
    The command: mmcontrol1.command="Save" will only save the recording to an existing file!
    That means I have to create an empty .wav file and then
    save the recording to that file using the command line:
    mmcontrol1.command="Save".
    I have feeling I am missing something big here because it doesn't make sense that one can only record to an existing .wav file that can not be created through code.
    Please tell me I am.
    If I have to do it "your way",or you can't help me with the latter, please tell me what lines I have to drop
    from your code,to get an empt wav file.
    Thanks again
    Dan.

  4. #4
    Hyperactive Member Juan Carlos Rey's Avatar
    Join Date
    Aug 1999
    Location
    Mendoza, Argentina
    Posts
    301
    For a complete description of the Wav File Format, see:
    http://www.music-center.com.br/princip.htm

    There are other sites with simpler descriptions, thou.

    In my code, not really sure, but try this:

    1. Replace the second "Print" isntruction with: Print #1, Chr(0); Chr(0); Chr(1); Chr(0);
    2. Delete all between For ... Next (also the For and the Next themselves!)
    3. That is!


    This I couldn't try 'couse I do not have VB in this machine, but almost sure that's all needed.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 1999
    Location
    u.s.a
    Posts
    127

    Juan you are the king

    You are the kingYou are the kingYou are the king
    You are the kingYou are the kingYou are the king
    You are the kingYou are the kingYou are the king
    You are the kingYou are the kingYou are the king
    You are the kingYou are the kingYou are the king
    You are the kingYou are the kingYou are the king
    You are the kingYou are the kingYou are the king
    You are the kingYou are the kingYou are the kingYou are the kingYou are the kingYou are the kingYou are the kingYou are the kingYou are the kingYou are the kingYou are the kingYou are the kingYou are the kingYou are the kingYou are the kingYou are the kingYou are the kingYou are the kingYou are the kingYou are the kingYou are the kingYou are the kingYou are the kingYou are the kingYou are the kingYou are the kingYou are the kingYou are the king

  6. #6
    Hyperactive Member Juan Carlos Rey's Avatar
    Join Date
    Aug 1999
    Location
    Mendoza, Argentina
    Posts
    301
    1- Still unable to edit/delete - Refresh, disconnect/turn machine off then try again, nothing seems to work.

    2-
    Code:
    For N = 1 to 1000
    You're welcome
    Next
    3- BTW, did you know that my name really really means King?



  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 1999
    Location
    u.s.a
    Posts
    127
    Juan, sorry my mistake,it didn't work.
    Although windows does recognise the file as a wav file,It still doesn't work.
    I opened in notpad, "your file" ,and also a new empty wav file (by right clicking in a windows directory and choosing "new wav file" in the popup menu).
    I noticed differences between both files.
    Any suggestions??
    Thanks.
    Dan.

  8. #8
    Hyperactive Member Juan Carlos Rey's Avatar
    Join Date
    Aug 1999
    Location
    Mendoza, Argentina
    Posts
    301
    Sorry there was a minor error:

    Code:
    Private Sub Command1_Click()
    Text1.Text = ""
    Open "c:\sinus.wav" For Output As #1
    Print #1, "RIFF";
    Print #1, Chr(4); Chr(0); Chr(1); Chr(0);
    Print #1, "WAVE";
    Print #1, "fmt ";
    Print #1, Chr(16); Chr(0); Chr(0); Chr(0);
    Print #1, Chr(1); Chr(0); Chr(1); Chr(0);
    Print #1, Chr(17); Chr(43); Chr(0); Chr(0);
    Print #1, Chr(17); Chr(43); Chr(0); Chr(0);
    Print #1, Chr(1); Chr(0); Chr(8); Chr(0);
    Print #1, "data";
    Print #1, Chr(4); Chr(0); Chr(1); Chr(0);
    For i = 1 To 65568
    Print #1, " ";
    Next
    Close
    Text1.Text = "Ready!"
    End Sub

  9. #9
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    The MMControl doesn't require a Wav file to exist in order to record one, example:
    Code:
    Private Sub cmdSave_Click()
        MMControl1.Command = "Save"
    End Sub
    
    Private Sub Form_Load()
        With MMControl1
            .DeviceType = "WaveAudio"
            .FileName = "C:\New.wav"
            .RecordMode = mciRecordOverwrite
            .UpdateInterval = 10
            .Command = "Open"
        End With
    End Sub
    
    Private Sub MMControl1_StatusUpdate()
        With MMControl1
            Caption = (.Position / 1000) & "/" & (.TrackLength / 1000)
        End With
    End Sub

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Sep 1999
    Location
    u.s.a
    Posts
    127
    Aaron
    The save function does not create a new file if one does not exist,even though one would think it should.
    I have tested this a few times with the code you presented and came to same result - no new file is created.
    However if a file does exist,
    (with the same name & path),then the mci recording is saved to it when given the command:
    MMControl1.Command = "Save".

    Again,I have a fealing I am missing something.
    Thanks,
    Dan.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Sep 1999
    Location
    u.s.a
    Posts
    127
    Juan:
    Is the loop that writes " " to the file realy a good idea?
    I don't think there are more than 65568 charecters in a newly created wav file (when you create it using the windows menu).
    I am about to try anyway.
    Cross your fingers.
    Dan.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Sep 1999
    Location
    u.s.a
    Posts
    127
    Juan ,sorry ,didn't work.
    We are close though.
    With the loop added windows does not recognise it as a wav file.
    If you keep sending suggestions,I will be most gratefull.
    Thanks.
    Dan.

  13. #13
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    Did you cut & paste the code I posted into a New Project and test it as is?

    I've used this code several times each time it creates a new file.

    If you think about it there is no need for the file to exist, when you pass the Command Method the Open String, it's Opening the Device, not the Wav File, nothing is saved to disk until you send the Save Command.

    I'd suggest trying the code I posted again, and perhaps anyone else reading this could try it also and post if it doesn't work.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Sep 1999
    Location
    u.s.a
    Posts
    127
    I decided to run a small test project consisting only of this code:
    Private Sub Form_Load()

    --------------------------------
    With Form1
    .MMControl1.DeviceType = "WaveAudio"
    .MMControl1.filename = App.Path & "HellOnEarth.wav"
    .MMControl1.RecordMode = mciRecordOverwrite
    .MMControl1.UpdateInterval = 10
    .MMControl1.Command = "Open"
    End With
    End Sub

    Private Sub save_Click()
    With Form1

    .MMControl1.Command = "Save" ' save wav to created file
    End With
    End Sub
    ------------------------------------

    No new file is created.
    It only works if the file already exists.
    HHHHHHHHHHHHHHHHHHHHelp?????

    Thanks
    Dan.


  15. #15

    Thread Starter
    Lively Member
    Join Date
    Sep 1999
    Location
    u.s.a
    Posts
    127
    I decided to run a small test project consisting only of this code:
    Private Sub Form_Load()

    --------------------------------
    With Form1
    .MMControl1.DeviceType = "WaveAudio"
    .MMControl1.filename = App.Path & "/HellOnEarth.wav"
    .MMControl1.RecordMode = mciRecordOverwrite
    .MMControl1.UpdateInterval = 10
    .MMControl1.Command = "Open"
    End With
    End Sub

    Private Sub save_Click()
    With Form1

    .MMControl1.Command = "Save" ' save wav to created file
    End With
    End Sub
    ------------------------------------

    No new file is created.
    It only works if the file already exists.
    HHHHHHHHHHHHHHHHHHHHelp?????

    Thanks
    Dan.


  16. #16
    Hyperactive Member Juan Carlos Rey's Avatar
    Join Date
    Aug 1999
    Location
    Mendoza, Argentina
    Posts
    301

    Sorry again

    Sorry I was away for a while.

    This code produces an empty Wav file:

    Code:
    Open "c:\sinus.wav" For Output As #1
    Print #1, "RIFF";
    Print #1, Chr(0); Chr(0); Chr(1); Chr(0);
    Print #1, "WAVE";
    Print #1, "fmt ";
    Print #1, Chr(16); Chr(0); Chr(0); Chr(0);
    Print #1, Chr(1); Chr(0); Chr(1); Chr(0);
    Print #1, Chr(17); Chr(43); Chr(0); Chr(0);
    Print #1, Chr(17); Chr(43); Chr(0); Chr(0);
    Print #1, Chr(1); Chr(0); Chr(8); Chr(0);
    Print #1, "data";
    Print #1, Chr(0); Chr(0); Chr(0); Chr(0);
    Close
    I was making a little error: in order to change the file size, you must modify two lines of code: a pointer and data size. Now it works for me.

    There are other data in the heading, such as stereo/mono, sample rate (frequency of sampling), block data size, etc. etc. But to produce an empty file, I used the most standard format: mono, 8 bits, 16000 samples/second.

    Hope it finally works!!!

    [Edited by Juan Carlos Rey on 03-19-2000 at 11:11 AM]

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Sep 1999
    Location
    u.s.a
    Posts
    127
    Juan-this time it worked.
    As I said before - you are the king.
    100%
    Thanks again.
    Dan.

  18. #18
    Lively Member
    Join Date
    Aug 1999
    Location
    Malaysia
    Posts
    108

    Wav File Project

    Hello danab..

    could you post full code to me. I'll like sharing knowledge
    with you...please.

    thanks

  19. #19
    New Member
    Join Date
    Mar 2000
    Posts
    1
    Danab,

    I have had the same problem saving a .wav file.
    The mmcontrol1 works perfectly in VB3, but don't use it
    in VB5 if you want to save a .wav file. I can suggest 3 approaches:

    Approach #1) Use the Windows API: the declaration looks like this:

    Declare Function mciSendString Lib "winmm.dll" Alias _
    "mciSendStringA" (ByVal lpstrCommand As String, ByVal _
    lpstrReturnString As Any, ByVal uReturnLength As Long, ByVal _
    hwndCallback As Long) As Long

    To OPEN A NEW FILE:

    Sub WavRecOpen(FileName As String)
    Dim i As Long, RS As String, cb As Long, t#
    On Local Error Resume Next

    RS = Space$(128)
    Kill "c:\" & FileName & ".wav"

    i = mciSendString("open wavaudio", RS, 128, cb)
    i = mciSendString("open new type waveaudio alias
    capture", RS, 128, cb)

    End Sub

    To SAVE A NEW WAVE FILE:

    Sub WavRecClose(FileName As String)

    Dim i As Long, RS As String, cb As Long, t#
    RS = Space$(128)

    i = mciSendString("close capture", RS, 128, cb)

    i = mciSendString("close c:\" & FileName & ".wav", RS, 128, cb)

    End Sub


    To START RECORDING:
    Sub WavRecStart()
    Dim i As Long, RS As String, cb As Long, t#
    On Local Error Resume Next

    RS = Space$(128)

    i = mciSendString("record capture", RS, 128, cb)

    'MsgBox "RS = " & RS

    End Sub

    To STOP RECORDING:
    Sub WavRecStop()

    Dim i As Long, RS As String, cb As Long, t#
    RS = Space$(128)

    i = mciSendString("stop capture", RS, 128, cb)

    End Sub

    These functions will work, but if you program crashes, Windows will still be recording. This brings me to

    APPROACH #2) Take the above code and make a recording
    control/.ocx


    APPROACH #3) If that isn't something that you want to get into, get a third party control. I know that there is a company called SwiftSoft that makes a few audio controls.
    There are other companies as well.

    Anyways, I have done all 3 and had success with all.

    Option #2 was a very light control, and was a good learning experience on making .ocx's. I just followed the VB tutorial.

    Good Luck,

    -Todd.

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Sep 1999
    Location
    u.s.a
    Posts
    127
    Todd ,Thanks for the replay.
    The code Juan suggested workes fine ,so I'm sticking to that approach.
    I think the control works fine in vb6 (when saving to an uncreated file),and thats why it worked for Aaron young and not for me (I am using vb5).
    Thanks.
    Dan.

  21. #21

    Thread Starter
    Lively Member
    Join Date
    Sep 1999
    Location
    u.s.a
    Posts
    127
    to alwsid:
    sorry I took a while:
    if you have vb6 don't bother doing the first step,the mmcontrol will do it for you automaticaly:
    first create the wav file:
    nfile = FreeFile
    Open WavDirectory & "\" & Record_File & ".wav" For Output As #nfile
    Print #nfile, "RIFF";
    Print #nfile, Chr(0); Chr(0); Chr(1); Chr(0);
    Print #nfile, "WAVE";
    Print #nfile, "fmt ";
    Print #nfile, Chr(16); Chr(0); Chr(0); Chr(0);
    Print #nfile, Chr(1); Chr(0); Chr(1); Chr(0);
    Print #nfile, Chr(17); Chr(43); Chr(0); Chr(0);
    Print #nfile, Chr(17); Chr(43); Chr(0); Chr(0);
    Print #nfile, Chr(1); Chr(0); Chr(8); Chr(0);
    Print #nfile, "data";
    Print #nfile, Chr(0); Chr(0); Chr(0); Chr(0);
    Close #nfile

    then open the mci control :

    With .MMControl1
    .Notify = False
    .Wait = True
    .Shareable = False
    .DeviceType = "WaveAudio"
    .Filename = WavDirectory & "\" & Record_File & ".wav"
    .RecordMode = mciRecordOverwrite
    .UpdateInterval = 10
    .Command = "Open"
    End With

    Then after you have recorded to your content create a command button to save the recording with the following code:
    .MMControl1.Command = "Save" ' save wav to created file
    .MMControl1.Command = "Close" 'close mci control

    Hope that helped.
    Dan.

  22. #22
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    Prague, Czech Republic
    Posts
    350
    How can I record to a wave file directly? (Collecting a data from input, display them /in real time/ and then write it to the file?)

    -----------
    Thanks,
    John (408697 sec. to 15th birthday!)

  23. #23
    New Member
    Join Date
    Mar 2000
    Posts
    1
    Todd T - thanks for the start. I modified your code a bit, and I have some working code now. But I am still having one problem: I get an error frequently that "All wav devices that can record files in the current format are in use. Wait until a wave device is free, and then try again." This error happens every time I have already recorded and then I try to record again. Even if I restart my app, it gives me the error. The only way to not get the error is to wait about 5 minutes, and then it seems to work ok. Anyone have any ideas? Here's my code:
    Thanks!

    Public Function WavRecordOpenFile() As Boolean

    WavRecordOpenFile = False

    If MyMciSendString("close all") Then
    '
    End If
    If MyMciSendString("open waveaudio") Then
    If MyMciSendString("open new type waveaudio alias gwavrec") Then
    WavRecordOpenFile = True
    End If
    End If

    End Function

    Public Function WavRecordCloseFile(sFile As String) As Boolean

    WavRecordCloseFile = False

    If MyMciSendString("save gwavrec " & sFile) Then
    If MyMciSendString("close gwavrec") Then
    WavRecordCloseFile = True
    End If
    End If

    End Function

    Public Function WavStartRecord() As Boolean

    If MyMciSendString("set gwavrec time format ms bitspersample 16 channels 2 samplespersec 44100") Then
    '
    End If

    WavStartRecord = MyMciSendString("record gwavrec")

    End Function

    Public Function WavStopRecord() As Boolean

    WavStopRecord = MyMciSendString("stop gwavrec")

    End Function

    Public Function MyMciSendString(lpstrCommand As String) As Boolean
    Dim lRet As Long
    Dim sRet As String

    MyMciSendString = False

    On Local Error Resume Next

    sRet = Space(128)
    lRet = mciSendString(lpstrCommand, sRet, 128, 0&)
    sRet = StringFromBuffer(sRet)

    If lRet <> 0 Then
    MsgBox "mciSendString " & lpstrCommand & vbCrLf & "Error: " & Format(lRet) & vbCrLf & sRet & vbCrLf & MyMciErrorString(lRet), vbExclamation
    Else
    MsgBox "mciSendString " & lpstrCommand & vbCrLf & sRet
    MyMciSendString = True
    End If

    End Function

    Public Function MyMciErrorString(lErr As Long) As String
    Dim sErr As String
    Dim lRet As Long
    sErr = Space(128)
    lRet = mciGetErrorString(lErr, sErr, 128&)
    MyMciErrorString = StringFromBuffer(sErr)
    End Function

    Public Function StringFromBuffer(Buffer As String) As String
    Dim nPos As Long

    nPos = InStr(Buffer, vbNullChar)
    If nPos > 0 Then
    StringFromBuffer = Left$(Buffer, nPos - 1)
    Else
    StringFromBuffer = Buffer
    End If
    End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width