当前位置: 首页>>代码示例>>Python>>正文


Python sox.Transformer方法代码示例

本文整理汇总了Python中sox.Transformer方法的典型用法代码示例。如果您正苦于以下问题:Python sox.Transformer方法的具体用法?Python sox.Transformer怎么用?Python sox.Transformer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sox的用法示例。


在下文中一共展示了sox.Transformer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _process_file

# 需要导入模块: import sox [as 别名]
# 或者: from sox import Transformer [as 别名]
def _process_file(file_item, target_sr):
    src = file_item[0]
    start = file_item[1]
    end = file_item[2]
    target = file_item[3]

    tfm = sox.Transformer()

    if start > 0 and end == float('inf'):
        tfm.trim(start)
    elif end != float('inf'):
        tfm.trim(start, end)

    tfm.convert(target_sr, 1, 16)
    try:
        tfm.build(src, target)
    except sox.core.SoxError:
        logger.error('The following file could not be converted: %s', src) 
开发者ID:ynop,项目名称:audiomate,代码行数:20,代码来源:wav.py

示例2: run_pipeline

# 需要导入模块: import sox [as 别名]
# 或者: from sox import Transformer [as 别名]
def run_pipeline(pipeline, input_filename, output_filename):
  """Run an audio transform pipeline.

  This will run the pipeline on an input audio file, producing an output audio
  file. Transform parameters will be sampled at each stage.

  Args:
    pipeline: The pipeline to run, a list of AudioTransformStage objects.
    input_filename: Path to the audio file to be transformed.
    output_filename: Path to the resulting output audio file.
  """
  transformer = sox.Transformer()
  transformer.set_globals(guard=True)
  for stage in pipeline:
    stage.apply(transformer)
  transformer.build(input_filename, output_filename) 
开发者ID:magenta,项目名称:magenta,代码行数:18,代码来源:audio_transform.py

示例3: mp3_to_flac

# 需要导入模块: import sox [as 别名]
# 或者: from sox import Transformer [as 别名]
def mp3_to_flac(data, samplerate, bitdepth):
    tid = threading.get_ident()
    tmp_filename = os.path.join('/tmp/transcode-{}.mp3'.format(tid))
    tmp2_filename = os.path.join('/tmp/transcode-{}.flac'.format(tid))
    with open(tmp_filename, 'wb') as content_file:
        size = content_file.write(data)
        status = 0 if size == len(data) else -1
    transformer = sox.Transformer()
    transformer.convert(samplerate=samplerate, n_channels=2, bitdepth=bitdepth)
    transformer.build(tmp_filename, tmp2_filename)

    # retrieve data in a buffer
    with open(tmp2_filename, mode='rb') as file:
        flac_data = file.read()

    os.remove(tmp_filename)
    os.remove(tmp2_filename)
    return flac_data 
开发者ID:PacktPublishing,项目名称:Hands-On-Reactive-Programming-with-Python,代码行数:20,代码来源:encoder.py

示例4: mp3_to_flac

# 需要导入模块: import sox [as 别名]
# 或者: from sox import Transformer [as 别名]
def mp3_to_flac(data):
    tmp_filename = os.path.join('/tmp/transcode-tmp.mp3')
    tmp2_filename = os.path.join('/tmp/transcode-tmp.flac')
    with open(tmp_filename, 'wb') as content_file:
        size = content_file.write(data)
        status = 0 if size == len(data) else -1
    transformer = sox.Transformer()
    transformer.convert(samplerate=16000, n_channels=2, bitdepth=16)
    transformer.build(tmp_filename, tmp2_filename)

    # retrieve data in a buffer
    with open(tmp2_filename, mode='rb') as file:
        flac_data = file.read()

    os.remove(tmp_filename)
    os.remove(tmp2_filename)
    return flac_data 
开发者ID:PacktPublishing,项目名称:Hands-On-Reactive-Programming-with-Python,代码行数:19,代码来源:encoder.py

示例5: mp3_to_flac

