QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: SpriggsySpriggs on September 26, 2020, 09:59:28 pm

Title: Recording Audio in QB64?!?!
Post by: SpriggsySpriggs on September 26, 2020, 09:59:28 pm
Well,well,well.... I found out that you can record audio in QB64 with one of the WinAPI functions from WINMM.DLL....
I'm not sure if someone already had a way to record audio in QB64 but I couldn't find anything in the forum.

I posted it in my API collection. Here are two recordings I made in QB64:
  [ This attachment cannot be displayed inline in 'Print Page' view ]    [ This attachment cannot be displayed inline in 'Print Page' view ]  
Title: Re: Recording Audio in QB64?!?!
Post by: FellippeHeitor on September 26, 2020, 10:57:34 pm
"This is meeee, singing into the microphone" %uD83C%uDFA4%uD83C%uDFB5%uD83C%uDFB6

I think someone ventured into audio recording via mci strings back in .net. Thanks for bringing the topic back up.
Title: Re: Recording Audio in QB64?!?!
Post by: SpriggsySpriggs on September 26, 2020, 11:02:35 pm
%uD83C%uDFA4%uD83C%uDFB5%uD83C%uDFB6
What is that?
Title: Re: Recording Audio in QB64?!?!
Post by: FellippeHeitor on September 26, 2020, 11:10:50 pm
Damn, they were emoji. 🎵
Title: Re: Recording Audio in QB64?!?!
Post by: Pete on September 26, 2020, 11:20:47 pm
FreeBASIC had a way to accomplish this. I remember the brief time I used FB, and made one recording while learning the language: swearwords.wav

Hey, pretty cool, Zach! I got by on javascript and html. Just change the path to wherever you keep your QB64 files, and, of course, the recording.wav needs to be in that folder. Just save the code below as whatever.html, in your QB64 folder, click it, and it will appear in your default browser. Click the play button to hear Zach's recording.

Code: Text: [Select]
  1. <!Doctype html>
  2. <html>
  3.   <head>
  4.     <title>Audio</title>
  5.   </head>
  6.   <body>
  7.     <div style="margin:200px auto; text-align:center; font-weight:bold; font-size:24px;">
  8.     <script>
  9.       function play() {
  10.         var audio = document.getElementById("audio");
  11.         audio.play();
  12.       }
  13.     </script><div style="display:inline;">Pete is your SCREEN 0 Hero!</div>
  14.     <input style="font-size:24px;" type="button" value="PLAY" onclick="play()">
  15.     <audio id="audio" src="file:///C:\qb64\recording.wav"></audio>
  16.     </div>
  17.   </body>
  18. </html>

Pete

Title: Re: Recording Audio in QB64?!?!
Post by: SpriggsySpriggs on September 26, 2020, 11:38:33 pm
Damn, they were emoji. 🎵
Oh, haha!
Title: Re: Recording Audio in QB64?!?!
Post by: SpriggsySpriggs on September 26, 2020, 11:51:39 pm
Hey, pretty cool, Zach! I got by on javascript and html. Just change the path to wherever you keep your QB64 files, and, of course, the recording.wav needs to be in that folder. Just save the code below as whatever.html, in your QB64 folder, click it, and it will appear in your default browser. Click the play button to hear Zach's recording.

Hey, @Pete
I wasn't able to make it play in my browser. I'm not sure what's going on :( Maybe it's a browser issue.
Title: Re: Recording Audio in QB64?!?!
Post by: Pete on September 27, 2020, 12:11:37 am
Did you remember to change the line: src="file:///C:\qb64\recording.wav" to whatever folder path your recording.wav is in? If so, what browser? I'm using Firefox. I'll test Opera and Edge, now...

Pete
Title: Re: Recording Audio in QB64?!?!
Post by: SpriggsySpriggs on September 27, 2020, 12:27:29 am
Did you remember to change the line: src="file:///C:\qb64\recording.wav" to whatever folder path your recording.wav is in? If so, what browser? I'm using Firefox. I'll test Opera and Edge, now...

