def to_wav(src_path, wav_path): sox_in = pysox.CSoxStream(src_path) sox_out = pysox.CSoxStream(wav_path, 'w', pysox.CSignalInfo(16000, 1, 16), fileType='wav') sox_chain = pysox.CEffectsChain(sox_in, sox_out) sox_chain.add_effect(pysox.CEffect('rate', ['16000'])) sox_chain.flow_effects() sox_out.close()
def downsample(fs, sig): in_file = random_string() + ".wav" out_file = random_string() + ".wav" frame_len = fs * WINDOW_SIZE pad = len(sig) % frame_len if pad > 0: sig = np.append(sig, np.zeros(frame_len - pad)) f = Sndfile(in_file, 'w', Format(type="wav", encoding='pcm16', endianness="file"), 1, fs) f.write_frames(sig) f.close() sox_in = pysox.CSoxStream(in_file) sox_out = pysox.CSoxStream(out_file, 'w', pysox.CSignalInfo(SAMPLE_RATE, 1, 8), fileType='wav') sox_chain = pysox.CEffectsChain(sox_in, sox_out) sox_chain.add_effect(pysox.CEffect("rate", [str(SAMPLE_RATE)])) sox_chain.flow_effects() sox_out.close() f = Sndfile(out_file, 'r') sig = f.read_frames(f.nframes) f.close() os.unlink(in_file) os.unlink(out_file) return sig
infile = pysox.CSoxStream(file) outfile = pysox.CSoxStream(file + "-converted.flac", "w", infile.get_signal_info()) chain = pysox.CEffectsChain(infile, outfile) chain.flow_effects() outfile.close() # effects chain def fileEffect(filestoconvert) for file in filestoconvert: infile = pysox.CSoxStream(file) # gather info on file samplerate = infile.get_signal().get_signalinfo()['rate'] precision = infile.get_signal().get_signalinfo()['precision'] # fix sample rate and precision if samplerate < minsamplerate and precision < minprecision: outfile = pysox.CSoxStream(file + "-upsampled-higher_bit.flac", "w", pysox.CSignalInfo(minsamplerate, 2, minprecision)) # fix precision only elif samplerate > minsamplerate and precision < minprecision: outfile = pysox.CSoxStream(file + "-higher_bit.flac", "w", pysox.CSignalInfo(samplerate, 2, minprecision)) # fix sample rate only elif samplerate < minsamplerate and precision > minprecision: outfile = pysox.CSoxStream(file + "-upsampled.flac", "w", pysox.CSignalInfo(minsamplerate, 2, precision)) chain = pysox.CEffectsChain(infile, outfile) chain.flow_effects() outfile.close() os.remove(file) def main(): checkArgs() # convert lossys lossyfilestoconvert = fileList(lossys)
parser.add_argument('-m, --model-file', action='store', type=str, dest='model_file', help='the model file to use', default='models/params.pkl') args = parser.parse_args() # downsample file to 8KHz, 8 bits per sample in_file = args.input_file out_file = random_string() + ".wav" sox_in = pysox.CSoxStream(in_file) sox_out = pysox.CSoxStream(out_file, 'w', pysox.CSignalInfo(SAMPLE_RATE, 1, 8), fileType='wav') sox_chain = pysox.CEffectsChain(sox_in, sox_out) sox_chain.add_effect(pysox.CEffect("rate", [str(SAMPLE_RATE)])) sox_chain.flow_effects() sox_out.close() input_file = Sndfile(out_file, 'r') fs = input_file.samplerate num_frames = input_file.nframes window = args.window_size / 1000. chunk_size = int(np.floor(window * fs)) mlp = MLP_VAD(args.model_file)