Play sound file on word input Topic is solved
Play sound file on word input
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
Re: Play sound file on word input
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.
Re: Play sound file on word input
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
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 11 times
-
- Screenshot_3.png (62.46 KiB) Viewed 599 times
-
- playsound.rar
- (82.29 KiB) Downloaded 13 times
Re: Play sound file on word input
i have this code
i need it to read from text file the words it has to play
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
Re: Play sound file on word input
here's a fixed sound file source
- Attachments
-
- playsound.rar
- (64.27 KiB) Downloaded 11 times
Re: Play sound file on word input
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
https://www.autohotkey.com/boards/viewtopic.php?p=345201#p345201
Re: Play sound file on word input
i have this script
how can i make it work
Code: Select all
Loop, read, textforplaysound.txt
{
CurrentLineSplit := StrSplit(A_LoopReadLine, ",")
Var := CurrentLineSplit[4]
}
SoundPlay, %Var% ".mp3"
Re: Play sound file on word input
Here is a general idea from the other script.
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.
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
Re: Play sound file on word input
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
https://www.autohotkey.com/boards/viewtopic.php?f=6&t=77&p=489#p489
Re: Play sound file on word input
how to change or choose a voice?gregster wrote: ↑29 Jul 2021, 08:40I 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
Re: Play sound file on word input
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.
Obviously letting the german voice speak english, or vice versa, is not very useful
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.
Obviously letting the german voice speak english, or vice versa, is not very useful
Re: Play sound file on word input
i have this code
instead of text 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
Code: Select all
ISpVoice := ComObjCreate("SAPI.SpVoice")
NumberOfVoices:=ISpVoice.GetVoices().Count
Loop, % NumberOfVoices
{
ISpVoice.Speak("Welcome")
break
}
ExitApp
Code: Select all
ISpVoice.Speak("Welcome")
and if possible make the text saved as a .mp3
Re: Play sound file on word input Topic is solved
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.
Of course, in a next step, you could encode that wav-file to mp3 or ogg, with some commandline tool.
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()
Re: Play sound file on word input
thank you