Yes, that was the first thing I did when I copied your snippet. I am using Edge Dev which is based on Chromium. I'll try it in Legacy Edge next or Chrome and see what happens.
Title: Re: Recording Audio in QB64?!?!
Post by: Pete on September 27, 2020, 12:36:40 am
64-bit Windows 10. Firefox, Opera, and Edge all worked. I don't have Chrome.

Pete
Title: Re: Recording Audio in QB64?!?!
Post by: SpriggsySpriggs on September 27, 2020, 12:39:49 am
Ah, of course it was something extremely simple. A space in my directory path. It needed to be substituted with %20, of course. Made it work right away. Neato.
Title: Re: Recording Audio in QB64?!?!
Post by: Pete on September 27, 2020, 12:59:19 am
Good. I agree, just a small problem. If it were a big one, you'd have to try %90 :D

Hey thanks for adding the sound API. Nice collection. I don't know if you have a windows persistency API, but you are welcome to test and add mine to the mix. https://www.qb64.org/forum/index.php?topic=1365.msg105588#msg105588

Pete
Title: Re: Recording Audio in QB64?!?!
Post by: SpriggsySpriggs on September 27, 2020, 01:03:55 am
Hey thanks for adding the sound API. Nice collection. I don't know if you have a windows persistency API, but you are welcome to test and add mine to the mix. https://www.qb64.org/forum/index.php?topic=1365.msg105588#msg105588

That's pretty neat that it is able to stay topmost! That's nice, especially in Windows 10 where I seem to constantly have an issue where sometimes the window goes to the background when my mouse moves away from it. I'll add it to my collection and credit you.
Title: Re: Recording Audio in QB64?!?!
Post by: Pete on September 27, 2020, 01:15:34 am
Please be sure to give a shout out to visionmercer, too. I got it working almost fully, except it would only work once. visionmercer pointed me to the SetWindowPos function, which I fiddle with a bit and got it to do the trick. I love that about forums; an abundance of resources and knowledge.

Pete
Title: Re: Recording Audio in QB64?!?!
Post by: SpriggsySpriggs on September 27, 2020, 01:17:15 am
Please be sure to give a shout out to visionmercer, too. I got it working almost fully, except it would only work once. visionmercer pointed me to the SetWindowPos function, which I fiddle with a bit and got it to do the trick.

Done. Right now I'm adding another bit of functionality to the sound API by allowing it to show a waveform using ffmpeg :)

  [ This attachment cannot be displayed inline in 'Print Page' view ]  
Title: Re: Recording Audio in QB64?!?!
Post by: FellippeHeitor on September 27, 2020, 01:21:58 am
Done. Right now I'm adding another bit of functionality to the sound API by allowing it to show a waveform using ffmpeg :)

  [ This attachment cannot be displayed inline in 'Print Page' view ]  

That's cool!
Title: Re: Recording Audio in QB64?!?!
Post by: Richard on September 27, 2020, 01:25:57 am
Any use?

Quote

DECLARE DYNAMIC LIBRARY "WINMM"
    FUNCTION mciSendStringA% (lpstrCommand AS STRING, lpstrReturnString AS STRING, BYVAL uReturnLength AS INTEGER, BYVAL hwndCallback AS INTEGER)
    ' mciSendStringA function plays media files and returns the following:
    ' 0 = command sucessful
    ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    ' lpstrCommand is the MCI command string (and optional flags) to send.
    ' lpstrReturnString is a string that holds any return information.
    ' uReturnLength is the length of the lpstrReturnString string passed.
    ' NOTE: If lpstrCommand given doesn't retun a value then lpstrReturnString
    '       can be empty and uReturnLength can be set to 0.
    ' hwndCallback contains a callback window handle (only if the Notify flag used in lpstrCommand)
    '====================================================================
    FUNCTION mciGetErrorStringA% (BYVAL dwError AS INTEGER, lpstrBuffer AS STRING, BYVAL uLength AS INTEGER)
    ' mciGetErrorStringA returns error info if the mciSendStringA failed.
    ' dwError is the return value from the mciSendString function.
    ' lpstrBuffer string holds the error information returned by the function.
    ' uLength is the length of the lpstrBuffer string buffer.
    '====================================================================
END DECLARE

