Skip to content

Commit

Permalink
Merge pull request #2 from arushsharma24/main
Browse files Browse the repository at this point in the history
Linux : Virtual device creation and routing microphone audio via a python script
  • Loading branch information
xprilion authored Jan 5, 2021
2 parents 3cb4052 + fc2fe7f commit 9d1d034
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
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'.

#
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)

0 comments on commit 9d1d034

Please sign in to comment.