AutoHotkey スレッド part7

このエントリーをはてなブックマークに追加
13名無しさん@お腹いっぱい。
■ サウンドデバイスの一覧を改行区切りで得る関数
waveOut/waveIn , midiOut/midiIn に対応

複数の入出力デバイスがなければ全く意味がない。

SoundDevice_GetDeviceList(media="wave", isOut=true, requireId=false) {
 static cbxoc:=256,delim:="`n"
 uDeviceID:=0
 func := InStr(media, "wave") ? "wave" : InStr(media, "midi") ? "midi" : ""
 if (!func)
  return
 func .= isOut ? "Out" : "In"
 VarSetCapacity(xoc, cbxoc, 0x00) ; Xoc (WAVEOUTCAPS / MIDIOUTCAPS)
 Loop {
  result := DllCall("winmm.dll\" func "GetDevCapsA", UInt,uDeviceID, Int,&xoc, UInt,cbxoc, Int)
  if (result == 2) ; MMSYSERR_BADDEVICEID
   break
  if (result == 0) { ; MMSYSERR_NOERROR
   devName:=""
   list .= StructGetString(xoc, 8) ; WAVEOUTCAPS と MIDIOUTCAPSのデバイス名称までは構造が同じ
    . (requireId ? ("`t" uDeviceID) : "")
    . delim
  }
  uDeviceID++
 }
 StringTrimRight, list, list, % StrLen(delim)
 return list
}
14名無しさん@お腹いっぱい。:2009/01/02(金) 17:27:03 ID:dpIHoV0B0
■ WaveOutを指定のデバイスIDで変更する
>>13 と組み合わせて使う。

SoundDevice_SetWaveDeviceId(id) {
 static WAVE_MAPPER:=-1, MMSYSERR_NOERROR:=0
 dwPreferedID:=0, dwUsePreferedOnly:=0
 if (MMSYSERR_NOERROR == DllCall("winmm.dll\waveOutMessage", UInt,WAVE_MAPPER, UInt,0x2015, UInt,&dwPreferedID, UInt,&dwUsePreferedOnly, Int)) {
  ret := DllCall("winmm.dll\waveOutMessage", UInt,WAVE_MAPPER, UInt,0x2016, Uint,id, dwUsePreferedOnly, Int)
  return true
 }
 return false
}



15名無しさん@お腹いっぱい。:2009/01/02(金) 17:31:07 ID:dpIHoV0B0
■ 現在選択されているWave再生デバイス名を取得する
SoundDevice_GetDeviceName() {
 RegRead, Playback, HKEY_CURRENT_USER, Software\Microsoft\Multimedia\Sound Mapper, Playback
 return Playback
}
■ 名前でWave再生デバイスを指定する (>>13-14 と組み合わせで使う)
SoundDevice_SetDeviceName(deviceName) {
 deviceList := SoundDevice_GetDeviceList("wave", true, true)
 Loop, PARSE, deviceList, `n
 {
  StringSplit, data, A_LoopField, %A_Tab%
  if (data1 == deviceName)
   return SoundDevice_SetWaveDeviceId(data2)
 }
}
■ 再生デバイスのトグルをする。上のものと組み合わせて使う。
 戻り値に変更後のデバイス名を返す
SoundDevice_ToggleDevice() {
 currentDeviceName := SoundDevice_GetDeviceName()
 deviceList := SoundDevice_GetDeviceList()
 StringSplit, device, deviceList, `n
 deviceCount:=device0, currentId:=0, nextId:=1
 Loop, %deviceCount%
  if (device%A_Index% == currentDeviceName)
   currentId := A_Index
  else if (currentId && A_Index == (currentId + 1))
   nextId := A_Index
 SoundDevice_SetDeviceName(device%nextId%)
 return device%nextId%
}