# 需要导入模块: import sox [as 别名]
# 或者: from sox import Transformer [as 别名]
def mp3_to_flac(data):
    tid = threading.get_ident()
    tmp_filename = os.path.join('/tmp/transcode-{}.mp3'.format(tid))
    tmp2_filename = os.path.join('/tmp/transcode-{}.flac'.format(tid))
    with open(tmp_filename, 'wb') as content_file:
        size = content_file.write(data)
        status = 0 if size == len(data) else -1
    transformer = sox.Transformer()
    transformer.convert(samplerate=16000, n_channels=2, bitdepth=16)
    transformer.build(tmp_filename, tmp2_filename)

    # retrieve data in a buffer
    with open(tmp2_filename, mode='rb') as file:
        flac_data = file.read()

    os.remove(tmp_filename)
    os.remove(tmp2_filename)
    return flac_data 
开发者ID:PacktPublishing,项目名称:Hands-On-Reactive-Programming-with-Python,代码行数:20,代码来源:encoder.py

示例6: __init__

# 需要导入模块: import sox [as 别名]
# 或者: from sox import Transformer [as 别名]
def __init__(self, name, params):
    """Initialize an AudioTransformStage.

    Args:
      name: The name of the stage. Should be the same as the name of the method
          called on a sox.Transformer object.
      params: A list of AudioTransformParameter objects.
    """
    self.name = name
    self.params = params 
开发者ID:magenta,项目名称:magenta,代码行数:12,代码来源:audio_transform.py

示例7: apply

# 需要导入模块: import sox [as 别名]
# 或者: from sox import Transformer [as 别名]
def apply(self, transformer):
    """Apply this stage to a sox.Transformer object.

    Args:
      transformer: The sox.Transformer object to which this pipeline stage
          should be applied. No audio will actually be transformed until the
          `build` method is called on `transformer`.
    """
    args = dict((param.name, param.sample()) for param in self.params)
    getattr(transformer, self.name)(**args) 
开发者ID:magenta,项目名称:magenta,代码行数:12,代码来源:audio_transform.py

示例8: ensure_samplerate

# 需要导入模块: import sox [as 别名]
# 或者: from sox import Transformer [as 别名]
def ensure_samplerate(audio_path):
    samplerate = sox.file_info.sample_rate(audio_path)
    if samplerate != 44100:
        tfm = sox.Transformer()
        tfm.rate(44100)
        _, pyin_audio = tempfile.mkstemp(suffix='.wav')
        tfm.build(audio_path, pyin_audio)
        os.remove(audio_path)
        shutil.move(pyin_audio, audio_path) 
开发者ID:marl,项目名称:medleydb,代码行数:11,代码来源:run_pyin_all.py

示例9: _generate_audio_file

# 需要导入模块: import sox [as 别名]
# 或者: from sox import Transformer [as 别名]
def _generate_audio_file(self):
        """
        Generic method used as a Callback in TTSModule
            - must provided the audio file and write it on the disk

        .. raises:: FailToLoadSoundFile
        """
        if self.path is None:
            # we try to get the path from the env
            self.path = self._get_pico_path()
            # if still None, we set a default value
            if self.path is None:
                self.path = "/usr/bin/pico2wave"

        # pico2wave needs that the file path ends with .wav
        tmp_path = self.file_path+".wav"
        pico2wave_options = ["-l=%s" % self.language, "-w=%s" % tmp_path]

        final_command = list()
        final_command.extend([self.path])
        final_command.extend(pico2wave_options)
        final_command.append(self.words)

        logger.debug("[Pico2wave] command: %s" % final_command)

        # generate the file with pico2wav
        subprocess.call(final_command)
        
        # convert samplerate
        if self.samplerate is not None:
            tfm = sox.Transformer()
            tfm.rate(samplerate=self.samplerate)
            tfm.build(str(tmp_path), str(tmp_path) + "tmp_name.wav")
            os.rename(str(tmp_path) + "tmp_name.wav", tmp_path)
        
        # remove the extension .wav
        os.rename(tmp_path, self.file_path) 
开发者ID:kalliope-project,项目名称:kalliope,代码行数:39,代码来源:pico2wave.py

示例10: convert_audio

# 需要导入模块: import sox [as 别名]
# 或者: from sox import Transformer [as 别名]
def convert_audio(src_audio_path, dst_audio_path, file_type=None, audio_format=DEFAULT_FORMAT):
    sample_rate, channels, width = audio_format
    try:
        transformer = sox.Transformer()
        transformer.set_output_format(file_type=file_type, rate=sample_rate, channels=channels, bits=width*8)
        transformer.build(src_audio_path, dst_audio_path)
    except sox.core.SoxError:
        return False
    return True 
