Exemplo n.º 1
0
def read_signal(filename, offset=0, nsamples=-1, nchannels=0, offset_is_samples=False):
    """Read a wavefile and return as numpy array of floats.

    Args:
        filename (string): Name of file to read
        offset (int, optional): Offset in samples or seconds (from start). Defaults to 0.
        nchannels: expected number of channel (default: 0 = any number OK)
        offset_is_samples (bool): measurement units for offset (default: False)
    Returns:
        ndarray: audio signal
    """
    try:
        wave_file = SoundFile(filename)
    except:
        # Ensure incorrect error (24 bit) is not generated
        raise Exception(f"Unable to read {filename}.")

    if nchannels != 0 and wave_file.channels != nchannels:
        raise Exception(
            f"Wav file ({filename}) was expected to have {nchannels} channels."
        )

    if wave_file.samplerate != CONFIG.fs:
        raise Exception(f"Sampling rate is not {CONFIG.fs} for filename {filename}.")

    if not offset_is_samples:  # Default behaviour
        offset = int(offset * wave_file.samplerate)

    if offset != 0:
        wave_file.seek(offset)

    x = wave_file.read(frames=nsamples)

    return x
Exemplo n.º 2
0
    def __init__(self, file: str):
        """Creates an instance of a Decoder source with the given configuration

        Args:
            file_name (str): Input file name
            frames_per_buffer (int): Number of frames per buffer.
            name (str): Name of the element
        """
        self.__instance = SoundFile(file=file, mode='r')
Exemplo n.º 3
0
def load_dict(filename):
    """
    Load a wave file and return the signal, sample rate and number of channels.

    Can be any format supported by the underlying library (libsndfile or SciPy)
    """
    soundfile = {}
    if wav_loader == 'pysoundfile':
        sf = SoundFile(filename)
        soundfile['signal'] = sf.read()
        soundfile['channels'] = sf.channels
        soundfile['fs'] = sf.samplerate
        soundfile['samples'] = len(sf)
        soundfile['format'] = sf.format_info + ' ' + sf.subtype_info
        sf.close()
    elif wav_loader == 'scikits.audiolab':
        sf = Sndfile(filename, 'r')
        soundfile['signal'] = sf.read_frames(sf.nframes)
        soundfile['channels'] = sf.channels
        soundfile['fs'] = sf.samplerate
        soundfile['samples'] = sf.nframes
        soundfile['format'] = sf.format
        sf.close()
    elif wav_loader == 'scipy.io.wavfile':
        soundfile['fs'], soundfile['signal'] = read(filename)
        try:
            soundfile['channels'] = soundfile['signal'].shape[1]
        except IndexError:
            soundfile['channels'] = 1
        soundfile['samples'] = soundfile['signal'].shape[0]
        soundfile['format'] = str(soundfile['signal'].dtype)

    return soundfile
Exemplo n.º 4
0
def load(filename):
    """
    Load a wave file and return the signal, sample rate and number of channels.
    Can be any format supported by the underlying library (libsndfile or SciPy)
    """
    if wav_loader == 'pysoundfile':
        sf = SoundFile(filename)
        signal = sf.read()
        channels = sf.channels
        sample_rate = sf.samplerate
        samples = len(sf)
        file_format = sf.format_info + ' ' + sf.subtype_info
        sf.close()
    elif wav_loader == 'scikits.audiolab':
        sf = Sndfile(filename, 'r')
        signal = sf.read_frames(sf.nframes)
        channels = sf.channels
        sample_rate = sf.samplerate
        samples = sf.nframes
        file_format = sf.format
        sf.close()
    elif wav_loader == 'scipy.io.wavfile':
        sample_rate, signal = read(filename)
        try:
            channels = signal.shape[1]
        except IndexError:
            channels = 1
        samples = signal.shape[0]
        file_format = str(signal.dtype)

    return signal, sample_rate, channels
