Exporting Audio using mod-script-pipe on MacOS

The Export2 command is not working properly for an audio file when I am using mod-script-pipe using python on my Mac. The exported output .wav file is giving error “file does not start with RIFF id”.
If I use the Export command it open the Export Dialog box on which no command is working for saving the audio finally. If I do that manually the exported audio is fine. But my requirements involve doing that with the script.

My Code:

import os
import sys
import time

if sys.platform == 'win32':
    TONAME = '\\\\.\\pipe\\ToSrvPipe'
    FROMNAME = '\\\\.\\pipe\\FromSrvPipe'
    EOL = '\r\n\0'
else:
    TONAME = '/tmp/audacity_script_pipe.to.' + str(os.getuid())
    FROMNAME = '/tmp/audacity_script_pipe.from.' + str(os.getuid())
    EOL = '\n'

if not os.path.exists(TONAME):
    sys.exit("Ensure Audacity is running with mod-script-pipe.")

if not os.path.exists(FROMNAME):
    sys.exit("Ensure Audacity is running with mod-script-pipe.")

TOFILE = open(TONAME, 'w')
FROMFILE = open(FROMNAME, 'rt')

def send_command(command):
    TOFILE.write(command + EOL)
    TOFILE.flush()

def get_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_command(command)
    response = get_response()
    return response

def apply_noise_reduction(input_file, output_file, noise_profile_duration=5):
    do_command(f'Import2: Filename="{input_file}"')
    time.sleep(5)

do_command(f'SelectTime: Start=0 End={noise_profile_duration}')
time.sleep(1)

do_command('NoiseReduction: action=GetProfile')
time.sleep(1)

do_command('SelectAll')
time.sleep(1)

do_command('NoiseReduction: action=Apply')
time.sleep(5)

# Export with specific parameters to ensure WAV format
do_command(f'Export2: Filename="{output_file}" Format=WAV(Microsoft) SampleRate=44100 Encoding="Signed 16-bit PCM"')
time.sleep(5)

# do_command('Close')
# time.sleep(1)

input_file = "/Users/jatin/Desktop/Python/extracted_audio.wav"
output_file = "/Users/jatin/Desktop/Python/audacity.wav"

apply_noise_reduction(input_file, output_file)

This is the dialog box that appears when using the “Export” Command

If anyone can help me, please respond I would really appreciate.

This topic was automatically closed after 30 days. New replies are no longer allowed.