Saving recorded sound file

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
dalkin
Posts: 176
Joined: Wed Jul 04, 2007 2:32 am
Location: Blackheath, Australia
Contact:

Saving recorded sound file

Post by dalkin » Wed Oct 16, 2019 7:49 am

I'm building a Mac standalone that follows a splash stack (Loader) and presents users with an option to clone a stack that holds various items of entered text and saves the field entries. It works as expected (and works well might I add) But on each cloned card, I want to present the user with an option to record up to 5 snippets of audio. Each snippet should ideally be named by the user and saved at a location chosen by the user, then the Play button should play the audio. So the problem I can't solve is giving each recording the name chosen by the user and using the Play button to find it and play it.

This is as far as I've got so any advice would be welcomed. Unfortunately, there doesn't seem to be any definitive guide for recording audio in standalones, I have been told that if I want to record audio on a Windows standalone I'll be looking at using this:

##
In Indy and above, you can use the Advanced Camera Controls to do audio
recording on Windows. Something like:

cameraControlSet tCameraControlName, "videoDevice", empty
cameraControlDo tCameraControlName, "startRecording",
specialFolderPath("documents") & "/video.mp4"
##


Record button:

on mouseUp
set the recordFormat to "wav"
mergMicrophoneStartRecording audio1
set the label of button "Record1" to "Recording"
end mouseUp

Stop Recording button:

on mouseUp
mergMicrophoneStopRecording
set the label of button "Record1" to "Record1"
end mouseUp

Play button

on mouseUp
play audio.wav
set the label of button "Play1" to "Playing"
wait until the sound is done
set the label of button "Play1" to "Play1"
end mouseUp
Attachments
Screen Shot 2019-10-16 at 5.28.25 pm.png
If we're treading on thin ice, well you might as well dance.

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Saving recorded sound file

Post by Klaus » Wed Oct 16, 2019 1:52 pm

Hi dalkin,

some hints:
In your own interest get used to put QUOTES around (file- and object) names:

Code: Select all

...
mergMicrophoneStartRecording "audio1"
...
play "audio.wav"
...
To be sure you are allowed to write to file use:

Code: Select all

...
ask "Please name the recording:"
if it = empty then
   exit to top
end if
put it into tUserDefinedName
## Now save this in the below mentioned "table"...
mergMicrophoneStartRecording (specialfolderpath("documents") & "/audio1.wav")
...
So the problem I can't solve is giving each recording the name chosen by the user and using the Play button to find it and play it.
Use a global variable or custom property and manage the recordings in a little "table" (= tab and CR delimited text) like this:
recording1 TAB user defined name 1 TAB filename of recording
recording2 TAB ...
recording3 TAB...
Get the picture?
Since you numbered your button names it will be easy to know which line of the "table" to access!

You can later save that info to a simple textfile and load it again when the app starts up the next time.


Best

Klaus

dalkin
Posts: 176
Joined: Wed Jul 04, 2007 2:32 am
Location: Blackheath, Australia
Contact:

Re: Saving recorded sound file

Post by dalkin » Thu Oct 17, 2019 7:47 am

I'm sorry Klaus but I don't understand where a table comes into it. The 3 buttons are now showing as follows but nothing is recorded and nothing is saved:

Record button:

on mouseUp
set the recordFormat to "wav"
ask "Please name the recording:"
if it = empty then
exit to top
end if
put it into tUserDefinedName
## Now save this in the below mentioned "table"...
mergMicrophoneStartRecording (specialfolderpath("desktop") & "/audio.wav")
set the label of button "Record1" to "Recording"
end mouseUp

Stop Recording button:
on mouseUp
mergMicrophoneStopRecording
set the label of button "Record1" to "Record1"
end mouseUp

Play button

on mouseUp
play audio.wav
set the label of button "Play1" to "Playing"
wait until the sound is done
set the label of button "Play1" to "Play1"
end mouseUp
If we're treading on thin ice, well you might as well dance.

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Saving recorded sound file

Post by Klaus » Thu Oct 17, 2019 10:11 am