Exemplo n.º 5
0
def batch_list(file_dir, list_name, data_path='data', make_new=False):
    """
	Places the file paths and wav lengths of an audio file into a dictionary, which
	is then appended to a list. 'glob' is used to support Unix style pathname
	pattern expansions. Checks if the training list has already been saved, and loads
	it.

	Argument/s:
		file_dir - directory containing the audio files.
		list_name - name for the list.
		data_path - path to store pickle files.
		make_new - re-create list.

	Returns:
		batch_list - list of file paths and wav length.
	"""
    extension = ['*.wav', '*.flac', '*.mp3']
    if not make_new:
        if os.path.exists(data_path + '/' + list_name + '_list_' +
                          platform.node() + '.p'):
            print('Loading ' + list_name + ' list...')
            with open(
                    data_path + '/' + list_name + '_list_' + platform.node() +
                    '.p', 'rb') as f:
                batch_list = pickle.load(f)
            if batch_list[0]['file_path'].find(file_dir) != -1:
                print(list_name + ' list has a totaltry: of %i entries.' %
                      (len(batch_list)))
                return batch_list

    print('Creating ' + list_name + ' list...')
    batch_list = []
    for i in extension:
        for j in glob.glob(os.path.join(file_dir, i)):
            try:
                f = SoundFile(j)
                wav_len = f.seek(0, SEEK_END)
                if wav_len == -1:
                    wav, _ = read_wav(j)
                    wav_len = len(wav)
            except NameError:
                wav, _ = read_wav(j)
                wav_len = len(wav)
            batch_list.append({
                'file_path': j,
                'wav_len': wav_len
            })  # append dictionary.
    if not os.path.exists(data_path): os.makedirs(data_path)  # make directory.
    with open(data_path + '/' + list_name + '_list_' + platform.node() + '.p',
              'wb') as f:
        pickle.dump(batch_list, f)
    print('The ' + list_name + ' list has a total of %i entries.' %
          (len(batch_list)))
    return batch_list
Exemplo n.º 6
0
class Encoder(Sink):
    """This class is an interface to write data into an audio file"""
    def __init__(self,
                 file_name: str,
                 rate: int,
                 channels: int,
                 name: str = ""):
        """Creates an instance of a Encoder source with the given configuration

        Args:
            file_name (str): Input file name
            rate (int): The sample rate of the file in Hz
            channels (int): The number of channels
            name (str): Name of the element
        """
        super().__init__(name)
        self.__instance = SoundFile(file=file_name,
                                    mode='w',
                                    samplerate=rate,
                                    channels=channels)

    @property
    def sample_rate(self) -> int:
        """Return the sampling rate in Hz."""
        return self.__instance.samplerate

    @property
    def channels(self) -> int:
        """Return the number of channels."""
        return self.__instance.channels

    @property
    def file_name(self) -> str:
        """Return the file name."""
        return self.__instance.name

    def seek(self, frames):
        """Set the write position.

        Args:
            frames: The frame index or offset to seek
        """
        return self.__instance.seek(frames=frames)

    def process(self, data, extra=None):
        """Writes the buffer of data into the audio file.

        Args:
            data: Array to write in the file
            extra: Any extra information previously computed.
        """
        self.__instance.write(data.flatten()), extra
Exemplo n.º 7
0
def test_resample_all():
    """
    Tests that a properly structured directory of labelled audio files is successfully
    resampled at the desired rate, saved as a different file type, and written to
    the desired new or old location.
    :return:
    """
    aud1, sr = librosa.load(librosa.ex('trumpet'))
    aud2, _ = librosa.load(librosa.ex('nutcracker'))

    # setup mock mnist style dataset file structure
    rand_loc1 = ''.join(random.choices(string.ascii_letters, k=6))
    rand_loc2 = ''.join(random.choices(string.ascii_letters, k=6))
    rand_loc1 = os.path.join(ROOT_DIR, rand_loc1)
    rand_loc2 = os.path.join(ROOT_DIR, rand_loc2)
    sub_dir1 = os.path.join(rand_loc1, 'l1')
    sub_dir2 = os.path.join(rand_loc1, 'l2')
    save1 = os.path.join(sub_dir1, 'test1.m4a')
    save2 = os.path.join(sub_dir2, 'test2.m4a')

    # write '.m4a' data to mock file structure
    os.makedirs(sub_dir1, exist_ok=True)
    os.makedirs(sub_dir2, exist_ok=True)

    with SoundFile(save1, 'w', sr, channels=1, format='WAV') as f1:
        f1.write(aud1)
    with SoundFile(save2, 'w', sr, channels=1, format='WAV') as f2:
        f2.write(aud2)

    # verify new files of type '.wav' save in same/old directory
    resample_all(rand_loc1, rand_loc1, sr)
    assert os.path.isfile(os.path.join(
        sub_dir1, 'rs_test1.wav')), "File not saved to old directory"
    assert os.path.isfile(os.path.join(
        sub_dir2, 'rs_test2.wav')), "File not saved to old directory"

    # verify new files of '.wav' are saved in new/different directory
    resample_all(rand_loc1, rand_loc2, sr)
    assert os.path.isfile(
        os.path.join(rand_loc2, sub_dir1,
                     'rs_test1.wav')), "File not saved to new directory"
    assert os.path.isfile(
        os.path.join(rand_loc2, sub_dir2,
                     'rs_test2.wav')), "File not saved to new directory"
    assert os.path.isfile(os.path.join(
        ROOT_DIR, 'manifest.txt')), "Manifest of resampled files not generated"

    # Delete all generated test directories and files.
    shutil.rmtree(rand_loc1)
    shutil.rmtree(rand_loc2)
    os.remove(os.path.join(ROOT_DIR, 'manifest.txt'))
