Confused about SoundPlay and Wave volume Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
doubledave22
Posts: 343
Joined: 08 Jun 2019, 17:36

Re: Confused about SoundPlay and Wave volume

Post by doubledave22 » 05 Jan 2022, 15:47

I had that idea, thanks for putting it together. Unfortunately the WMPlayer comobject (or perhaps the while loops) is conflicting with my ocr code and causing async errors. I will be trying out bass.ahk for now and see how it goes. If you can take a look it's not overly complicated. The trouble is I have to set the volume *after* the sound begins and you can hear a small blip of full volume before it kicks in. I'm so close I just cant seem to get the hstream before the initial BASS_Play() call.

doubledave22
Posts: 343
Joined: 08 Jun 2019, 17:36

Re: Confused about SoundPlay and Wave volume

Post by doubledave22 » 05 Jan 2022, 16:02

Ok had to update the library to add a Volume arg to BASS_Play() and BASS_StreamFileToChannel()

Code: Select all


BASS_Play(File="",Volume:=""){  ;plays a file or restarts it when paused and no file is specified (returns hStream on success, otherwise -1)         
  Return BASS_StreamFileToChannel("Play",File,Volume)
}
BASS_StreamFileToChannel(Modus,File="",Volume:=""){
  static
  If (File AND !FileExist(File)){     ;check if file exists when specified
    MsgBox, 48, BASS error!,
      (Ltrim
        File does not exist:
        %File%
      )
    Return -1    
  }
  If (Modus = "Play" And File){      ;play file from beginning
  	If !(hStream := DllCall("BASS\BASS_StreamCreateFile", UInt,FromMem:=0
                           , UInt,&File, UInt64,Offset:=0, UInt64,Length:=0
                           , UInt,(A_IsUnicode ? 0x80000000 : 0x40000))){
      ErrorLevel := BASS_ErrorGetCode()
      MsgBox, 48, BASS error!,
        (Ltrim
          Failed to create a stream from file:
          %File%
          Error: %ErrorLevel%
        )
      Return -1
    }
    if (Volume) 
      BASS_SetSliding(hStream,2,Volume,0)
    If !DllCall("BASS\BASS_ChannelPlay", UInt,hStream, Int,Restart:=1){
      ErrorLevel := BASS_ErrorGetCode()
      MsgBox, 48, BASS error!,
        (Ltrim
          Failed to play stream from file:
          %File%
          Error: %ErrorLevel%
        )
      Return -1      
    }
  }Else If (Modus = "Play" And !File And hStream){   ;restart playback (when paused)
    If !DllCall("BASS\BASS_ChannelPlay", UInt,hStream, Int,Restart:=0){
      ErrorLevel := BASS_ErrorGetCode()
      MsgBox, 48, BASS error!,
        (Ltrim
          Failed to restart stream from file:
          %File%
          Error: %ErrorLevel%
        )
      Return -1      
    }
  }Else If (Modus = "Stop" And hStream){             ;stop playback
    If BASS_IsPlaying(hStream)
      If !DllCall("BASS\BASS_ChannelStop", UInt,hStream){
        ErrorLevel := BASS_ErrorGetCode()
        MsgBox, 48, BASS error!,
          (Ltrim
            Failed to stop stream from file:
            %File%
            Error: %ErrorLevel%
          )
        Return -1
      }
    hStream =                                        ;clear hStream
  }Else If (Modus = "Pause" And hStream){            ;toogle pause of playback
    local IsPlaying
    IsPlaying := BASS_IsPlaying(hStream)               ;get status
    If (IsPlaying = 3)                                    ;stream is paused
      hStream := BASS_Play()                                 ;restart playback
    Else If (IsPlaying = 1){                              ;stream is playing
      If !DllCall("BASS\BASS_ChannelPause", UInt,hStream){   ;pause playback
        ErrorLevel := BASS_ErrorGetCode()
        MsgBox, 48, BASS error!,
          (Ltrim
            Failed to pause stream from file:
            %File%
            Error: %ErrorLevel%
          )
        Return -1
      }
    }
  }
  Return hStream
}

So now can do:

