aboutsummaryrefslogtreecommitdiffstats
path: root/pyagl/services/audiomixer.py
blob: 23d382e582a742bcf8f8345435bf1ac9d7f35242 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# Copyright (C) 2020 Konsulko Group
# Author: Edi Feschiyan
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from pyagl.services.base 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, control='Master Playback', value=None):
        if value is not None:
            return await self.request('volume', {'control': control, 'value': value})
        else:
            return await self.request('volume', {'control': control})

    async def mute(self, control='Master Playback', value=None):
        return await self.request('mute', {'control': control, '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 = await ams.afbresponse()
        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 = await ams.afbresponse()
        print(r)

    if args.getvolume:
        resp = await ams.volume()
        print(f'Requesting volume with id {resp}')
        r = await ams.afbresponse()
        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 = await ams.afbresponse()
        print(r)

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

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

    if args.unsubscribe:
        for event in args.unsubscribe:
            msgid = await ams.unsubscribe(event)
            print(f'Unsubscribing from {event} with id {msgid}')
            r = await ams.afbresponse()
            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())