Exemplo n.º 8
0
def load(filename):
    """
    Load a wave file and return the signal, sample rate and number of channels.

    Can be any format supported by the underlying library (libsndfile or SciPy)
    """
    if wav_loader == 'pysoundfile':
        sf = SoundFile(filename)
        signal = sf.read()
        channels = sf.channels
        sample_rate = sf.samplerate
        sf.close()
    elif wav_loader == 'scikits.audiolab':
        sf = Sndfile(filename, 'r')
        signal = sf.read_frames(sf.nframes)
        channels = sf.channels
        sample_rate = sf.samplerate
        sf.close()
    elif wav_loader == 'scipy.io.wavfile':
        sample_rate, signal = read(filename)
        try:
            channels = signal.shape[1]
        except IndexError:
            channels = 1

    return signal, sample_rate, channels
Exemplo n.º 9
0
def Batch_list(file_dir, list_name, data_path=None, make_new=False):
    from soundfile import SoundFile, SEEK_END
    '''
	Places the file paths and wav lengths of an audio file into a dictionary, which 
	is then appended to a list. SPHERE format cannot be used. 'glob' is used to 
	support Unix style pathname pattern expansions. Checks if the training list 
	has already been pickled, and loads it. If a different dataset is to be 
	used, delete the pickle file.

	Inputs:
		file_dir - directory containing the wavs.
		list_name - name for the list.
		data_path - path to store pickle files.
		make_new - re-create list.

	Outputs:
		batch_list - list of file paths and wav length.
	'''
    file_name = ['*.wav', '*.flac', '*.mp3']
    if data_path == None: data_path = 'data'
    if not make_new:
        if os.path.exists(data_path + '/' + list_name + '_list_' +
                          platform.node() + '.p'):
            print('Loading ' + list_name + ' list from pickle file...')
            with open(
                    data_path + '/' + list_name + '_list_' + platform.node() +
                    '.p', 'rb') as f:
                batch_list = pickle.load(f)
            if batch_list[0]['file_path'].find(file_dir) != -1:
                print('The ' + list_name + ' list has a total of %i entries.' %
                      (len(batch_list)))
                return batch_list
    print('Creating ' + list_name + ' list, as no pickle file exists...')
    batch_list = []  # list for wav paths and lengths.
    for fn in file_name:
        for file_path in glob.glob(os.path.join(file_dir, fn)):
            f = SoundFile(file_path)
            seq_len = f.seek(0, SEEK_END)
            batch_list.append({
                'file_path': file_path,
                'seq_len': seq_len
            })  # append dictionary.
    if not os.path.exists(data_path): os.makedirs(data_path)  # make directory.
    with open(data_path + '/' + list_name + '_list_' + platform.node() + '.p',
              'wb') as f:
        pickle.dump(batch_list, f)
    print('The ' + list_name + ' list has a total of %i entries.' %
          (len(batch_list)))
    return batch_list
Exemplo n.º 10
0
def get_audio_metadata(audioPath, sphereType=False):
    '''
    Returns sampling rate, number of channels and duration of an audio file
    :param audioPath: 
    :param sphereType: 
    :return: 
    '''
    #this will get rid of audiolab which is py2.7 only
    # snd_file = SoundFile(audioPath, mode='r')
    # inf = snd_file._info
    # sr = inf.samplerate
    # channels = inf.channels
    # duration = float(inf.frames) / float(inf.samplerate)
    # return int(sr), int(channels), float(duration)
    #--delete below when uncommented

    ext = os.path.splitext(audioPath)[1][1:].lower()
    if ext == "aiff" or sphereType:  # SPHERE headers for the TIMIT dataset
        audio = scikits.audiolab.Sndfile(audioPath)
        sr = audio.samplerate
        channels = audio.channels
        duration = float(audio.nframes) / float(audio.samplerate)
    elif ext == "mp3":  # Use ffmpeg/ffprobe
        sr, channels, duration = get_mp3_metadata(audioPath)
    else:
        snd_file = SoundFile(audioPath, mode='r')
        inf = snd_file._info
        sr = inf.samplerate
        channels = inf.channels
        duration = float(inf.frames) / float(inf.samplerate)
    return int(sr), int(channels), float(duration)
