Sound recording with API

Hello my friends,
I spent a lot of time to try to record sound from a microphone into a wav file.
The API commands seem to work in VB but would anybody have already managed to record sound with RealBasic?
Other MCI commands work very well but not the recording.
Here are the necessary commands:

s is a MemoryBlock i = mciSendString("Open new type waveaudio alias RecFile", s, 1024, 0) i = mciSendString("Record RecFile", s, 1024, 0)

Thank you for your help.

BB

To record a sound with Windows 10 on Xojo, first make a global property called:

Dim S as String = "SoundName"

Create a new file folder called c:\test\

The first instruction starts a new session to be recorded and the second instruction records the physical input from the microphone.
In the action event of the first pushbutton add:

[code]Declare Function mciSendString lib “winmm.dll” Alias “mciSendStringA” (lpstrCommand as Cstring, _
lpstrReturnString as CString, uReturnLength as Integer, hwndCallback as Integer) as Integer

//Dim S as String = “SoundName” 'global property

Call mciSendString(“open new type waveaudio alias RecFile”, s, 1024, 0)
Call mciSendString(“record RecFile”, s, 1024, 0)[/code]

Two instructions are sent again and the first instruction saves the recorded sound in c:\test\SoundName.wav file and path. The second instruction closes the recording session.
In the action event of the second pushbutton add:

[code]Declare Function mciSendString lib “winmm.dll” Alias “mciSendStringA” (lpstrCommand as Cstring, _
lpstrReturnString as CString, uReturnLength as Integer, hwndCallback as Integer) as Integer

//Dim S as String = “SoundName” 'global property

Call mciSendString(“save RecFile c:\test”+s+".wav", s, 1024, 0)
Call mciSendString(“close recsound”, s, 1024, 0)[/code]

To play the recorded sound, add the following to a third pushbutton action event:

Dim f as FolderItem = GetFolderItem("c:\\test\"+s+".wav") If f <> Nil Then Dim MySound as Sound = f.OpenAsSound If MySound <> Nil Then MySound.Play Else //Check f.LastErrorCode End If Else //UserCancelled End If

Edit: P.S. You may want to change this question from the General category to Windows. :slight_smile:

Thank you for sharing this Eugene.

This was actually exactly what I was also looking for in one of my projects.

Hello Eugen,

Thank you very much for your answer and explanations.
Firstable I forgot to add the save and close commands in my post.

I read yesterday in an VB forum that by saving the record in the same directory than the program it will work.
So by combining your code and that remark that finally works!!!

[code]Call mciSendString(“open new type waveaudio alias RecFile”, s, 1024, 0)
Call mciSendString(“record RecFile”, s, 1024, 0)

Call mciSendString(“save RecFile whatever.wav”, s, 1024, 0)
Call mciSendString(“close recsound”, s, 1024, 0)
[/code]

You have certainly an answer to that strange thing. So with c:\test"+s+".wav" it doesn’t work.
The difference between your code and mine is the “Call” in front of the MCI command.
Do you think it could be the only reason?
I am working with RealBasic 2010 on Windows 7.

Best Regards

BB

[quote=329090:@BenotBouillon]I read yesterday in an VB forum that by saving the record in the same directory than the program it will work.
So by combining your code and that remark that finally works!!![/quote]

Don’t do that. It is a bad habit from way back when.

It is not recommended by Microsoft at all. Some administrators already prevent writing there, and chances are in the future it will be forbidden anyway. It is already impossible in Windows Store apps.

Rather create your own subdirectory in SpecialFolder.ApplicationData such as com.supersoftware.mygreatprogram where you know you can safely save anything you want and find it back at next launch. That is also where you should save your databases and preference files.

[code]Dim f as folderitem = specialfolder.applicationdata.child(“com.supersoftware.mygreatprogram”)
if not f.exists then f.createasfolder

f = f.child(“Mysound1.wav”)[/code]

Then use f the same way you would have next to the executable.

If you need the path, just use f.shellpath

Hello Michel,

Thanks for that info, you are right because during my trials I have found my file elsewhere.
So I tried that and come back to you.

Best Regards.

BB

[quote=329090:@BenotBouillon]The difference between your code and mine is the “Call” in front of the MCI command.
Do you think it could be the only reason?[/quote]

Call simply tells Xojo to ignore the return value, there shouldn’t be any difference there.

Probably Windows 7 is less lenient in the string mciSendString uses and your use of “Open” should be “open”, I’ve read it is case sensitive, but I have tested it in Windows 10 and that didn’t seem the case, but in Windows 7 it may be.

[quote=329090:@BenotBouillon]You have certainly an answer to that strange thing. So with c:\test"+s+".wav" it doesn’t work.
The difference between your code and mine is the “Call” in front of the MCI command.
Do you think it could be the only reason?
I am working with RealBasic 2010 on Windows 7.[/quote]

Hello Benoit,

Here is a download link for the example. Please make a c:\test\ folder before running the program.

APISound.zip

This works on Windows 10, Xojo 2017.

If the program does not work, maybe download the free trial version of Xojo 2017 and run the program on Windows 7.

Hello My Friends,

I took back my program this evening and I am always in the same situation, even worse.
I have tried your proposal Michel but nothing to do it does not work.
If I give as path a directory + the file name the record is not created.
If I give the file name only the file is created but remains empty.
Eugen I can’t open your attached file? I am sorry but I am not a specialist.

Thank you for your patience.

BB

Its ok. I installed RealStudio 2010 r4 and created the file in rbp format. Download this file and it worked on my machine.

APISound.zip

Download the file, unzip the file and then open APISound.rbp with Real Studio.

  1. Make sure the C:\test\ file exists on your computer
  2. Run the Program
  3. Press the Record button and talk into the microphone
  4. Press Save to create a .wav file in C:\test\ folder
  5. Press Play to play the recording on your computer.

Happy to help

Please excuse me for my absence but I come back to thank you for your help.
I just tested it and it works fine.

Thank you.

Benot