Sound Player - Volume Control / Random Sounds / Multiple Sounds

Post your working scripts, libraries and tools.
User avatar
WarlordAkamu67
Posts: 220
Joined: 21 Mar 2023, 06:52

Sound Player - Volume Control / Random Sounds / Multiple Sounds

Post by WarlordAkamu67 » 29 Jan 2024, 14:32

Easily create databases of sounds to play from your chosen directories (folders). Currently only captures (.mp3) and (.wav) file types.

Can play up to 25 sounds at once, by default.

Options include volume, waiting for sound to end, and interruptibility.

Play random sounds from any or all databases created. 1 database, or an array of databases. If omitted, all databases are used using the built in array "allFilesDatabase".

The main function, playSound(), is standalone for easy porting.

Using other programs such as VoiceMeter, and the Advance Sound Setting of windows, it is possible to create a soundboard.

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
#MaxThreads 255
#MaxThreadsPerHotkey 255

; Preload sound objects. Functions include construction of sound objects if needed.
completedLoading := 0
ToolTip("Loading...")
soundPlayer := []
stopLevel := []
Loop (25) {
  soundPlayer.Push(ComObject("WMPlayer.OCX.7"))
  stopLevel.Push(0)
}

; ------------------------- ;

; Add sound directories. This section should be altered to include the folders of sound you wish to play.
; For better randomness, an array (allFilesDatabase) of all files in databases is created. This may cause load times with many files.

myOnlyDatabase := createDatabase("Z:\01 EMPLOYEE FILES\00 Me\Audio\Sounds")
; secondUncreatedDatabase := createDatabase(["C:\1st Directory 2nd Database", "F:\2nd Directory 2nd Database"])
; ect.

; ------------------------- ;

completedLoading := 1
ToolTip()
return

; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;

#HotIf (completedLoading)
~1::playRandomSound(, 50) ;                                                                        	        Random sound from all databases played at 50% volume.
~2::playSound("Z:\01 EMPLOYEE FILES\00 Me\Audio\Sounds\auto_0.wav") ;                  Play a sound. 100% volume, do not wait, can be inturrupted. Only a path to the file is required.
~3::playRandomSound(myOnlyDatabase, 75, 0, 1) ;                                                    Play a random sound from a specific database of sounds at 75% volume. Do not wait (waitFor = 0), can not be inturrupted (unStopable = 1).
~4::stopSounds() ;                                                                                                     Stops all inturruptable sounds.

; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;
; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;
; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;
; Functions
; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;
; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;
; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;

createDatabase(search_Location, search_Mode := "F") {
  global allFilesDatabase
  global soundDatabases
  if (!IsSet(soundDatabases)) {
    allFilesDatabase := []
    soundDatabases := []
}
  soundDatabases.Push([])
  if (HasProp(search_Location, "length")) {
    for (index, location_to_Search in search_Location) {
      location_to_Search := location_to_Search "\*"
      Loop Files (location_to_Search), (search_Mode) {
        if (A_LoopFileExt == "wav" || A_LoopFileExt == "mp3") {
          soundDatabases[soundDatabases.length].Push(A_LoopFileFullPath)
          toAdd := 1
          for (index, theFileName in allFilesDatabase) {
            if (theFileName == A_LoopFileFullPath) {
              toAdd := 0
              break
}
}
          toAdd ? allFilesDatabase.Push(A_LoopFileFullPath) : ""
}
}
}
} else {
    search_Location := search_Location "\*"
    Loop Files (search_Location), (search_Mode) {
      if (A_LoopFileExt == "wav" || A_LoopFileExt == "mp3") {
        soundDatabases[soundDatabases.length].Push(A_LoopFileFullPath)
        toAdd := 1
        for (index, theFileName in allFilesDatabase) {
          if (theFileName == A_LoopFileFullPath) {
            toAdd := 0
            break
}
}
        toAdd ? allFilesDatabase.Push(A_LoopFileFullPath) : ""
}
}
}
  return(soundDatabases[soundDatabases.length])
}

; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;

playRandomSound(useDatabase := allFilesDatabase, volume := 100, waitFor := 0, unStopable := 0) {
  global allFilesDatabase
  if (Type(useDatabase) == "Array" && useDatabase.length) {
    if (Type(useDatabase[1]) == "Array" && useDatabase[1].length) {
      useDatabase := useDatabase[Random(1, useDatabase.length)]
}
  playSound(useDatabase[Random(1, useDatabase.length)], volume, waitFor, unStopable)
}
  return
}

; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;

playSound(mediaFilePath, volume := 100, waitFor := 0, unStopable := 0) {
  if (waitFor) {
    theSoundPlayers(mediaFilePath, volume, waitFor, unStopable)
} else {
    SetTimer () => theSoundPlayers(mediaFilePath, volume, waitFor, unStopable), -1
}
  return
; ------------------------- ;
  theSoundPlayers(mediaFilePath, volume := 100, waitFor := 0, unStopable := 0) {
    global soundPlayer
    global stopLevel
    if (!IsSet(soundPlayer)) {
      soundPlayer := []
      stopLevel := []
      Loop (25) {
        soundPlayer.Push(ComObject("WMPlayer.OCX.7"))
        stopLevel.Push(0)
}
}
    playerIndex := 1
    while (soundPlayer[playerIndex].playState = 3 || soundPlayer[playerIndex].playState = 9) {
      playerIndex := playerIndex + 1
      if (playerIndex > soundPlayer.length) {
        return
}
}
    stopLevel[playerIndex] := unStopable
    selectedPlayer := soundPlayer[playerIndex]
    selectedPlayer.Settings.Volume := volume
    selectedPlayer.url := mediaFilePath
    if (waitFor) {
      while (selectedPlayer.playState != 1 && selectedPlayer.url != "") {
        Sleep(50)
}
      selectedPlayer.url := ""
}
  return
}
}

; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;

stopSounds() {
  global soundPlayer
  global stopLevel
  try {
    for (index, thePlayer in soundPlayer) {
      stopLevel[index] ? "" : thePlayer.url := ""
}
}
  return
}

; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;

Return to “Scripts and Functions (v2)”