Code: Select all

hStream := BASS_Play(mediaFilePath,.5) ; play file at 50% volume

User avatar
SteveMylo
Posts: 233
Joined: 22 Jun 2021, 00:50
Location: Australia
Contact:

Re: Confused about SoundPlay and Wave volume

Post by SteveMylo » 23 Feb 2022, 18:00

Does anyone know how to reset back to master volume after a simple SoundPlay has run?
Edit** I figured it out. See Spoiler Below. Just used SoundGet, Master & SoundSet, Master
Spoiler

So... My simple SoundPlay code that works is as follows:

Code: Select all

z::
File := "C:\Users\Mylo\Downloads\New Recording.m4a"
SoundPlay(File,,8) 

SoundPlay(File, Wait = 0, VolumePercent = 100) {
   SoundSet, %VolumePercent%
   SoundPlay, %File%, %Wait%
}
return
Here is a code I have for SoundBeep that resets to what the volume was before the beep sound which is exactly what I want but for SoundPlay.
I tried extracting parts but no luck

Code: Select all

z::
SoundBeep(700, 33)
return


soundbeep(frequency:=523, volume:=50) {
		global master_volume, volume_unset
		static _ := onexit("on_exit_sb")
		if not (volume_unset) {
			soundget master_volume
			volume_unset := true
		} soundset % volume
		soundbeep % frequency
		settimer, return_master_volume, -150
	}
		return_master_volume() {
			global master_volume, volume_unset
			soundset master_volume
			volume_unset := false
		}
			on_exit_sb() {
				global master_volume, volume_unset
				if (volume_unset) {
					sleep 150
					SoundSet % master_volume
				}
			}

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Confused about SoundPlay and Wave volume

Post by teadrinker » 24 Feb 2022, 09:03

Why not like this:

Code: Select all

soundbeep(900, 10)

soundbeep(frequency:=523, volume:=50) {
   SoundGet, master_volume
   SoundSet, volume
   SoundBeep, frequency
   SoundSet, master_volume
}

User avatar
SteveMylo
Posts: 233
Joined: 22 Jun 2021, 00:50
Location: Australia
Contact:

Re: Confused about SoundPlay and Wave volume

Post by SteveMylo » 24 Feb 2022, 16:14

teadrinker wrote:
24 Feb 2022, 09:03
Why not like this:
Yes that's easier, I pretty much used that in my ' Spoiler ' .

Also, your code needs a little sleep in there, cause the 1st time the script is run, it happens too quickly and the beep is at master volume :-)
But thankyou very much

Skrell
Posts: 302
Joined: 23 Jan 2014, 12:05

Re: Confused about SoundPlay and Wave volume

Post by Skrell » 20 Jun 2022, 10:35

How do you include the bass.dll in your exe and then find it upon running the compiled exe?


Skrell
Posts: 302
Joined: 23 Jan 2014, 12:05

Re: Confused about SoundPlay and Wave volume

Post by Skrell » 20 Jun 2022, 12:39

So I used this and it failed to find the dll upon executing the exe. Note that my executable is NOT on the C: Maybe that has something to do with it?

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Confused about SoundPlay and Wave volume

Post by teadrinker » 20 Jun 2022, 12:57

Please show your code.

Skrell
Posts: 302
Joined: 23 Jan 2014, 12:05

Re: Confused about SoundPlay and Wave volume

Post by Skrell » 20 Jun 2022, 13:28

image.png
image.png (38.99 KiB) Viewed 1558 times

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance force
#MaxHotkeysPerInterval 200
#Include %A_ScriptDir%\bass.ahk

SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

FileInstall, bass.dll, %A_ScriptDir%\bass.dll, 1
If ErrorLevel == 0
    msgbox, "Error installing bass.dll " %ErrorLevel%
Else
    BASS_Load("I:\Programs\bass.dll")

~$LButton::
  PlaySong(snap.wav, 1.0)