Exemplo n.º 11
0
def generate_spectrogram(mseed_file, highpass, samplerate):
    """
    Execute a high pass filter on the meed input. Create a still image movie of the spectrogram with the resulting
    flac audio.

    :param mseed_file:  Hydrophone audio file in mseed format
    :param highpass:    High pass frequency for filtering
    :return:  output video filename if successful, otherwise None
    """

    root, ext = os.path.splitext(mseed_file)
    if ext != '.mseed':
        log.error('input file (%s) must be mseed format' % mseed_file)
        return None

    stream = obspy.read(mseed_file)
    filtered = stream.copy()

    # generate plot image
    image = '%s.png' % root
    filtered.filter('highpass', freq=highpass)
    filtered.plot(color='blue', outfile=image)

    # generate audio file
    audio = '%s.flac' % root
    filtered.normalize()
    with SoundFile(audio, 'w', samplerate, 1) as f:
        f.write(filtered[0].data)

    # generate movie clip
    clip = '%s.mp4' % root
    try:
        subprocess.check_call([
            'ffmpeg',
            '-loop',
            '1',
            '-i',
            image,
            '-i',
            audio,
            '-c:v',
            'libx264',
            '-tune',
            'stillimage',  # video codec, tuned for still image
            '-c:a',
            'aac',
            '-b:a',
            '192k',  # audio codec
            '-pix_fmt',
            'yuv420p',
            '-shortest',
            clip
        ])  # output options
        log.info('successfully created clip: %s' % clip)

    except subprocess.CalledProcessError as e:
        log.error('failed to create clip: %s - %r' % (clip, e))
        return None

    return clip
Exemplo n.º 12
0
def get_audio_metadata(audioPath, sphereType=False):
    '''
    Returns sampling rate, number of channels and duration of an audio file
    :param audioPath: 
    :param sphereType: 
    :return: 
    '''
    ext = os.path.splitext(audioPath)[1][1:].lower()
    if False:
        if ext == "aiff" or sphereType:  # SPHERE headers for the TIMIT dataset
            audio = scikits.audiolab.Sndfile(audioPath)
            sr = audio.samplerate
            channels = audio.channels
            duration = float(audio.nframes) / float(audio.samplerate)
        elif ext == "mp3":  # Use ffmpeg/ffprobe
            sr, channels, duration = get_mp3_metadata(audioPath)
        else:
            snd_file = SoundFile(audioPath, mode='r')
            inf = snd_file._info
            sr = inf.samplerate
            channels = inf.channels
            duration = float(inf.frames) / float(inf.samplerate)
    else:
        if ext == "mp3":  # Use ffmpeg/ffprobe
            sr, channels, duration = get_mp3_metadata(audioPath)
        else:
            sr, _, _, channels, num_frames = sndio.get_info(audioPath,
                                                            extended_info=True)
            duration = num_frames / sr

    return int(sr), int(channels), float(duration)
Exemplo n.º 13
0
def convert_flac_to_wav(wav_path: str) -> None:
    """ Convert a .flac speech file to .wav file 
        Parameters
        -----------
        :params wav_path: Path to the flac file 
        
        Notes
        -----------
        Open the .flac file using soundfile and write to .wav
        format.
        
        Usage
        -----------
        >>> #TODO
    """

    with SoundFile(wav_path, ) as wav:
        wav_arr = wav.read()

        sample_rate = wav.samplerate
        nframes = wav.frames
        duration = nframes / sample_rate
        print(f"Wave duration is {duration} .")
        output_paths = wav_path.split(".")
        output_path = output_paths[0] + ".wav"
        wav_id = output_paths[0].split("/")[-1]

        sf.write(output_path, wav_arr, sample_rate)

    return (output_path, duration, wav_id)
Exemplo n.º 14
0
 def save_wav(self, wav, overwrite=False):
     try:
         out_arr = wav.view(-1).cpu().numpy()
         fname = self.out_dir + "/" + self.out_name + ".wav"
         import soundfile
         from soundfile import SoundFile
         if overwrite:
             soundfile.write(fname,
                             out_arr,
                             samplerate=self.sample_rate,
                             format="WAV")
         else:
             try:
                 with SoundFile(fname, mode="r+") as wav_file:
                     wav_file.seek(0, soundfile.SEEK_END)
                     wav_file.write(out_arr)
             except Exception as e:
                 soundfile.write(fname,
                                 out_arr,
                                 samplerate=self.sample_rate,
                                 format="WAV")
                 self.comm("[conversion-done]", "first")
         # if self.mlDevice == "cuda":
         #     torch.cuda.empty_cache()
     except Exception as e:
         self.output_err("Write error", e)
