Solved

Recording in VB

Posted on 1998-11-17
8
154 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
 
LVL 2

Expert Comment

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

Trillo
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now