开发者ID:mozilla,项目名称:DSAlign,代码行数:11,代码来源:audio.py

示例11: mp3_to_flac

# 需要导入模块: import sox [as 别名]
# 或者: from sox import Transformer [as 别名]
def mp3_to_flac(data, dest_path, name):
    tmp_filename = os.path.join('/tmp/transcode-tmp.mp3')
    dest_filename = os.path.join(dest_path, name + '.flac')
    with open(tmp_filename, 'wb') as content_file:
        size = content_file.write(data)
        status = 0 if size == len(data) else -1
    transformer = sox.Transformer()
    transformer.convert(samplerate=16000, n_channels=2, bitdepth=16)
    transformer.build(tmp_filename, dest_filename)
    return dest_filename 
开发者ID:PacktPublishing,项目名称:Hands-On-Reactive-Programming-with-Python,代码行数:12,代码来源:encoder.py

示例12: convert_audio_and_split_transcript

# 需要导入模块: import sox [as 别名]
# 或者: from sox import Transformer [as 别名]
def convert_audio_and_split_transcript(input_dir,
                                       source_name,
                                       target_name,
                                       output_dir,
                                       output_file):
    """Convert FLAC to WAV and split the transcript.
    Args:
        input_dir: the directory which holds the input dataset.
        source_name: the name of the specified dataset. e.g. test-clean
        target_name: the directory name for the newly generated audio files.
                 e.g. test-clean-wav
        output_dir: the directory to place the newly generated csv files.
        output_file: the name of the newly generated csv file. e.g. test-clean.csv
    """

    logging.info("Processing audio and transcript for %s" % source_name)
    source_dir = os.path.join(input_dir, source_name)
    target_dir = os.path.join(input_dir, target_name)

    if not gfile.Exists(target_dir):
        gfile.MakeDirs(target_dir)

    files = []
    tfm = Transformer()
    # Convert all FLAC file into WAV format. At the same time, generate the csv
    for root, _, filenames in gfile.Walk(source_dir):
        for filename in fnmatch.filter(filenames, "*.trans.txt"):
            trans_file = os.path.join(root, filename)
            with codecs.open(trans_file, "r", "utf-8") as fin:
                for line in fin:
                    seqid, transcript = line.split(" ", 1)
                    # We do a encode-decode transformation here because the output type
                    # of encode is a bytes object, we need convert it to string.
                    transcript = (
                        unicodedata.normalize("NFKD", transcript)
                        .encode("ascii", "ignore")
                        .decode("ascii", "ignore")
                        .strip()
                        .lower()
                    )

                    # Convert FLAC to WAV.
                    flac_file = os.path.join(root, seqid + ".flac")
                    wav_file = os.path.join(target_dir, seqid + ".wav")
                    if not gfile.Exists(wav_file):
                        tfm.build(flac_file, wav_file)
                    # wav_filesize = os.path.getsize(wav_file)
                    wav_length = get_wave_file_length(wav_file)
                    speaker = seqid.split('-')[0]
                    files.append((os.path.abspath(wav_file), wav_length, transcript, speaker))
    # Write to CSV file which contains three columns:
    # "wav_filename", "wav_length_ms", "transcript", "speaker".
    csv_file_path = os.path.join(output_dir, output_file)
    df = pandas.DataFrame(
        data=files, columns=["wav_filename", "wav_length_ms", "transcript", "speaker"]
    )
    df.to_csv(csv_file_path, index=False, sep="\t")
    logging.info("Successfully generated csv file {}".format(csv_file_path)) 
开发者ID:athena-team,项目名称:athena,代码行数:60,代码来源:prepare_data.py

示例13: preprocess

