summaryrefslogtreecommitdiffstats
path: root/audiomixer.py
blob: 3d61c10214dcc855e49601380df71a77ac05c3b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from aglbaseservice import AGLBaseService, AFBResponse
import asyncio
import os

verbs = ['subscribe', 'unsubscribe', 'list_controls', 'volume', 'mute']
events = ['volume_changed', 'mute_changed', 'controls_changed']


class AudioMixerService(AGLBaseService):
    service = 'agl-service-audiomixer'
    parser = AGLBaseService.getparser()
    parser.add_argument('--list_controls', default=True, help='Request list of controls', action='store_true')
    parser.add_argument('--getmute', help='Get mute state', action='store_true')
    parser.add_argument('--setmute', help='Set mute state', type=int, choices=[0, 1])
    parser.add_argument('--setvolume', help='Set volume level', type=float)
    parser.add_argument('--getvolume', help='Get volume level', action='store_true')


    def __init__(self, ip, port=None, service='agl-service-audiomixer'):
        super().__init__(api='audiomixer', ip=ip, port=port, service=service)

    async def subscribe(self, event='volume_changed'):  # audio mixer uses 'event' instead 'value',
        return await self.request('subscribe', {'event': event})

    async def unsubscribe(self, event='volume_changed'):
        return await self.request('unsubscribe', {'event': event})

    async def list_controls(self):
        return await self.request('list_controls')

    async def volume(self, value=None):
        if value is not None:
            return await self.request('volume', {'control': 'Master', 'value': value})
        else:
            return await self.request('volume', {'control': 'Master'})

    async def mute(self, value=None):
        return await self.request('mute', {'control': 'Master', 'value': value})


async def main():
    args = AudioMixerService.parser.parse_args()
    ams = await AudioMixerService(ip=args.ipaddr, port=args.port)

    if args.list_controls:
        resp = await ams.list_controls()
        print(f'Requesting list_controls with id {resp}')
        r = AFBResponse(await ams.response())
        print(r)

    if args.setvolume is not None:
        resp = await ams.volume(args.setvolume)
        print(f'Setting volume to {args.setvolume} with id {resp}')
        r = AFBResponse(await ams.response())
        print(r)

    if args.getvolume:
        resp = await ams.volume()
        print(f'Requesting volume with id {resp}')
        r = AFBResponse(await ams.response())
        print(r)

    if args.setmute is not None:
        resp = await ams.mute(args.setmute)
        print(f'Setting mute to {args.setmute} with id {resp}')
        r = AFBResponse(await ams.response())
        print(r)

    if args.getmute:
        resp = await ams.mute()
        r = AFBResponse(await ams.response())
        print(r)

    if args.subscribe:
        for event in args.subscribe:
            id = await ams.subscribe(event)
            print(f'Subscribing to {event} with id {id}')
            r = AFBResponse(await ams.response())
            print(r)

    if args.unsubscribe:
        for event in args.unsubscribe:
            id = await ams.unsubscribe(event)
            print(f'Unsubscribing from {event} with id {id}')
            r = AFBResponse(await ams.response())
            print(r)

    if args.listener:
        async for response in ams.listener():
            print(response)

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())