Play sound file on word input Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
crypter
Posts: 90
Joined: 15 Dec 2020, 09:57

Play sound file on word input

Post by crypter » 28 Jul 2021, 19:10

I need a script to play a mp3 file based on a word input or a word from a text file. When the word is "hello" in the text file the script plays the mp3 "hello.mp3" when there is a period like "." the script stops playing mp3 for 1 second. It basically plays the mp3 based on words matching the mp3 file name

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Play sound file on word input

Post by mikeyww » 28 Jul 2021, 19:18

If you provide specific examples of your files, and then also explain what should happen based on those files, step by step, I think it will help readers to understand this and provide you with feedback.

crypter
Posts: 90
Joined: 15 Dec 2020, 09:57

Re: Play sound file on word input

Post by crypter » 28 Jul 2021, 19:52

i have one mp3 for each word from the sentence "AutoHotkey is a free open source scripting language." and i need to select the text file with this sentence and play every mp3 that matches the words in their file name

like sound play:

AutoHotkey.mp3
is.mp3
a.mp3
free.mp3
open.mp3
source.mp3
scripting.mp3
language.mp3
Attachments
textforplaysound.txt
(52 Bytes) Downloaded 10 times
Screenshot_3.png
Screenshot_3.png (62.46 KiB) Viewed 596 times
playsound.rar
(82.29 KiB) Downloaded 12 times

crypter
Posts: 90
Joined: 15 Dec 2020, 09:57

Re: Play sound file on word input

Post by crypter » 28 Jul 2021, 20:08

i have this code

i need it to read from text file the words it has to play

Code: Select all

Sounds2PlayFolder := "C:\Users\lapto\OneDrive\Desktop\playsound" ; Location of *.mp3 files to play.

PlayList := []
Loop, Files, %Sounds2PlayFolder%\*.mp3 , R
   PlayList.Push(A_LoopFileFullPath)
SoundID := 5
SplitPath, % PlayList[SoundID], OutFileName

Gui, Add, Button, xm ym w100, Play
Gui, Add, Button, xm+100 ym w100 +Disabled, Stop
Gui, Add, Text, xm w200 h2 0x7
Gui, Add, Button, xm ym+35 w100 vPause +Disabled, Pause
Gui, Add, Button, xm+100 ym+35 w100 +Disabled, Resume
Gui, Add, Text, xm w200 h2 0x7
Gui, Add, Button, xm ym+70 w100 +Disabled, +10 seconds
Gui, Add, Button, xm+100 ym+70 w100 +Disabled, -10 seconds
Gui, Add, Text, xm w200 h2 0x7
Gui, Add, Button, xm ym+105 w100 +Disabled, Previous
Gui, Add, Button, xm+100 ym+105 w100 +Disabled, Next
Gui, Add, Text, xm w200 vTime, Playing Time :
Gui, Add, Progress, w200 h10 cGreen vTimeProgress, 200
Gui, Add, Edit, w200 +ReadOnly -vScroll R1 vCurrentName
Gui, Show

Gui, Add, ActiveX, vAV, WMPLayer.OCX
AV.Settings.setMode("loop" ,true)
CurrSound := PlayList[SoundID]
CM := AV.newMedia(CurrSound)
SoundLen := CM.getItemInfo("duration")

Loop
{
   Sleep, 1000
   GuiControl,, Time, % "Playing Time : " Round(AV.controls.currentPosition, 0) " s / " Round(SoundLen, 0) " s"
   GuiControl,, TimeProgress, % Round(AV.controls.currentPosition, 0) / Round(SoundLen, 0) * 100
   GuiControl,, CurrentName, % OutFileName
}
Return

ButtonPlay:
   AV.Url := CurrSound
   If !ErrorLevel
   {
      GuiControl, Disable, Play
      GuiControl, Enable, Pause
      GuiControl, Enable, Stop
      GuiControl, Enable, +10 seconds
      GuiControl, Enable, -10 seconds
      GuiControl, Enable, Previous
      GuiControl, Enable, Next
   }
Return

ButtonStop:
   AV.controls.stop
   If !ErrorLevel
   {
      GuiControl, Enable, Play
      GuiControl, Disable, Pause
      GuiControl, Disable, Stop
      GuiControl, Disable, Resume
      GuiControl, Disable, +10 seconds
      GuiControl, Disable, -10 seconds
      GuiControl, Disable, Previous
      GuiControl, Disable, Next
   }
Return

ButtonPause:
   AV.controls.pause
   If !ErrorLevel
   {
      GuiControl, Enable, Resume
      GuiControl, Disable, Pause
   }
Return

ButtonResume:
   AV.controls.play
   If !ErrorLevel
   {
      GuiControl, Enable, Pause
      GuiControl, Disable, Resume
   }
Return

Button+10seconds:
   t := AV.controls.currentPosition + 10
   AV.controls.currentPosition := t
Return

Button-10seconds:
   t := AV.controls.currentPosition - 10
   If (t < 0)
      t := 0
   AV.controls.currentPosition := t
Return

ButtonPrevious:
   If (SoundID > 1)
   {
      SoundID--
      SplitPath, % PlayList[SoundID], OutFileName
      CurrSound := PlayList[SoundID]
      CM := AV.newMedia(CurrSound)
      SoundLen := CM.getItemInfo("duration")
      AV.Url := CurrSound
   }
