Skip to content

Commit

Permalink
Merge 0233b39 into 6082cb0
Browse files Browse the repository at this point in the history
  • Loading branch information
spyoungtech committed Mar 30, 2019
2 parents 6082cb0 + 0233b39 commit 931d03a
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 2 deletions.
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -126,6 +126,20 @@ ahk.pixel_get_color(100, 100) # get color of pixel located at coords (100, 100)
ahk.pixel_search('0x9d6346') # get coords of first pixel with specified color
```

## Sound

```python
from ahk import AHK
ahk = AHK()

ahk.sound_play('C:\\path\\to\\sound.wav') # play an audio file
ahk.sound_beep(frequency=440, duration=1000) # play a beep
ahk.get_volume(device_number=1) # get volume of a device
ahk.set_volume(50, device_number=1) # set volume of a device
ahk.sound_get(device_number=1, component_type='MASTER', control_type='VOLUME') # get sound device property
ahk.sound_set(50, device_number=1, component_type='MASTER', control_type='VOLUME') # set sound device property
```

## non-blocking modes

For some functions, you can also opt for a non-blocking interface, so you can do other stuff while AHK scripts run.
Expand Down
4 changes: 2 additions & 2 deletions ahk/autohotkey.py
Expand Up @@ -4,9 +4,9 @@
from ahk.script import ScriptEngine
from ahk.screen import ScreenMixin
from ahk.keyboard import KeyboardMixin
from ahk.sound import SoundMixin


class AHK(WindowMixin, MouseMixin, KeyboardMixin, ScreenMixin):
class AHK(WindowMixin, MouseMixin, KeyboardMixin, ScreenMixin, SoundMixin):
pass


Expand Down
84 changes: 84 additions & 0 deletions ahk/sound.py
@@ -0,0 +1,84 @@
from ahk.script import ScriptEngine


class SoundMixin(ScriptEngine):
def sound_beep(self, frequency=523, duration=150):
"""
REF: https://autohotkey.com/docs/commands/SoundBeep.htm
:param frequency: number between 37 and 32767
:param duration: how long in milliseconds to play the beep
:return: None
"""

script = self.render_template('sound/beep.ahk', frequency=frequency, duration=duration)
self.run_script(script)

def sound_play(self, filename, blocking=True):
"""
REF: https://autohotkey.com/docs/commands/SoundPlay.htm
:param filename:
:param blocking:
:param wait:
:return:
"""

script = self.render_template('sound/play.ahk', filename=filename, wait=1, blocking=blocking)
self.run_script(script, blocking=blocking)

def sound_get(self, device_number=1, component_type='MASTER', control_type='VOLUME'):
"""
REF: https://autohotkey.com/docs/commands/SoundGet.htm
:param device_number:
:param component_type:
:param control_type:
:return:
"""

script = self.render_template('sound/sound_get.ahk')
return self.run_script(script)

def get_volume(self, device_number=1):
"""
REF: https://autohotkey.com/docs/commands/SoundGetWaveVolume.htm
:param device_number:
:return:
"""
script = self.render_template('sound/get_volume.ahk', device_number=device_number)
result = self.run_script(script)
return result

def sound_set(self, value, device_number=1, component_type='MASTER', control_type='VOLUME'):
"""
REF: https://autohotkey.com/docs/commands/SoundSet.htm
:param value:
:param device_number:
:param component_type:
:param control_type:
:return:
"""

script = self.render_template('sound/sound_set.ahk', value=value,
device_number=device_number,
component_type=component_type,
control_type=control_type)
self.run_script(script)

def set_volume(self, value, device_number=1):
"""
REF: https://autohotkey.com/docs/commands/SoundSetWaveVolume.htm
:param value: percent volume to set volume to
:param device_number:
:return:
"""

script = self.render_template('sound/set_volume.ahk', value=value, device_number=device_number)
self.run_script(script)
4 changes: 4 additions & 0 deletions ahk/templates/sound/beep.ahk
@@ -0,0 +1,4 @@
{% extends "base.ahk" %}
{% block body %}
SoundBeep, {{ frequency }}, {{ duration }}
{% endblock body %}
5 changes: 5 additions & 0 deletions ahk/templates/sound/get_volume.ahk
@@ -0,0 +1,5 @@
{% extends "base.ahk" %}
{% block body %}
SoundGetWaveVolume, retval, {{ device_number }}
FileAppend, %retval%, *
{% endblock body %}
4 changes: 4 additions & 0 deletions ahk/templates/sound/play.ahk
@@ -0,0 +1,4 @@
{% extends "base.ahk" %}
{% block body %}
SoundPlay, {{ filename }}{% if wait %}, {{ wait }}{% endif %}
{% endblock body %}
4 changes: 4 additions & 0 deletions ahk/templates/sound/set_volume.ahk
@@ -0,0 +1,4 @@
{% extends "base.ahk" %}
{% block body %}
SoundSetWaveVolume, {{ value }}, {{ device_number }}
{% endblock body %}
5 changes: 5 additions & 0 deletions ahk/templates/sound/sound_get.ahk
@@ -0,0 +1,5 @@
{% extends "base.ahk" %}
{% block body %}
SoundGet, retval , {{ component_type }}, {{ control_type }}, {{ device_number }}
FileAppend, %retval%, *
{% endblock body %}
5 changes: 5 additions & 0 deletions ahk/templates/sound/sound_set.ahk
@@ -0,0 +1,5 @@
{% extends "base.ahk" %}
{% block body %}
SoundSet, {{ value }}, {{ component_type }}, {{ control_type }}, {{ device_number }}
FileAppend, %retval%, *
{% endblock body %}

0 comments on commit 931d03a

Please sign in to comment.