# 需要导入模块: import sox [as 别名]
# 或者: from sox import Transformer [as 别名]
def preprocess(data, input_dir, dest_dir, target_sr=None, speed=None,
               overwrite=True):
    speed = speed or []
    speed.append(1)
    speed = list(set(speed))  # Make uniqe

    input_fname = os.path.join(input_dir,
                               data['input_relpath'],
                               data['input_fname'])
    input_sr = sox.file_info.sample_rate(input_fname)
    target_sr = target_sr or input_sr

    os.makedirs(os.path.join(dest_dir, data['input_relpath']), exist_ok=True)

    output_dict = {}
    output_dict['transcript'] = data['transcript'].lower().strip()
    output_dict['files'] = []

    fname = os.path.splitext(data['input_fname'])[0]
    for s in speed:
        output_fname = fname + \
            '{}.wav'.format('' if s == 1 else '-{}'.format(s))
        output_fpath = os.path.join(dest_dir,
                                    data['input_relpath'],
                                    output_fname)

        if not os.path.exists(output_fpath) or overwrite:
            cbn = sox.Transformer().speed(factor=s).convert(target_sr)
            cbn.build(input_fname, output_fpath)

        file_info = sox.file_info.info(output_fpath)
        file_info['fname'] = os.path.join(os.path.basename(dest_dir),
                                          data['input_relpath'],
                                          output_fname)
        file_info['speed'] = s
        output_dict['files'].append(file_info)

        if s == 1:
            file_info = sox.file_info.info(output_fpath)
            output_dict['original_duration'] = file_info['duration']
            output_dict['original_num_samples'] = file_info['num_samples']

    return output_dict 
开发者ID:mlperf,项目名称:inference,代码行数:45,代码来源:preprocessing_utils.py

示例14: resample_file

# 需要导入模块: import sox [as 别名]
# 或者: from sox import Transformer [as 别名]
def resample_file(resampled_dir, filepath, ext):
    """
    Resample an audio file to 16kHZ and transform to monochannel
    Remove incompatible files.

    Args:
        resampled_dir: Directory of transformed files.
        filepath: Filepath of Audio
        ext: File type e.g. "wav", "flac"

    Returns:

    """
    head, filename = os.path.split(filepath)
    _, clsname = os.path.split(head)

    filename, _ = os.path.splitext(filename)

    new_dir = os.path.join(resampled_dir, clsname)
    if not os.path.exists(new_dir):
        os.makedirs(new_dir)

    new_path = os.path.join(new_dir, filename + f'.{ext}')

    # check if the resampled data exists.
    if os.path.exists(new_path):
        print(f"Resampled file {filepath} exists. Skip it.")
        return None

    transform = sox.Transformer()
    transform.set_output_format(file_type='wav')
    transform.convert(samplerate=16000, n_channels=1)

    try:
        transform.build(filepath, new_path)
        print(f"Finished converting file {filepath}.")

        return None

    except sox.core.SoxError as e:

        try:
            # Check if the file is readable
            librosa.load(filepath)

            # if it is, force input format and try again
            transform.set_input_format(file_type=ext)
            transform.build(filepath, new_path)
            return None

        except Exception:
            return filepath 
开发者ID:NVIDIA,项目名称:NeMo,代码行数:54,代码来源:freesound_resample.py

示例15: preprocess

# 需要导入模块: import sox [as 别名]
# 或者: from sox import Transformer [as 别名]
def preprocess(data, input_dir, dest_dir, target_sr=None, speed=None,
               overwrite=True):
    speed = speed or []
    speed.append(1)
    speed = list(set(speed))  # Make uniqe

    input_fname = os.path.join(input_dir,
                               data['input_relpath'],
                               data['input_fname'])
    input_sr = sox.file_info.sample_rate(input_fname)
    target_sr = target_sr or input_sr

    os.makedirs(os.path.join(dest_dir, data['input_relpath']), exist_ok=True)

    output_dict = {}
    output_dict['transcript'] = data['transcript'].lower().strip()
    output_dict['files'] = []

    fname = os.path.splitext(data['input_fname'])[0]
    for s in speed:
        output_fname = fname + '{}.wav'.format('' if s==1 else '-{}'.format(s))
        output_fpath = os.path.join(dest_dir,
                                    data['input_relpath'],
                                    output_fname)

        if not os.path.exists(output_fpath) or overwrite:
            cbn = sox.Transformer().speed(factor=s).convert(target_sr)
            cbn.build(input_fname, output_fpath)

        file_info = sox.file_info.info(output_fpath)
        file_info['fname'] = os.path.join(os.path.basename(dest_dir),
                                          data['input_relpath'],
                                          output_fname)
        file_info['speed'] = s
        output_dict['files'].append(file_info)

        if s == 1:
            file_info = sox.file_info.info(output_fpath)
            output_dict['original_duration'] = file_info['duration']
            output_dict['original_num_samples'] = file_info['num_samples']

    return output_dict 
开发者ID:mlperf,项目名称:training,代码行数:44,代码来源:preprocessing_utils.py


注:本文中的sox.Transformer方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。