Return

ButtonNext:
   If (SoundID < PlayList.MaxIndex())
   {
      SoundID++
      SplitPath, % PlayList[SoundID], OutFileName
      CurrSound := PlayList[SoundID]
      CM := AV.newMedia(CurrSound)
      SoundLen := CM.getItemInfo("duration")
      AV.Url := CurrSound
   }
Return

GuiClose:
ExitApp

crypter
Posts: 90
Joined: 15 Dec 2020, 09:57

Re: Play sound file on word input

Post by crypter » 28 Jul 2021, 20:19

here's a fixed sound file source
Attachments
playsound.rar
(64.27 KiB) Downloaded 10 times

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Play sound file on word input

Post by mikeyww » 28 Jul 2021, 20:21

You could use StrSplit to parse your phrase. You could then use the following sort of play routine from @Smile_.

https://www.autohotkey.com/boards/viewtopic.php?p=345201#p345201

crypter
Posts: 90
Joined: 15 Dec 2020, 09:57

Re: Play sound file on word input

Post by crypter » 29 Jul 2021, 05:31

i have this script

Code: Select all

Loop, read, textforplaysound.txt
{
    CurrentLineSplit := StrSplit(A_LoopReadLine, ",")
    Var := CurrentLineSplit[4]
}
SoundPlay, %Var% ".mp3"
how can i make it work

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Play sound file on word input

Post by mikeyww » 29 Jul 2021, 08:14

Here is a general idea from the other script.

Code: Select all

mp3 = ....mp3         ; Full path to MP3 file
Gui, Add, ActiveX, vAV, WMPLayer.OCX
Gosub, F3
F3::AV.Url := mp3     ; Play from start
F4::AV.controls.stop  ; Stop
F5::AV.controls.pause ; Pause
F6::AV.controls.play  ; Resume
You'll need to adjust, and also figure out a way to continue to the next audio file. The other script includes code to get the audio duration as well as the time progress, so this may help you in your revisions.

gregster
Posts: 8918
Joined: 30 Sep 2013, 06:48

Re: Play sound file on word input

Post by gregster » 29 Jul 2021, 08:40

I don't know if this is relevant for your use case, but there is also the SAPI COM object. It can speak sentences with the installed voices. Just in case...
https://www.autohotkey.com/boards/viewtopic.php?f=6&t=77&p=489#p489

crypter
Posts: 90
Joined: 15 Dec 2020, 09:57

Re: Play sound file on word input

Post by crypter » 29 Jul 2021, 11:26

gregster wrote:
29 Jul 2021, 08:40
I don't know if this is relevant for your use case, but there is also the SAPI COM object. It can speak sentences with the installed voices. Just in case...
https://www.autohotkey.com/boards/viewtopic.php?f=6&t=77&p=489#p489
how to change or choose a voice?

gregster
Posts: 8918
Joined: 30 Sep 2013, 06:48

Re: Play sound file on word input

Post by gregster » 29 Jul 2021, 12:31

An example is in the second code box in ahk7's post here: https://www.autohotkey.com/boards/viewtopic.php?f=76&t=71024&p=307267#p307267

There are generally not many Microsoft voices to choose from, but you might be able to install more. I have currently two US-English (one male, one female) and a female german voice installed. They are not great, but for a quick sound output of random text, it's better than nothing. 8-)
Obviously letting the german voice speak english, or vice versa, is not very useful :D

crypter
Posts: 90
Joined: 15 Dec 2020, 09:57

Re: Play sound file on word input

Post by crypter » 29 Jul 2021, 16:18

i have this code

Code: Select all

ISpVoice := ComObjCreate("SAPI.SpVoice")

NumberOfVoices:=ISpVoice.GetVoices().Count

Loop, % NumberOfVoices
	{
	 ISpVoice.Speak("Welcome")
	 break
	}

ExitApp
instead of text

Code: Select all

ISpVoice.Speak("Welcome")
i want the text to load from a text file (.txt) in the script's folder

and if possible make the text saved as a .mp3

gregster
Posts: 8918
Joined: 30 Sep 2013, 06:48

Re: Play sound file on word input  Topic is solved

Post by gregster » 30 Jul 2021, 08:36

You can just load a text from a text file into a variable instead of using a hardcoded text.

Also, you can stream the voice into a wav-file (mp3 is not supported, afaik) - but there are different qualities available, so that file size can be reduced.

Code: Select all

voice := ComObjCreate("SAPI.SpVoice")
stream := ComObjCreate("SAPI.SpFileStream")
file := "example.txt"
FileRead, input, % file
output := "VoiceToFile.wav"

; Stream.Format.Type := 16		; for other formats see https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms720595(v=vs.85)
stream.open(output, 3)		; overwrites existing output file

; Set voice output to the stream and speak
voice.AudioOutputStream := stream
voice.Speak(input)

; close stream
stream.close()
Of course, in a next step, you could encode that wav-file to mp3 or ogg, with some commandline tool.

crypter
Posts: 90
Joined: 15 Dec 2020, 09:57

Re: Play sound file on word input

Post by crypter » 30 Jul 2021, 10:44

thank you

Post Reply

Return to “Ask for Help (v1)”