Exemplo n.º 15
0
    def read_from_file(self, audio_file_path):
        """
        Read audio from file.

        Parameters:
        ----------
        audio_file_path : str
            Path to audio file.

        Returns:
        -------
        np.array
            Audio data.
        """
        from soundfile import SoundFile
        with SoundFile(audio_file_path, "r") as data:
            sample_rate = data.samplerate
            audio_data = data.read(dtype="float32")

        audio_data = audio_data.transpose()

        if sample_rate != self.desired_audio_sample_rate:
            from librosa.core import resample as lr_resample
            audio_data = lr_resample(y=audio_data,
                                     orig_sr=sample_rate,
                                     target_sr=self.desired_audio_sample_rate)
        if audio_data.ndim >= 2:
            audio_data = np.mean(audio_data, axis=1)

        return audio_data
Exemplo n.º 16
0
def _loadFile(fileName):
    sf = SoundFile(fileName)
    signal = sf.read()
    channels = sf.channels
    sample_rate = sf.samplerate
    sf.close()
    return signal, channels, sample_rate
Exemplo n.º 17
0
def cli_convert_main(input_files):
    loop = gobject.MainLoop()
    gobject.threads_init()
    context = loop.get_context()
    error.set_error_handler(error.ErrorPrinter())

    output_type = settings['cli-output-type']
    output_suffix = settings['cli-output-suffix']

    generator = TargetNameGenerator()
    generator.suffix = output_suffix

    progress = CliProgress()

    queue = TaskQueue()
    for input_file in input_files:
        input_file = SoundFile(input_file)
        output_name = generator.get_target_name(input_file)
        c = Converter(input_file, output_name, output_type)
        c.init()
        c.start()
        while c.running:
            if c.get_duration():
                percent = min(100,
                              100.0 * (c.get_position() / c.get_duration()))
                percent = '%.1f %%' % percent
            else:
                percent = '/-\|'[int(time.time()) % 4]
            progress.show(
                '%s: %s' %
                (unquote_filename(c.sound_file.filename[-65:]), percent))
            time.sleep(0.01)
            context.iteration(True)
        print

    previous_filename = None
    '''
    queue.start()
    
    #running, progress = queue.get_progress(perfile)
    while queue.running:
        t = None #queue.get_current_task()
        if t and not settings['quiet']:
            if previous_filename != t.sound_file.get_filename_for_display():
                if previous_filename:
                    print _('%s: OK') % previous_filename
                previous_filename = t.sound_file.get_filename_for_display()

            percent = 0
            if t.get_duration():
                percent = '%.1f %%' % ( 100.0* (t.get_position() / t.get_duration() ))
            else:
                percent = '/-\|' [int(time.time()) % 4]
            progress.show('%s: %s' % (t.sound_file.get_filename_for_display()[-65:], percent ))
        time.sleep(0.10)
        context.iteration(True)
    '''
    if not settings['quiet']:
        progress.clear()
Exemplo n.º 18
0
async def sunvox2audio(
    process: BufferedProcess,
    slot: Slot,
    audio_paths: List[Path],
    freq: int,
    channels: int,
    max_file_size=8000000,
):
    audio_paths = [Path(p) for p in audio_paths]
    try:
        length = slot.get_song_length_frames()
        log.info("Sunvox reports song length is %d frames", length)
        slot.play_from_beginning()
        position = 0
        audio_files = [
            SoundFile(
                str(p),
                "w",
                freq,
                channels,
                "FLOAT" if str(p).endswith(".wav") else None,
            )
            for p in audio_paths
        ]
        try:
            while position < length:
                percentage = position * 100.0 / length
                buffer = process.fill_buffer()
                one_second = position + freq
                end_pos = min(one_second, length)
                copy_size = end_pos - position
                if copy_size < one_second:
                    buffer = buffer[:copy_size]
                for f in audio_files:
                    f.buffer_write(buffer, dtype="float32")
                with audio_paths[0].open("rb") as primary_f:
                    primary_f.seek(0, 2)
                    file_size = primary_f.tell()
                    if file_size > max_file_size:
                        log.warning(
                            "Stopped writing at %r bytes (exceeded %r)",
                            file_size,
                            max_file_size,
                        )
                        break
                log.info(
                    "Rendered %r of %r (%.2f%%), %r bytes written",
                    position,
                    length,
                    percentage,
                    file_size,
                )
                position = end_pos
                await asyncio.sleep(0)
        finally:
            for f in audio_files:
                f.close()
    finally:
        process.kill()