The 3 buttons are now showing as follows but nothing is recorded and nothing is saved:
I thought your only problem is to assign the user given name to the appropriate recording?
I do not yet see where the user defined name comes into play however, where do you want to display them?

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Saving recorded sound file

Post by Klaus » Thu Oct 17, 2019 11:32 am

Or use another approach with custom properties like this:
Button "record1"

Code: Select all

on mouseUp
   set the recordFormat to "wav"
   ask "Please name the recording:"
   if it = empty then
      exit to top
   end if
   put it into tUserDefinedName
   put specialfolderpath("desktop") & "/audio1.wav" into tFile
   
   ## Now set two custom properties fo the PLAY1 button, so the button knows what to play when clicked!
   set the cAudioFile of btn "play1" to tFile
   set the cUserDefinedName of btn "play1" to tUserDefinedName
   mergMicrophoneStartRecording tFile
   set the label of button "Record1" to "Recording"
end mouseUp
Stop Recording button:

Code: Select all

on mouseUp
   mergMicrophoneStopRecording
   set the label of button "Record1" to "Record1"
end mouseUp
Play button

Code: Select all

on mouseUp
   put the cAudioFile of me into tFile
   put the cUserDefinedName of me into tUserDefinedName
   if tFile = EMPTY then
   ## Nothing recorded yet!
      exit to top
   end if
   answer "Now playing:" && tUserDefinedName
   play tFile
   set the label of button "Play1" to "Playing"
   wait until the sound is done
   set the label of button "Play1" to "Play1"
end mouseUp
If you want to name the recorded file with the user defined name use this in the RECORD buttons:

Code: Select all

...
put it into tUserDefinedName
put specialfolderpath("desktop") & "/" & tUserDefinedName & ".wav" into tFile
...

dalkin
Posts: 176
Joined: Wed Jul 04, 2007 2:32 am
Location: Blackheath, Australia
Contact:

Re: Saving recorded sound file

Post by dalkin » Thu Oct 17, 2019 9:35 pm

That is so cool, Klaus. I would never have got there. Just 2 questions, is there a way for the user to define what the specialfolderpath is and change the generic "audio1" to the user's choice of name? Could I also suggest that you bring this conversation to the attention of the LC team because cross-platform audio recording (which was due to be a major part of LC9) is still part of the roadmap and seems to be scheduled for future development.

If you ever make it to Sydney, please let me know and I will fill you with beer.
Screen Shot 2019-10-18 at 7.34.52 am.png
Screen Shot 2019-10-18 at 7.34.52 am.png (77.72 KiB) Viewed 4599 times
If we're treading on thin ice, well you might as well dance.

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Saving recorded sound file

Post by Klaus » Thu Oct 17, 2019 9:55 pm

Hi dalkin,
is there a way for the user to define what the specialfolderpath is?
sure, check if the user already defined a folder for the recordings and ask him if not:

Code: Select all

on mouseUp
   put the cUserDefinedRecordingFolder of this stack into tUserFolder
   
   ## User did not define a folder yet:
   if tUserFolder = EMPTY then
    answer folder "Select a folder for the recordings:"
    if it = EMPTY then
       exit to top
    end if
    ## We store the folder also in a custom property:
    set the cUserDefinedRecordingFolder of this stack to IT
    
    ## We need this in this script:
    put it into tUserFolder
  end if
   
   set the recordFormat to "wav"
   ask "Please name the recording:"
   if it = empty then
      exit to top
   end if
   put it into tUserDefinedName
   put tUserFolder & "/" & tUserDefinedName & ".wav" into tFile
  ...
Could I also suggest that you bring this conversation to the attention of the LC team...
Although it might not look like this, but I am only a user of LC and not related nor related by marriage to the LC company. :D

I doubt however that I will ever make if to Sydney in this life, but thanks anyway.
And I literally never FRET, see my bass on mybass page: https://major-k.de/bass/index.html 8)


Best

Klaus

P.S.
You can of course use shorter names for your custom properties! :)

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”