• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

Recording in VB

Please guide me to record a wave file using the low-level (not MCI) Win32 API calls. I plan to use it to produce an oscilloscope
0
simchiang
Asked:
simchiang
  • 4
  • 4
1 Solution
 
simchiangAuthor Commented:
Adjusted points to 150
0
 
trilloCommented:
I'm currently developing an app that records sound using the low level Waveaudio functions, and it's not very easy....hmmm.... actually I'm using VC++. The problem is that a lot of those functions recieve adresses as parameters. Another problem is that you have to send empty buffers to the soundcard to fill them in with wave data. Thereafter you must analize those buffers, and for this purpose I'm using pointers..... I know a lot of VB but I don't know if it woul be easy (or even possible) to translate the code form C++ to VB. (because as you know, you don't have pointers in VB, and you can't handle adresses as easily)

Anyway, I can help you with some VC++ code (if you know C++ you can translate it) or otherwise I can try to tell you how would you do it in VB.
Please place a comment here telling me what you think, and how could I help you..

trillo
0
 
simchiangAuthor Commented:
trillo
I'm hopeless in C++ or even VC++. Please help in what ever way. I need to know the steps to set up the wave headers and start recording into buffers. I know how to play a wave file using low-level API though.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
trilloCommented:
I'll be soon back with an answer... be a little bit patient please!

Trillo
0
 
trilloCommented:

>>I know how to play a wave file using low-level API though.
In this case, I will asume that you know how to declare the databuffers, if not, tell me. (I've not tried in VB but I supose you must use an array of bytes)

Note on my answer: The API declarations and structures I place here were token form the VB 4.0 API text wiewer, and they'll differ a little bit form my C++ program because I'm using VC++ 5.0 and it has some new API's and structures.

1.-First of all you have to declare a wave format structure

Type WAVEFORMAT
    wFormatTag As Integer     '  format type
    nChannels As Integer      '  number of channels (i.e. mono, stereo, etc.)
    nSamplesPerSec As Long    '  sample rate
    nAvgBytesPerSec As Long   '  for buffer estimation
    nBlockAlign As Integer    '  block size of data
End Type

VERSION NOTE: I used another structure: WAVEFORMATEX (I didn't find the declaration in VB 4.0). In the followint lines, I'll asume that you have the API text viewer so I won't place the function and structure declarations explicitly.

2.-Next, you have to call the "waveInOpen" function with the WAVE_FORMAT_QUERY flag (last param), to ensure the soundcard supports the format. The return value must be different to the MMSYSERR_NOERROR constant. (The Wavein device won't be opened yet)

3.-Next, you have to call the "waveInOpen" function again but this time without the WAVE_FORMAT_QUERY flag, and you must pass the WAVEFORMAT structure, now, the device will be opend for recording.

4.-Next, you have to declare some (2 or 3 or more maybe) WAVEHDR structures to specify the amount of data that can be filled in each buffer.
Type WAVEHDR
    lpData As Long                 '  pointer to locked data buffer
    dwBufferLength As Long         '  length of data buffer
    dwBytesRecorded As Long        '  used for input only
    dwUser As Long                 '  for client's use
    dwFlags As Long                '  assorted flags (see defines)
    dwLoops As Long                '  loop control counter
    wavehdr_tag As Long            '  reserved for driver
    reserved As Long               '  reserved for driver
End Type

For example:

Dim wh as WAVEHDR
wh.lpData:  adress of array of bytes
wh.dwBufferLength: size of the array
wh.dwFlags = 0
wh.dwLoops = 0
(The other members can be ignored)

5.-Next, you have to use the "waveInPrepareHeader" and "waveInAddBuffer" functions for each WAVEHDR declared, to prepare and send the buffers to be filled to the soundcard.

6.-The final step: call the "wveInStart" function to start recording, passing the handle to the wave device obtained with the waveInOpen function in step 3.

-----------------------
I don't know how did you manage to play sound using low-level API, because windows notifies your App when a buffer is filled, and as far as I know, you can't recieve notification messages in VB (version 4.0 at least)..., but you have to use the same methodology.
You can test the dwFlags member form a WAVEHDR against the WHDR_DONE constant to know if a buffer is full or not, when it's full you must do what is explained in step 7.
-----------------------
7.-when windows notifies you, that a buffer has been filled you can read the buffer's data to do anithing what you want to it, then, you must use the "waveInUnprepareHeader" to clean the buffer, and then you must use "waveInPrepareHeader" and "waveInAddBuffer" again for your buffer to be filled.

Note: for example: if you declare 3 WAVEHDR structures, you must prepare and send them. When buffer#1 is filled, you can read it's data while the soundcard fills buffer#2, this gives you the time to read, unprepare, prepare and send the buffer#1 again. When the soundcard finished with buffer#2, you do the same while it's filling the buffer #3, etc...etc.

Trillo
0
 
simchiangAuthor Commented:
Shall wait for you. You may also email me at simchiang@ieee.org
Thanks.
0
 
trilloCommented:
>> Shall wait for you.
Did you see the proposed answer?
0
 
simchiangAuthor Commented:

You're right there's no WAVEFORMATEX even in VB6.

Please tell me more about:
a) declaring the databuffers and use them.
b) what are the parameters that I have to fill in in waveInOpen
   and with what.
c) how to use waveInAddBuffer - never used it before.
d) I don't know if in VB6, it can receive Windows notification.
   But please elaborate further on how to test the dwFlags as
   you mentioned.

Could you email me so that we get to correspond faster in the future? I think I'll need to ask you more questions as I continue with the oscilloscope program.

Sim Chiang

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now