Exemplo n.º 19
0
def readWave(audio_path,
             start_frame,
             end_frame,
             mono=True,
             sample_rate=None,
             clip=True):
    snd_file = SoundFile(audio_path, mode='r')
    inf = snd_file._info
    audio_sr = inf.samplerate

    snd_file.seek(start_frame)
    audio = snd_file.read(end_frame - start_frame, dtype='float32')
    snd_file.close()
    audio = audio.T  # Tuple to numpy, transpose axis to (channels, frames)

    # Convert to mono if desired
    if mono and len(audio.shape) > 1 and audio.shape[0] > 1:
        audio = np.mean(audio, axis=0)

    # Resample if needed
    if sample_rate is not None and sample_rate != audio_sr:
        audio = librosa.resample(audio,
                                 audio_sr,
                                 sample_rate,
                                 res_type="kaiser_fast")
        audio_sr = sample_rate

    # Clip to [-1,1] if desired
    if clip:
        audio = np.minimum(np.maximum(audio, -1.0), 1.0)

    return audio, audio_sr
Exemplo n.º 20
0
    def __init__(self,
                 file_name: str,
                 rate: int,
                 channels: int,
                 name: str = ""):
        """Creates an instance of a Encoder source with the given configuration

        Args:
            file_name (str): Input file name
            rate (int): The sample rate of the file in Hz
            channels (int): The number of channels
            name (str): Name of the element
        """
        super().__init__(name)
        self.__instance = SoundFile(file=file_name,
                                    mode='w',
                                    samplerate=rate,
                                    channels=channels)
Exemplo n.º 21
0
    def __init__(self, sf=None, **parameters):
        self.unroll_parameters(parameters)
        if isinstance(sf, str):
            sf = SoundFile(sf)

        self.sf = sf
        metadata = list(self._gen_metadata_from_sf(sf))
        self.extend_metadata(metadata)
        self.fetch_metadata_as_attrs()
Exemplo n.º 22
0
def create_soundfile():
    import numpy as np
    from io import BytesIO
    from soundfile import write, SoundFile
    bio = BytesIO()
    x = np.random.randn(10 * 2048)
    x /= np.max(np.abs(x))
    write(bio, x, 44100, format='WAV')
    bio.seek(0)
    return SoundFile(bio)
Exemplo n.º 23
0
def audio_recorder(final_file_name, temp_video_file, temp_audio_file):

    q = queue.Queue()

    def callback(indata, frames, time, status):
        q.put(indata.copy())

    device_info = query_devices(0, 'input')
    samplerate = int(device_info['default_samplerate'])
    initial_time = time()
    with SoundFile("Recorded Videos/" + temp_audio_file + ".wav",
                   mode='x',
                   samplerate=samplerate,
                   channels=2) as file:
        with InputStream(samplerate=samplerate,
                         device=0,
                         channels=2,
                         callback=callback):
            while not stop:
                file.write(q.get())

    print("счет1 ", count1)
    print("time()-initial_time = ", time() - initial_time)
    fps_real = count1 / (time() - initial_time)
    processing_condition_showing("Обработка")
    print("Действительный fps ", fps_real)

    changing_fps(fps_real, temp_video_file)

    #merging
    sound = AudioSegment.from_wav("Recorded Videos/" + temp_audio_file +
                                  ".wav")
    sound.export("Recorded Videos/" + temp_audio_file + ".mp3")

    clip = mpe.VideoFileClip("Recorded Videos/" + temp_video_file +
                             "_corrected.avi")
    audio = mpe.AudioFileClip("Recorded Videos/" + temp_audio_file + ".mp3")

    final_audio = mpe.CompositeAudioClip([audio])
    final_file = clip.set_audio(final_audio)

    final_file.write_videofile("Recorded Videos/" + final_file_name + ".mp4")

    print("Удаление временных файлов")
    if isfile("Recorded Videos/" + temp_audio_file + ".mp3"):
        remove("Recorded Videos/" + temp_audio_file + ".mp3")
    if isfile("Recorded Videos/" + temp_audio_file + ".wav"):
        remove("Recorded Videos/" + temp_audio_file + ".wav")
    if isfile("Recorded Videos/" + temp_video_file + ".avi"):
        remove("Recorded Videos/" + temp_video_file + ".avi")

    if isfile("Recorded Videos/" + temp_video_file + "_corrected.avi"):
        remove("Recorded Videos/" + temp_video_file + "_corrected.avi")
    processing_condition_showing("done")