DECLARE CUSTOMTYPE LIBRARY
    FUNCTION FindWindow& (BYVAL ClassName AS _OFFSET, WindowName$)
END DECLARE


handle& = _NEWIMAGE(800, 600, 256)
SCREEN handle&


_TITLE "QB64 Video"
hwnd& = FindWindow(0, "QB64 Video" + CHR$(0))

ReturnString$ = SPACE$(255)
ErrorString$ = SPACE$(255)
filename$ = "c:\DavPiano.mpg" '<========== video file to play

a% = mciSendStringA%("open " + filename$ + " style popup", ReturnString$, LEN(ReturnString$), 0)

IF a% THEN
    x% = mciGetErrorStringA%(a%, ErrorString$, LEN(ErrorString$))
    PRINT ErrorString$
    END
ELSE
    a2% = mciSendStringA%("window " + filename$ + " handle " + STR$(hwnd&), ReturnString$, LEN(ReturnString$), 0)
    b% = mciSendStringA%("play " + filename$, "", 0, 0)
    _SCREENMOVE _MIDDLE
    '=== Play video...
    DO: _LIMIT 30: LOOP UNTIL INKEY$ <> ""

    x% = mciSendStringA%("stop " + filename$, "", 0, 0)
    x% = mciSendStringA%("close " + filename$, "", 0, 0)
END IF
Title: Re: Recording Audio in QB64?!?!
Post by: SpriggsySpriggs on September 27, 2020, 01:26:54 am
@Richard I never have been able to get that to work with any video. I'm not sure what the requirements are for codecs but I've never seen it work. I'd love to have videos play in QB64 but no luck for me.
Title: Re: Recording Audio in QB64?!?!
Post by: Unseen Machine on September 27, 2020, 06:07:42 am
Oh i soooooo miss .net! We had sooooo much stuff that was lost to time! All i have now is pictures in my dropbox!

My version of mic input/playing/saving is attached but will only work with SDL .954 as the development team have yet to fix QB64 GL's ability to use linked libs. (I do hope it is gonna be fixed!)

If your looking for something to work on next, assimp or box2d would be nice (but with the affore said problems i doubt it'll be possible ( i havent figured out a waya yet!)).

Any how, nice work mr spriggs.

Unseen
Title: Re: Recording Audio in QB64?!?!
Post by: SpriggsySpriggs on September 27, 2020, 10:36:39 am
Thanks, @Unseen Machine !
The only issue with mciSendStringA is the quality. It is rather poor and has very little gain. You aren't able to change the input volume, either. I even tried recording desktop audio with it and it sounded horrible. I don't think there is even a way to adjust the sample rate. It's just the absolute bare necessity.
MWAHAHAHAHA that's fixed
Title: Re: Recording Audio in QB64?!?!
Post by: SpriggsySpriggs on September 27, 2020, 03:43:34 pm
@Pete @Unseen Machine I finally figured out how to make it record in stereo and in a rather good quality! Here is a recording sample from myself and my desktop audio along with the waveforms generated from ffmpeg:

  [ This attachment cannot be displayed inline in 'Print Page' view ]  
  [ This attachment cannot be displayed inline in 'Print Page' view ]  

 [ This attachment cannot be displayed inline in 'Print Page' view ]  
  [ This attachment cannot be displayed inline in 'Print Page' view ]  
Title: Re: Recording Audio in QB64?!?!
Post by: Unseen Machine on September 27, 2020, 06:10:01 pm
Well done, glad you got it sorted out. Can you access the individual samples? Also, thats not what i thought youd sound like!
Title: Re: Recording Audio in QB64?!?!
Post by: Pete on September 27, 2020, 06:42:30 pm
Yes, well done! And... Weirdly enough, that's exactly what I thought you would sound like.

Pete
Title: Re: Recording Audio in QB64?!?!
Post by: SpriggsySpriggs on September 27, 2020, 07:12:36 pm
Well done, glad you got it sorted out. Can you access the individual samples? Also, thats not what i thought youd sound like!

I don't know anything about accessing sound files, really. The waveform is a PNG that is created by ffmpeg when I pass it arguments. It would be nice to learn more about WAV files and all that.