Solved

Recording in VB

Posted on 1998-11-17
8
161 Views
Last Modified: 2010-05-03
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
Comment
Question by:simchiang
  • 4
  • 4
8 Comments
 

Author Comment

by:simchiang
ID: 1445200
Adjusted points to 150
0
 
LVL 2

Expert Comment

by:trillo
ID: 1445201
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
 

Author Comment

by:simchiang
ID: 1445202
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 2

Expert Comment

by:trillo
ID: 1445203
I'll be soon back with an answer... be a little bit patient please!

Trillo
0
 
LVL 2

Accepted Solution

by:
trillo earned 150 total points
ID: 1445204

>>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
 

Author Comment

by:simchiang
ID: 1445205
Shall wait for you. You may also email me at simchiang@ieee.org
Thanks.
0
 
LVL 2

Expert Comment

by:trillo
ID: 1445206
>> Shall wait for you.
Did you see the proposed answer?
0
 

Author Comment

by:simchiang
ID: 1445207

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

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MS Access Search and Replace Using VBA 6 72
How to hault or freeze parent form when a 2d form is open in vb6 3 38
fso.FolderExists("\\server\HiddenFolder$") 4 68
vbModal 12 53
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question