Remotely open and save files in Audacity

This commit is contained in:
Keith Edmunds 2023-12-08 19:57:25 +00:00
parent 540846223b
commit 83a817234d
3 changed files with 32 additions and 20 deletions

View File

@ -27,7 +27,7 @@ class AudacityManager:
Manage comms with Audacity
"""
def __init__(self, path: str) -> None:
def __init__(self) -> None:
"""
Open passed file in Audacity
@ -37,15 +37,34 @@ class AudacityManager:
self.to_pipe: str = "/tmp/audacity_script_pipe.to." + str(os.getuid())
self.from_pipe: str = "/tmp/audacity_script_pipe.from." + str(os.getuid())
self.eol: str = "\n"
self.path = path
self.do_command(f'Import2: Filename="{self.path}"')
self.path = None
def send_command(self, command: str) -> None:
def open_file(self, path):
"""Open passed file in Audacity"""
self.path = path
response = self._do_command(f'Import2: Filename="{self.path}"')
return response
def export_file(self):
"""Export current file"""
if not self.path:
return
sa_response = self._do_command("SelectAll:")
if sa_response == '\nBatchCommand finished: OK\n':
exp_response = self._do_command(f'Export2: Filename="{self.path}" NumChannels=2')
return exp_response
else:
print("SelectAll response: " + sa_response)
def _send_command(self, command: str) -> None:
"""Send a single command."""
self.to_audacity.write(command + self.eol)
self.to_audacity.flush()
def get_response(self) -> str:
def _get_response(self) -> str:
"""Return the command response."""
result: str = ""
@ -58,14 +77,14 @@ class AudacityManager:
break
return result
def do_command(self, command: str) -> str:
def _do_command(self, command: str) -> str:
"""Send one command, and return the response."""
with open(self.to_pipe, "w") as self.to_audacity, open(
self.from_pipe, "rt"
) as self.from_audacity:
self.send_command(command)
response = self.get_response()
self._send_command(command)
response = self._get_response()
return response

View File

@ -29,7 +29,6 @@ from helpers import (
file_is_unreadable,
get_embedded_time,
get_relative_date,
open_in_audacity,
ms_to_mmss,
set_track_metadata,
)
@ -1518,9 +1517,6 @@ class PlaylistProxyModel(QSortFilterProxyModel):
header_row_number, existing_prd, note
)
def open_in_audacity(self, row_number: int) -> None:
return self.data_model.open_in_audacity(row_number)
def previous_track_ended(self) -> None:
return self.data_model.previous_track_ended()

View File

@ -34,8 +34,8 @@ from classes import MusicMusterSignals, track_sequence
from config import Config
from helpers import (
ask_yes_no,
AudacityManager,
ms_to_mmss,
open_file_in_audacity,
show_OK,
show_warning,
)
@ -569,13 +569,10 @@ class PlaylistTab(QTableView):
if not path:
return
open_file_in_audacity(path)
if ask_yes_no("Reimport file", "Click yes to reimport file, No to ignore"):
audacity = AudacityManager()
audacity.open_file(path)
if ask_yes_no("Export file", "Click yes to export file, no to ignore"):
audacity.export_file()
def _rescan(self, row_number: int) -> None:
"""Rescan track"""