Exemplo n.º 24
0
def save_wav(wav, count=-1):
    # print("Outputing wav file...")
    out_arr = wav.view(-1).cpu().numpy()
    fname = out_name + ".wav"
    import soundfile
    from soundfile import SoundFile
    try:
        with SoundFile(fname, mode="r+") as wav_file:
            wav_file.seek(0, soundfile.SEEK_END)
            wav_file.write(out_arr)
    except Exception as e:
        soundfile.write(fname, out_arr, samplerate=sample_rate, format="WAV")
Exemplo n.º 25
0
    def init_soundfile(self):
        "init_soundfile() - initialize flexible SoundFile audio interface"

        self.soundfileinfo = _SoundFileInfo(self.filename, verbose=True)
        self.data = None
        self.fileobj = SoundFile(self.filename)

        self.Fs = self.fileobj.samplerate
        self.samplesN = self.soundfileinfo.frames
        self.channels = self.soundfileinfo.channels
        self.format = self.soundfileinfo.format
        self.subtype = self.soundfileinfo.subtype
Exemplo n.º 26
0
def soundfile(hz=440, seconds=5., sr=44100.):
    bio = BytesIO()
    s = signal(hz, seconds, sr)
    with SoundFile(bio,
                   mode='w',
                   channels=2,
                   format='WAV',
                   subtype='PCM_16',
                   samplerate=int(sr)) as f:
        f.write(s)
    bio.seek(0)
    return s, HasUri(bio)
Exemplo n.º 27
0
    def __init__(self):
        super().__init__('audio_output', namespace='voice')

        # create topics
        self.audio_subscriber = self.create_subscription(
            Audio, 'audio_out', self.audio_listener, 10)

        # get node parameters
        self.declare_parameter('device', '')  # input audio device ID or name
        self.declare_parameter('sample_rate', 16000)  # sample rate (in Hz)
        self.declare_parameter('chunk_size',
                               4096)  # number of samples per buffer

        self.device_name = str(self.get_parameter('device').value)
        self.sample_rate = self.get_parameter('sample_rate').value
        self.chunk_size = self.get_parameter('chunk_size').value

        if self.device_name == '':
            raise ValueError(
                "must set the 'device' parameter to either an input audio device ID/name or the path to a .wav file"
            )

        self.get_logger().info(f'device={self.device_name}')
        self.get_logger().info(f'sample_rate={self.sample_rate}')
        self.get_logger().info(f'chunk_size={self.chunk_size}')

        # check if this is an audio device or a wav file
        file_ext = os.path.splitext(self.device_name)[1].lower()

        if file_ext == '.wav' or file_ext == '.wave':
            self.wav = SoundFile(self.device_name,
                                 mode='w',
                                 samplerate=self.sample_rate,
                                 channels=1)
            self.device = None
        else:
            self.wav = None
            self.device = AudioOutput(self.device_name,
                                      sample_rate=self.sample_rate,
                                      chunk_size=self.chunk_size)
Exemplo n.º 28
0
 def serialize(self, context):
     bio = BytesIO()
     samples = context.value
     with SoundFile(bio,
                    mode='w',
                    samplerate=samples.samples_per_second,
                    channels=samples.channels,
                    format='OGG',
                    subtype='VORBIS') as sf:
         for i in range(0, len(samples), samples.samples_per_second):
             sf.write(samples[i:i + samples.samples_per_second])
     bio.seek(0)
     return TempResult(bio.read(), 'audio/ogg')
Exemplo n.º 29
0
    def from_file(self, filename):
        """Load sound from an audio file."""
        if filename[-4:].lower() == '.lmp':
            self.data = readfile(filename)
        else:
            with SoundFile(filename) as file:
                # get sound data and convert to 8-bit unsigned mono
                sound = (file.read(dtype='int16') >> 8) + 128
                if file.channels > 1:
                    sound = np.mean(sound, axis=1)

                # create new format 3 sound
                self.from_raw(
                    sound.astype('uint8').tobytes(), 3, file.samplerate)