PlaySong(File,Volume=0.5)
{
	static stream
	If stream
		DllCall("Bass.dll\BASS_ChannelStop", UInt, stream)
	If (Volume<0) or (Volume>1)
               Volume:=0.5
	Stream := DllCall("BASS\BASS_StreamCreateFile",UInt,0,UInt,&File,UInt64,0,UInt64,0,UInt,0x80000000)
	DllCall("Bass.dll\BASS_ChannelSetAttribute",UInt,stream,UInt,2,Float,Volume)
	DllCall("bass.dll\BASS_ChannelPlay",UInt,Stream,Int,0)
}

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Confused about SoundPlay and Wave volume

Post by teadrinker » 20 Jun 2022, 16:00

Skrell wrote:

Code: Select all

FileInstall, bass.dll, %A_ScriptDir%\bass.dll, 1
If ErrorLevel == 0
    msgbox, "Error installing bass.dll " %ErrorLevel%
Else
    BASS_Load("I:\Programs\bass.dll")
At least this part is wrong. If ErrorLevel = 0 it means that all is ok. See docs.

Skrell
Posts: 302
Joined: 23 Jan 2014, 12:05

Re: Confused about SoundPlay and Wave volume

Post by Skrell » 24 Jun 2022, 19:11

teadrinker wrote:
20 Jun 2022, 16:00
Skrell wrote:

Code: Select all

FileInstall, bass.dll, %A_ScriptDir%\bass.dll, 1
If ErrorLevel == 0
    msgbox, "Error installing bass.dll " %ErrorLevel%
Else
    BASS_Load("I:\Programs\bass.dll")
At least this part is wrong. If ErrorLevel = 0 it means that all is ok. See docs.
Where can I find all these include files?
;include effects & handlers
#Include *i basstags.ahk
#Include *i bassfx.ahk
#Include *i bassvfx.ahk
#Include *i bassenc.ahk
#Include *i bassmix.ahk
#Include *i bassvis.ahk
#Include *i bassvst.ahk
#Include *i basswadsp.ahk
#Include *i bassvideo.ahk

;include format plugins
#Include *i basscd.ahk
#Include *i bassmidi.ahk
#Include *i bassflac.ahk
#Include *i basswma.ahk
#Include *i basswv.ahk
#Include *i bassaac.ahk
#Include *i bassape.ahk
#Include *i bassmpc.ahk
#Include *i bassac3.ahk
#Include *i bassalac.ahk
#Include *i bassspx.ahk
#Include *i basstta.ahk
#Include *i bassofr.ahk

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Confused about SoundPlay and Wave volume

Post by teadrinker » 25 Jun 2022, 05:18

No idea, I've never used them.

Skrell
Posts: 302
Joined: 23 Jan 2014, 12:05

Re: Confused about SoundPlay and Wave volume

Post by Skrell » 25 Jun 2022, 10:19

teadrinker wrote:
25 Jun 2022, 05:18
No idea, I've never used them.
No matter what I do I can't get the dll to load. Neither the 32bit nor 64bit (different errors in both cases). I wonder if the dll has changed since this code was written and hence the dllcall() functions need to change in the bass.ahk ? Can you please send me the *.dll file(s) you use with your ahk code that you said loads no problem?

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Confused about SoundPlay and Wave volume

Post by teadrinker » 25 Jun 2022, 10:49

Attached.
Attachments
bass.dll.zip
(412.67 KiB) Downloaded 27 times

Skrell
Posts: 302
Joined: 23 Jan 2014, 12:05

Re: Confused about SoundPlay and Wave volume

Post by Skrell » 25 Jun 2022, 12:12

teadrinker wrote:
25 Jun 2022, 10:49
Attached.
It worked!!! THANK YOU THANK YOU!! You made my weekend!!

User avatar
SteveMylo
Posts: 233
Joined: 22 Jun 2021, 00:50
Location: Australia
Contact:

Re: Confused about SoundPlay and Wave volume

Post by SteveMylo » 25 Jun 2022, 17:07

Skrell wrote:
teadrinker wrote:
25 Jun 2022, 10:49
Attached.
It worked!!! THANK YOU THANK YOU!! You made my weekend!!

[youtube] https://youtu.be/vZCLA7boX1g[/youtube]

Post Reply

Return to “Ask for Help (v1)”