|
| 1 | +# ============================================================================= |
| 2 | +# Module: audio.py |
| 3 | +# Contacts: Edward Li (drawdeil@gmail.com) |
| 4 | +# ============================================================================= |
| 5 | +"""___DESC___ |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +# ============================================================================= |
| 10 | +# IMPORTS |
| 11 | +# ============================================================================= |
| 12 | +import subprocess |
| 13 | + |
| 14 | +# ============================================================================= |
| 15 | +# CLASSES |
| 16 | +# ============================================================================= |
| 17 | +class Audio(object): |
| 18 | + |
| 19 | + # ========================================================================= |
| 20 | + def __init__(self): |
| 21 | + |
| 22 | + self._devices = list() |
| 23 | + self._device = Device() |
| 24 | + |
| 25 | + args = 'pacmd list-sources' |
| 26 | + popen = subprocess.Popen(args, shell=True, |
| 27 | + stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 28 | + |
| 29 | + device = Device() |
| 30 | + for line in popen.stdout: |
| 31 | + line = line.strip() |
| 32 | + |
| 33 | + if line.startswith('name: '): |
| 34 | + device.name = line.split(':', 1)[1].strip() |
| 35 | + |
| 36 | + elif line.startswith('device.api = '): |
| 37 | + device.api = line.split('=', 1)[1].strip().strip('"') |
| 38 | + |
| 39 | + elif line.startswith('alsa.subdevice = '): |
| 40 | + device.subdevice = line.split('=', 1)[1].strip().strip('"') |
| 41 | + |
| 42 | + elif line.startswith('alsa.card = '): |
| 43 | + device.card = line.split('=', 1)[1].strip().strip('"') |
| 44 | + |
| 45 | + elif line.startswith('device.description = '): |
| 46 | + device.description = line.split('=', 1)[1].strip().strip('"') |
| 47 | + |
| 48 | + if device.name.startswith('<alsa_input.') and \ |
| 49 | + device.api and device.subdevice and device.card and \ |
| 50 | + device.description: |
| 51 | + |
| 52 | + if device not in self._devices: |
| 53 | + self._devices.append(device) |
| 54 | + |
| 55 | + device = Device() |
| 56 | + |
| 57 | + # ========================================================================= |
| 58 | + def deviceSet(self, index): |
| 59 | + |
| 60 | + self._device = self.devices[index] |
| 61 | + |
| 62 | + # ========================================================================= |
| 63 | + @property |
| 64 | + def devices(self): |
| 65 | + |
| 66 | + return self._devices |
| 67 | + |
| 68 | + # ========================================================================= |
| 69 | + @property |
| 70 | + def device(self): |
| 71 | + |
| 72 | + return self._device |
| 73 | + |
| 74 | +# ============================================================================= |
| 75 | +class Device(object): |
| 76 | + |
| 77 | + # ========================================================================= |
| 78 | + def __init__(self, name=None, api=None, subdevice=None, card=None, |
| 79 | + description=None): |
| 80 | + |
| 81 | + self._name = name |
| 82 | + self._api = api |
| 83 | + self._subdevice = subdevice |
| 84 | + self._card = card |
| 85 | + self._description = description |
| 86 | + |
| 87 | + # ========================================================================= |
| 88 | + @property |
| 89 | + def source(self): |
| 90 | + |
| 91 | + return 'plughw:{c},{s}'.format(c=self.card, s=self.subdevice) |
| 92 | + |
| 93 | + # ========================================================================= |
| 94 | + @property |
| 95 | + def name(self): |
| 96 | + |
| 97 | + return self._name |
| 98 | + |
| 99 | + # ========================================================================= |
| 100 | + @name.setter |
| 101 | + def name(self, value): |
| 102 | + |
| 103 | + self._name = value |
| 104 | + |
| 105 | + # ========================================================================= |
| 106 | + @property |
| 107 | + def api(self): |
| 108 | + |
| 109 | + return self._api |
| 110 | + |
| 111 | + # ========================================================================= |
| 112 | + @api.setter |
| 113 | + def api(self, value): |
| 114 | + |
| 115 | + self._api = value |
| 116 | + |
| 117 | + # ========================================================================= |
| 118 | + @property |
| 119 | + def subdevice(self): |
| 120 | + |
| 121 | + return self._subdevice |
| 122 | + |
| 123 | + # ========================================================================= |
| 124 | + @subdevice.setter |
| 125 | + def subdevice(self, value): |
| 126 | + |
| 127 | + self._subdevice = value |
| 128 | + |
| 129 | + # ========================================================================= |
| 130 | + @property |
| 131 | + def card(self): |
| 132 | + |
| 133 | + return self._card |
| 134 | + |
| 135 | + # ========================================================================= |
| 136 | + @card.setter |
| 137 | + def card(self, value): |
| 138 | + |
| 139 | + self._card = value |
| 140 | + |
| 141 | + # ========================================================================= |
| 142 | + @property |
| 143 | + def description(self): |
| 144 | + |
| 145 | + return self._description |
| 146 | + |
| 147 | + # ========================================================================= |
| 148 | + @description.setter |
| 149 | + def description(self, value): |
| 150 | + |
| 151 | + self._description = value |
| 152 | + |
| 153 | +# ============================================================================= |
| 154 | + |
0 commit comments