Skip to content

Instantly share code, notes, and snippets.

@Antonytm
Created July 23, 2024 15:10
Show Gist options
  • Save Antonytm/61fcf57526f2446ddc92d6558306447b to your computer and use it in GitHub Desktop.
Save Antonytm/61fcf57526f2446ddc92d6558306447b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Audacity script to prepare long audio books or podcasts for listening in the swimming pool.
# The script will amplify the audio, because the volume usually is too low for the underwater speakers.
# It will also add DTMF tones every XX minutes, so the listener knows how much time has passed.
# It is very convenient to know how much time has passed, especially in Ukraine, when there is electricity outage. When you don't see the clock.
# The script will also add labels every XX minutes, so you can export multiple files for easier navigation between parts.
# Before running the script, make sure Audacity is running first and that mod-script-pipe is enabled
# Requires Python 2.7 or later. Python 3 is strongly recommended.
import os
import sys
import json
if sys.platform == 'win32':
print("pipe-test.py, running on windows")
TONAME = '\\\\.\\pipe\\ToSrvPipe'
FROMNAME = '\\\\.\\pipe\\FromSrvPipe'
EOL = '\r\n\0'
else:
print("pipe-test.py, running on linux or mac")
TONAME = '/tmp/audacity_script_pipe.to.' + str(os.getuid())
FROMNAME = '/tmp/audacity_script_pipe.from.' + str(os.getuid())
EOL = '\n'
print("Write to \"" + TONAME +"\"")
if not os.path.exists(TONAME):
print(" ..does not exist. Ensure Audacity is running with mod-script-pipe.")
sys.exit()
print("Read from \"" + FROMNAME +"\"")
if not os.path.exists(FROMNAME):
print(" ..does not exist. Ensure Audacity is running with mod-script-pipe.")
sys.exit()
print("-- Both pipes exist. Good.")
TOFILE = open(TONAME, 'w')
print("-- File to write to has been opened")
FROMFILE = open(FROMNAME, 'rt')
print("-- File to read from has now been opened too\r\n")
def send_command(command):
"""Send a single command."""
print("Send: >>> \n"+command)
TOFILE.write(command + EOL)
TOFILE.flush()
def get_response():
"""Return the command response."""
result = ''
line = ''
while True:
result += line
line = FROMFILE.readline()
if line == '\n' and len(result) > 0:
break
return result
def do_command(command):
"""Send one command, and return the response."""
send_command(command)
response = get_response()
print("Rcvd: <<< \n" + response)
return response
def quick_test():
"""Example list of commands."""
do_command('Help: Command=Help')
do_command('Help: Command="GetInfo"')
quick_test()
# settings
trackDuration = 2700
toneDuration = 1
do_command('SelectAll:')
do_command('Amplify: Ratio=2.238 AllowClipping=0')
info = do_command('GetInfo: Type=Tracks')
infoJson = json.loads(info.replace("BatchCommand finished: OK", ""))
duration = infoJson[0].get("end") - infoJson[0].get("start")
print(duration)
currentTime = 0
while currentTime < duration:
do_command('Select: Start=' + str(currentTime) + ' End=' + str(currentTime + toneDuration))
do_command('DtmfTones:')
currentTime += trackDuration
do_command('SelectAll:')
do_command('RegularIntervalLabels: mode=Interval interval=' +str(trackDuration))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment