Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux : Virtual device creation and routing microphone audio via a python script #2

Merged
merged 10 commits into from
Jan 5, 2021
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#Temporary work files
linux/initial
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
9 changes: 9 additions & 0 deletions linux/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### Step 1:
Run the script with the command:
`$ python3 dynopii.py`

### Step 2: (only for first time use)
In the "Playback" tab of the pavucontrol window that opens, and make sure that the playback device for the running "ALSA plug-in[aplay]: ALSA Playback _on_" is 'dynopii'.

#
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove

To close this, stop the script using `Ctrl-C` and it will also delete the virtual device module and stop the playback.
49 changes: 49 additions & 0 deletions linux/dynopii.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import subprocess
import audioop

try:

print('#' * 80)
print('Initiating dynopii virtual audio cable ... ')
loadcmd = ['pacmd', 'load-module', 'module-null-sink', 'sink_name=dynopii_sink', 'sink_properties=device.description=dynopii']
subprocess.Popen(loadcmd)
subprocess.Popen('pavucontrol')

cmd0 = ['arecord', '-f', 'cd', '-']
p0 = subprocess.Popen(cmd0, stdout=subprocess.PIPE)

'''
This stdout of p0 is basically the data we get from arecord, and the data that we can manipulate on the fly and then
feed to the stdin of the next process p1.
If you will work on this, you'll have to replace the stdin=p0.stdout of the following process p1 with your manipulated data.
'''

cmd1 = ['aplay', '-f', 'cd', '-']
p1 = subprocess.Popen(cmd1, stdin=p0.stdout)
print('')
print('To quit this program and close your virtual device, press Ctrl-C')
print('')
print('#' * 80)
input()

except EOFError:
print('EOF Error')

except KeyboardInterrupt:
print()
print('Closing the virtual audio cable ....')
print('Unloading null-sinks ...')
print('Stopping playback ...')

uncmd0 = ['pactl', 'list', 'short', 'modules']
uncmd1 = ['grep', 'sink_name=dynopii']
uncmd2 = [ 'cut', '-f1' ]
uncmd3 = [ 'xargs', '-L1', 'pactl', 'unload-module' ]

unp0 = subprocess.Popen(uncmd0, stdout=subprocess.PIPE)
unp1 = subprocess.Popen(uncmd1, stdin=unp0.stdout, stdout=subprocess.PIPE)
unp2 = subprocess.Popen(uncmd2, stdin=unp1.stdout, stdout=subprocess.PIPE)
unp3 = subprocess.Popen(uncmd3, stdin=unp2.stdout)

killcmd = ['pkill', 'arecord'] #kill arecord (hence killing ALSA playback since aplay simultaneously stops receiving input via the pipe)
closefin = subprocess.Popen(killcmd)