-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudacityPipe.py
More file actions
142 lines (112 loc) · 3.89 KB
/
audacityPipe.py
File metadata and controls
142 lines (112 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#! /usr/bin/python3
# -*- coding: utf-8 -*-
"""Test Import / Export and recording.
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 sys
import time
import json
# Platform specific file name and file path.
# PATH is the location of files to be imported / exported.
# #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'
else:
print("recording-test.py, running on linux or mac")
PIPE_TO_AUDACITY = '/tmp/audacity_script_pipe.to.' + str(os.getuid())
PIPE_FROM_AUDACITY = '/tmp/audacity_script_pipe.from.' + str(os.getuid())
EOL = '\n'
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()
print("Read from \"" + PIPE_FROM_AUDACITY +"\"")
if not os.path.exists(PIPE_FROM_AUDACITY):
print(""" ..does not exist.
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 send_command(command):
"""Send a command to Audacity."""
print("Send: >>> "+command)
TOPIPE.write(command + EOL)
TOPIPE.flush()
def get_response():
"""Get response from Audacity."""
line = FROMPIPE.readline()
result = ""
while True:
result += line
line = FROMPIPE.readline()
# print(f"Line read: [{line}]")
if line == '\n':
return result
def do_command(command):
"""Do the command. Return the response."""
send_command(command)
# time.sleep(0.1) # may be required on slow machines
response = get_response()
print("Rcvd: <<< " + response)
return response
def do_command_async(command):
"""Do the command. Return the response."""
send_command(command)
def get_response_async():
"""Get response from Audacity."""
line = FROMPIPE.readline()
print(line + 'at line consideration')
result = ""
if line == "":
return False
else:
while True:
result += line
line = FROMPIPE.readline()
# print(f"Line read: [{line}]")
if line == '\n':
return result
def quick_test():
"""Quick test to ensure pipe is working."""
print(do_command('Record1stChoice'))
if __name__ == '__main__':
quick_test()