Exemplo n.º 30
0
    def encode(self, flo=None, fmt='WAV', subtype='PCM_16'):
        """
        Return audio samples encoded as bytes given a particular audio format

        Args:
            flo (file-like): A file-like object to write the bytes to.  If flo
                is not supplied, a new :class:`io.BytesIO` instance will be
                created and returned
            fmt (str): A libsndfile-friendly identifier for an audio encoding
                (detailed here: http://www.mega-nerd.com/libsndfile/api.html)
            subtype (str): A libsndfile-friendly identifier for an audio
                encoding subtype (detailed here:
                http://www.mega-nerd.com/libsndfile/api.html)

        Examples:
            >>> from zounds import SR11025, AudioSamples
            >>> import numpy as np
            >>> silence = np.zeros(11025*10)
            >>> samples = AudioSamples(silence, SR11025())
            >>> bio = samples.encode()
            >>> bio.read(10)
            'RIFFx]\\x03\\x00WA'
        """
        flo = flo or BytesIO()
        with SoundFile(
                flo,
                mode='w',
                channels=self.channels,
                format=fmt,
                subtype=subtype,
                samplerate=self.samples_per_second) as f:

            if fmt == 'OGG':
                # KLUDGE: Trying to write too-large chunks to an ogg file seems
                # to cause a segfault in libsndfile
                # KLUDGE: This logic is very similar to logic in the OggVorbis
                # processing node, and should probably be factored into a common
                # location
                factor = 20
                chunksize = self.samples_per_second * factor
                for i in range(0, len(self), chunksize):
                    chunk = self[i: i + chunksize]
                    f.write(chunk)
            else:
                # write everything in one chunk
                f.write(self)

        flo.seek(0)
        return flo
Exemplo n.º 31
0
def convert_mseed_to_flac(mseed_dir, mseed_files):
    for mseed_filename in mseed_files:
        filename, file_extension = os.path.splitext(mseed_filename)
        flac_file = os.path.join(mseed_dir, filename + '.flac')

        # If this flac file does not already exist, convert it.
        if not os.path.exists(flac_file):
            mseed_file = os.path.join(mseed_dir, mseed_filename)
            stream = obspy.read(mseed_file)

            with SoundFile(flac_file, 'w', 64000, 1,
                           subtype='PCM_24') as soundFile:
                soundFile.write(stream[0].data / MAX_MSEED_VALUE)

            log.info('Converted ' + mseed_file + ' to ' + flac_file)
Exemplo n.º 32
0
def analyze(filename):
    if wav_loader == 'pysoundfile':
        sf = SoundFile(filename)
        signal = sf.read()
        channels = sf.channels
        sample_rate = sf.samplerate
        samples = len(sf)
        file_format = sf.format_info + ' ' + sf.subtype_info
        sf.close()
    elif wav_loader == 'scikits.audiolab':
        sf = Sndfile(filename, 'r')
        signal = sf.read_frames(sf.nframes)
        channels = sf.channels
        sample_rate = sf.samplerate
        samples = sf.nframes
        file_format = sf.format
        sf.close()
    elif wav_loader == 'scipy.io.wavfile':
        sample_rate, signal = read(filename)
        try:
            channels = signal.shape[1]
        except IndexError:
            channels = 1
        samples = signal.shape[0]
        file_format = str(signal.dtype)

        # Scale common formats
        # Other bit depths (24, 20) are not handled by SciPy correctly.
        if file_format == 'int16':
            signal = signal.astype(float) / (2**15)
        elif file_format == 'uint8':
            signal = (signal.astype(float) - 128) / (2**7)
        elif file_format == 'int32':
            signal = signal.astype(float) / (2**31)
        elif file_format == 'float32':
            pass
        else:
            raise Exception("Don't know how to handle file "
                            "format {}".format(file_format))

    else:
        raise Exception("wav_loader has failed")

    header = 'dBFS values are relative to a full-scale square wave'

    if samples/sample_rate >= 1:
        length = str(samples/sample_rate) + ' seconds'
    else:
        length = str(samples/sample_rate*1000) + ' milliseconds'

    results = [
        "Using sound file backend '" + wav_loader + "'",
        'Properties for "' + filename + '"',
        str(file_format),
        'Channels:\t%d' % channels,
        'Sampling rate:\t%d Hz' % sample_rate,
        'Samples:\t%d' % samples,
        'Length: \t' + length,
        '-----------------',
        ]

    if channels == 1:
        # Monaural
        results += properties(signal, sample_rate)
    elif channels == 2:
        # Stereo
        if array_equal(signal[:, 0], signal[:, 1]):
            results += ['Left and Right channels are identical:']
            results += properties(signal[:, 0], sample_rate)
        else:
            results += ['Left channel:']
            results += properties(signal[:, 0], sample_rate)
            results += ['Right channel:']
            results += properties(signal[:, 1], sample_rate)
    else:
        # Multi-channel
        for ch_no, channel in enumerate(signal.transpose()):
            results += ['Channel %d:' % (ch_no + 1)]
            results += properties(channel, sample_rate)

    display(header, results)

    plot_histogram = False
    if plot_histogram:
        histogram(signal)