mirror of
https://github.com/cookiengineer/audacity
synced 2026-01-09 06:07:06 +01:00
Fix Python recording test
and update for Python 3
This commit is contained in:
@@ -1,92 +1,158 @@
|
|||||||
# recording-test.py
|
#! /usr/bin/python3
|
||||||
# loads testfile-in.wav, plays it, recording at the same time, and then
|
# -*- coding: utf-8 -*-
|
||||||
# exports the result as testfile-out.wav.
|
|
||||||
# Suitable for rinse and repeat with different input files.
|
|
||||||
# Assumes testfile-in.wav is one minute of audio.
|
|
||||||
|
|
||||||
# Make sure Audacity is running first and that mod-script-pipe is enabled
|
"""Test Import / Export and recording.
|
||||||
# before running this script.
|
|
||||||
|
recording-test.py loads a WAV file, plays it, recording at the same time until
|
||||||
|
the end of the track, and then exports the recording as a WAV with "-out"
|
||||||
|
appended to the file name.
|
||||||
|
|
||||||
|
To run the test without input prompts, set valid values for
|
||||||
|
PATH and INFILE.
|
||||||
|
|
||||||
|
User supplied variables
|
||||||
|
-------
|
||||||
|
PATH: Path to the folder containing the input test file. Also used for exporting the result.
|
||||||
|
INFILE: Name of the input WAV file.
|
||||||
|
|
||||||
|
With a little modification, can be suitable for rinse and repeat with different
|
||||||
|
input files.
|
||||||
|
|
||||||
|
Make sure Audacity is running and that mod-script-pipe is enabled
|
||||||
|
before running this script.
|
||||||
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
import json
|
||||||
|
|
||||||
if( sys.platform == 'win32' ):
|
|
||||||
print( "recording-test.py, running on windows" )
|
# Platform specific file name and file path.
|
||||||
toname = '\\\\.\\pipe\\ToSrvPipe'
|
# PATH is the location of files to be imported / exported.
|
||||||
fromname = '\\\\.\\pipe\\FromSrvPipe'
|
|
||||||
|
#PATH = './'
|
||||||
|
PATH = ""
|
||||||
|
while not os.path.isdir(PATH):
|
||||||
|
PATH = os.path.realpath(input('Path to test folder: '))
|
||||||
|
if not os.path.isdir(PATH):
|
||||||
|
print('Invalid path. Try again.')
|
||||||
|
print('Test folder: ' + PATH)
|
||||||
|
|
||||||
|
|
||||||
|
#INFILE = "testfile.wav"
|
||||||
|
INFILE = ""
|
||||||
|
while not os.path.isfile(os.path.join(PATH, INFILE)):
|
||||||
|
INFILE = input('Name of input WAV file: ')
|
||||||
|
# Ensure we have the .wav extension.
|
||||||
|
INFILE = os.path.splitext(INFILE)[0] + '.wav'
|
||||||
|
if not os.path.isfile(os.path.join(PATH, INFILE)):
|
||||||
|
print(f"{os.path.join(PATH, INFILE)} not found. Try again.")
|
||||||
|
else:
|
||||||
|
print(f"Input file: {os.path.join(PATH, INFILE)}")
|
||||||
|
# Remove file extension.
|
||||||
|
INFILE = os.path.splitext(INFILE)[0]
|
||||||
|
|
||||||
|
|
||||||
|
# Platform specific constants
|
||||||
|
if sys.platform == 'win32':
|
||||||
|
print("recording-test.py, running on windows")
|
||||||
|
PIPE_TO_AUDACITY = '\\\\.\\pipe\\ToSrvPipe'
|
||||||
|
PIPE_FROM_AUDACITY = '\\\\.\\pipe\\FromSrvPipe'
|
||||||
EOL = '\r\n\0'
|
EOL = '\r\n\0'
|
||||||
else:
|
else:
|
||||||
print( "recording-test.py, running on linux or mac" )
|
print("recording-test.py, running on linux or mac")
|
||||||
toname = '/tmp/audacity_script_pipe.to.' + str(os.getuid())
|
PIPE_TO_AUDACITY = '/tmp/audacity_script_pipe.to.' + str(os.getuid())
|
||||||
fromname = '/tmp/audacity_script_pipe.from.' + str(os.getuid())
|
PIPE_FROM_AUDACITY = '/tmp/audacity_script_pipe.from.' + str(os.getuid())
|
||||||
EOL = '\n'
|
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." )
|
print("Write to \"" + PIPE_TO_AUDACITY +"\"")
|
||||||
|
if not os.path.exists(PIPE_TO_AUDACITY):
|
||||||
|
print(""" ..does not exist.
|
||||||
|
Ensure Audacity is running with mod-script-pipe.""")
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
tofile = open( toname, 'wt+' )
|
print("Read from \"" + PIPE_FROM_AUDACITY +"\"")
|
||||||
print( "-- File to write to has been opened" )
|
if not os.path.exists(PIPE_FROM_AUDACITY):
|
||||||
fromfile = open( fromname, 'rt')
|
print(""" ..does not exist.
|
||||||
print( "-- File to read from has now been opened too\r\n" )
|
Ensure Audacity is running with mod-script-pipe.""")
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
print("-- Both pipes exist. Good.")
|
||||||
|
|
||||||
|
TOPIPE = open(PIPE_TO_AUDACITY, 'w')
|
||||||
|
print("-- File to write to has been opened")
|
||||||
|
FROMPIPE = open(PIPE_FROM_AUDACITY, 'r')
|
||||||
|
print("-- File to read from has now been opened too\r\n")
|
||||||
|
|
||||||
|
|
||||||
def sendCommand( command ) :
|
def send_command(command):
|
||||||
print( "Send: >>> "+command )
|
"""Send a command to Audacity."""
|
||||||
tofile.write( command + EOL )
|
print("Send: >>> "+command)
|
||||||
tofile.flush()
|
TOPIPE.write(command + EOL)
|
||||||
|
TOPIPE.flush()
|
||||||
|
|
||||||
def getResponse() :
|
|
||||||
result = ''
|
def get_response():
|
||||||
line = ''
|
"""Get response from Audacity."""
|
||||||
while line != '\n' :
|
line = FROMPIPE.readline()
|
||||||
|
result = ""
|
||||||
|
while True:
|
||||||
result += line
|
result += line
|
||||||
line = fromfile.readline()
|
line = FROMPIPE.readline()
|
||||||
#print(" I read line:["+line+"]")
|
# print(f"Line read: [{line}]")
|
||||||
return result
|
if line == '\n':
|
||||||
|
return result
|
||||||
|
|
||||||
def doCommand( command ) :
|
|
||||||
sendCommand( command )
|
def do_command(command):
|
||||||
response = getResponse()
|
"""Do the command. Return the response."""
|
||||||
print( "Rcvd: <<< " + response )
|
send_command(command)
|
||||||
|
# time.sleep(0.1) # may be required on slow machines
|
||||||
|
response = get_response()
|
||||||
|
print("Rcvd: <<< " + response)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def do( command ) :
|
|
||||||
doCommand( command )
|
|
||||||
|
|
||||||
def quickTest() :
|
def play_record(filename):
|
||||||
do( 'Help: CommandName=Help' )
|
"""Import track and record to new track.
|
||||||
|
Note that a stop command is not required as playback will stop at end of selection.
|
||||||
|
"""
|
||||||
|
do_command(f"Import2: Filename={os.path.join(PATH, filename + '.wav')}")
|
||||||
|
do_command("Select: Track=0")
|
||||||
|
do_command("SelTrackStartToEnd")
|
||||||
|
# Our imported file has one clip. Find the length of it.
|
||||||
|
clipsinfo = do_command("GetInfo: Type=Clips")
|
||||||
|
clipsinfo = clipsinfo[:clipsinfo.rfind('BatchCommand finished: OK')]
|
||||||
|
clips = json.loads(clipsinfo)
|
||||||
|
duration = clips[0]['end'] - clips[0]['start']
|
||||||
|
# Now we can start recording.
|
||||||
|
do_command("Record2ndChoice")
|
||||||
|
print('Sleeping until recording is complete...')
|
||||||
|
time.sleep(duration + 0.1)
|
||||||
|
|
||||||
# You will need to modify the paths in this function and the next.
|
|
||||||
def playRecord( name ) :
|
|
||||||
do("Import: Filename='C:\\Users\\James Crook\\Documents\\Audacity\\" + name + ".wav'")
|
|
||||||
do("Select: Track=0")
|
|
||||||
do("SelectTrackStartToEnd")
|
|
||||||
do("MenuCommand: CommandName=Record2ndChoice")
|
|
||||||
|
|
||||||
def exportIt( name ):
|
def export(filename):
|
||||||
do("Select: Track=1")
|
"""Export the new track, and deleted both tracks."""
|
||||||
do("SelectTrackStartToEnd")
|
do_command("Select: Track=1 mode=Set")
|
||||||
do("Export: Filename='C:\\Users\\James Crook\\Documents\\Audacity\\" + name + ".wav' Mode=Selection Channels=1.0")
|
do_command("SelTrackStartToEnd")
|
||||||
do("Select: Track=0")
|
do_command(f"Export2: Filename={os.path.join(PATH, filename)} NumChannels=1.0")
|
||||||
do("SelectTrackStartToEnd")
|
do_command("SelectAll")
|
||||||
do("MenuCommand: CommandName=RemoveTracks")
|
do_command("RemoveTracks")
|
||||||
|
|
||||||
def doOneFile( name ):
|
|
||||||
playRecord(name + "-in")
|
|
||||||
time.sleep( 65 )
|
|
||||||
exportIt(name + "-out")
|
|
||||||
|
|
||||||
|
|
||||||
quickTest()
|
def do_one_file(name):
|
||||||
doOneFile("testfile")
|
"""Run test with one input file only."""
|
||||||
|
play_record(name)
|
||||||
|
export(name + "-out.wav")
|
||||||
|
|
||||||
|
|
||||||
|
def quick_test():
|
||||||
|
"""Quick test to ensure pipe is working."""
|
||||||
|
do_command('Help: CommandName=Help')
|
||||||
|
|
||||||
|
|
||||||
|
quick_test()
|
||||||
|
# Run the test with "testfile.wav" in the specified PATH.
|
||||||
|
do_one_file(INFILE)
|
||||||
|
|||||||
Reference in New Issue
Block a user