Esempio n. 1
0
def read_wavefile_metadata(filepath):
    """ Read AudioMetadata of a WAV file without reading all of it

    Heavily depends on `scipy.io.wavfile`.

    # Arguments
        filepath: str: full path to the WAV file

    # Returns
        meta: AudioMetadata object (namedtuple): with information as:

    # Reference
        https://github.com/scipy/scipy/blob/master/scipy/io/wavfile.py#L116

    """
    import struct
    from scipy.io.wavfile import _read_riff_chunk, _read_fmt_chunk
    from scipy.io.wavfile import _skip_unknown_chunk

    fid = open(filepath, 'rb')

    def _read_n_samples(fid, big_endian, bits):
        if big_endian:
            fmt = '>i'
        else:
            fmt = '<i'

        size = struct.unpack(fmt, fid.read(4))[0]

        return size // (bits // 8)  # indicates total number of samples

    try:
        size, is_big_endian = _read_riff_chunk(fid)

        while fid.tell() < size:
            chunk = fid.read(4)
            if chunk == b'fmt ':
                fmt_chunk = _read_fmt_chunk(fid, is_big_endian)
                channels, samplerate = fmt_chunk[2:4]  # info relevant to us
                bits = fmt_chunk[6]
            elif chunk == b'data':
                n_samples = _read_n_samples(fid, is_big_endian, bits)
                break  # NOTE: break as now we have all info we need
            elif chunk in (b'JUNK', b'Fake', b'LIST', b'fact'):
                _skip_unknown_chunk(fid, is_big_endian)
            else:
                warnings.warn("Chunk (non-data) not understood, skipping it.",
                              RuntimeWarning)
                _skip_unknown_chunk(fid, is_big_endian)
    finally:  # always close
        fid.close()

    return AudioMetadata(
        filepath=filepath,
        format='wav',
        samplerate=samplerate,
        nchannels=channels,
        seconds=(n_samples // channels) / samplerate,
        nsamples=n_samples // channels  # for one channel
    )
Esempio n. 2
0
def read(filename):
    """ Reads common .wav file formats used in bat or bird experiments.

    Mainly based on scipy.io.wavfile functions, to which were added some
    robustness to support Petterson file format.
    """
    with open(filename, 'rb') as fid:
        with mmap.mmap(fid.fileno(), 0, access=mmap.ACCESS_READ) as mfid:
            # Petterson wave file have multiple RIFF headers, find the last one
            rindex = mfid.rfind(b'RIFF')
            if rindex == -1:
                raise ValueError('Missing RIFF tag in wav file.')

            mfid.seek(rindex)
            fsize = _read_riff_chunk(mfid)
            if mfid.read(4) != b'fmt ':
                raise ValueError('Missing format tag in wav file.')

            # Ignore scipy warning on unknown wave file format
            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                size, comp, noc, fs, sbytes, ba, bits = _read_fmt_chunk(mfid, is_big_endian=False)

            if mfid.read(4) != b'data':
                raise ValueError('Missing data tag in wav file.')

            x = _read_data_chunk(mfid, comp, noc, bits, is_big_endian=False, mmap=False)
    return fs, x
Esempio n. 3
0
def readmarkers(input_dir, mmap=False):
    for file in glob.glob(input_dir+audio_type):
        file_name=((file.split('/')[-1]).split('.'))[0]
        write_file=open(output_dir+'/'+file_name+'_cue_info.txt', 'w')
        writer = csv.writer(write_file)
        if hasattr(file, 'read'):
            fid = file
        else:
            fid = open(file, 'rb')
        fsize = s._read_riff_chunk(fid)
        cue = []
        while (fid.tell() < fsize[0]):
            chunk_id = fid.read(4)
            if chunk_id == b'cue ':
                size, numcue = struct.unpack('<ii', fid.read(8))
                if file == '/Users/spanta/Desktop/jon_code_test/new_files/drive-download-20200107T220609Z-001/Udmurt_N2UDMIBT_Completed/N2_UDM_IBT_029_MRK_01_VOX.wav':
                    print(file,numcue)
                for c in range(numcue):
                    id, position, datachunkid, chunkstart, blockstart, sampleoffset = struct.unpack('<iiiiii',
                                                                                                    fid.read(24))
                    cue.append(position)
            else:
                s._skip_unknown_chunk(fid, False)
        fid.close()
        cue=[round(x/44100,3) for x in cue]

        if file=='/Users/spanta/Desktop/jon_code_test/new_files/drive-download-20200107T220609Z-001/Udmurt_N2UDMIBT_Completed/N2_UDM_IBT_029_MRK_01_VOX.wav': print(cue)
        corrected_cue=list()
        corrected_cue.append(cue[0])
        #Check values in cue info so that the cues are not misplaced
        for i in range1(1,len(cue)-1):
            if cue[i]>cue[i-1]: corrected_cue.append(cue[i])


        #Format the cue info. like aud format
        final_cue=list()
        # print(file,corrected_cue)
        for i,each_cue in enumerate(corrected_cue):

            if args.contains_verses:
                if i==0:

                    final_cue.append('0.000' + '\t' + str(each_cue) + '\t' + str(i))
                    final_cue.append(str(each_cue)+'\t'+str(corrected_cue[i+1])+'\t'+str(i+1))
                elif i>1:final_cue.append(str(corrected_cue[i-1]) + '\t' + str(each_cue)+'\t'+str(i))
            else:
                if i == 0:
                    final_cue.append('0.000' + '\t' + str(each_cue))
                else:
                    final_cue.append(str(corrected_cue[i - 1]) + '\t' + str(each_cue))

            if i<=1: print(final_cue)

        writer.writerows(zip(final_cue))
        write_file.close()
Esempio n. 4
0
def read_wav_metadata(stream):
    """
    Reads a stream that contains an wav signal. Returns a tuple containing sampling rate, bit depth, number of
    channels, user data, and data in that order. Before returning it closes the stream.
    :param stream: An instance of a class derived from io.IOBase.
        The stream from which to read. A call to its readable() and seekable() methods must return True. Its
        contents must be in WAV format, otherwise an exception is raised.
    """
    # read the first chunk (the riff chunk),
    # contains the size and a way to know this is a WAV stream
    fsize = wavfile._read_riff_chunk(stream)

    noc = 1
    bits = 16
    rate = 44100
    userData = ''
    data = None

    # read each chunk
    while stream.tell() < fsize:
        chunk_id = stream.read(4)

        if chunk_id == asbytes('fmt '):
            # read fmt chunk, contains all metadata
            size, comp, noc, rate, sbytes, ba, bits = wavfile._read_fmt_chunk(
                stream)
        elif chunk_id == asbytes('data'):
            # read data chunk
            if wavfile._big_endian:
                fmt = '>i'
            else:
                fmt = '<i'
            size = struct.unpack(fmt, stream.read(4))[0]
            stream.seek(size, SEEK_CUR)
        else:
            # ignore unknown chunk
            dt = stream.read(4)
            if wavfile._big_endian:
                fmt = '>i'
            else:
                fmt = '<i'
            size = struct.unpack(fmt, dt)[0]
            stream.seek(size, SEEK_CUR)

    stream.close()
    return rate, bits, noc, userData, size
Esempio n. 5
0
    def __init__(self, files_path, audio_dir, transform=None):
        """
        Args:
            files_path (string): Path to the .txt file which the address of files are saved in it.
            root_dir (string): Directory with all the audio files.
            transform (callable, optional): Optional transform to be applied
                on a sample.
        """

        # self.sound_files = [x.strip() for x in content]
        self.audio_dir = audio_dir
        self.transform = transform

        # Open the .txt file and create a list from each line.
        with open(files_path, 'r') as f:
            content = f.readlines()
        # you may also want to remove whitespace characters like `\n` at the end of each line
        list_files = []
        for x in content:
            sound_file_path = os.path.join(self.audio_dir,
                                           x.strip().split()[1])
            try:
                with open(sound_file_path, 'rb') as f:
                    riff_size, _ = wav._read_riff_chunk(f)
                    file_size = os.path.getsize(sound_file_path)

                # Assertion error.
                # assert riff_size == file_size and os.path.getsize(sound_file_path) > 1000, "Bad file!"
                if riff_size == file_size and os.path.getsize(
                        sound_file_path) > 16000:
                    list_files.append(x.strip())
                else:
                    pass
                    # Add to list if file is OK!

            except OSError as err:
                print("OS error: {0}".format(err))
            except ValueError:
                print('file %s is corrupted!' % sound_file_path)
                # except:
                #     print("Unexpected error:", sys.exc_info()[0])
                #     raise

        # Save the correct and healthy sound files to a list.
        self.sound_files = list_files
Esempio n. 6
0
    def __init__(self, files_path, audio_dir, transform=None):
        """
        Args:
            files_path (string): Path to the .txt file which the address of files are saved in it.
            root_dir (string): Directory with all the audio files.
            transform (callable, optional): Optional transform to be applied
                on a sample.
        """

        # self.sound_files = [x.strip() for x in content]
        self.audio_dir = audio_dir
        self.transform = transform

        # Open the .txt file and create a list from each line.
        with open(files_path, 'r') as f:
            content = f.readlines()
        # you may also want to remove whitespace characters like `\n` at the end of each line
        list_files = []
        for x in content:
            sound_file_path = os.path.join(self.audio_dir, x.strip().split()[1])
            try:
                with open(sound_file_path, 'rb') as f:
                    riff_size, _ = wav._read_riff_chunk(f)
                    file_size = os.path.getsize(sound_file_path)

                # Assertion error.
                assert riff_size == file_size and os.path.getsize(sound_file_path) > 1000, "Bad file!"

                # Add to list if file is OK!
                list_files.append(x.strip())
            except OSError as err:
                print("OS error: {0}".format(err))
            except ValueError:
                print('file %s is corrupted!' % sound_file_path)
            # except:
            #     print("Unexpected error:", sys.exc_info()[0])
            #     raise

        # Save the correct and healthy sound files to a list.
        self.sound_files = list_files
Esempio n. 7
0
    def check_dataset(self, _dict):
        total_count = sum(len(v) for v in _dict.values())
        current_count = 0
        _checked_dict = dict()

        for speaker, data in _dict.items():
            temp_data_list = list()

            filename_w_ext = os.path.basename(speaker)
            filename, file_extension = os.path.splitext(filename_w_ext)

            for file in data:
                try:
                    with open(file, 'rb') as f:
                        riff_size, _ = wavfile._read_riff_chunk(f)
                        file_size = os.path.getsize(file)

                    # Assertion error.
                    assert riff_size == file_size and os.path.getsize(
                        file) > 1000, "Bad file!"

                    temp_data_list.append(file)
                except OSError as err:
                    print('OS error: {0}'.format(err))

                except ValueError:
                    print('file {0} is corrupted!'.format(file))

                self.printProgressBar(current_count + 1,
                                      total_count,
                                      prefix='Progress (Checking items):',
                                      suffix='Complete',
                                      length=50)
                current_count += 1

            _checked_dict[filename] = temp_data_list
        return _checked_dict
Esempio n. 8
0
    def __init__(self, files_path, audio_dir, transform=None):
        """
        Args:
            files_path (string): Path to the .txt file which the address of files are saved in it.
            root_dir (string): Directory with all the audio files.
            transform (callable, optional): Optional transform to be applied
                on a sample.
        """

        # self.sound_files = [x.strip() for x in content]
        self.audio_dir = audio_dir
        self.transform = transform

        # Open the .txt file and create a list from each line.
        with open(files_path, 'r') as f:
            content = f.readlines()
        # you may also want to remove whitespace characters like `\n` at the end of each line
        list_files = []
        previous_label = None
        list_files_each_class = []

        # this loop go through the files and from each class it chooses one sample at random.
        for x in content:

            current_label = x.strip().split()[0]
            if previous_label is None:
                previous_label = current_label

            if current_label == previous_label:
                # Get the path to the sound file
                sound_file_path = os.path.join(self.audio_dir,
                                               x.strip().split()[1])

                try:
                    ####### Check each file #####
                    with open(sound_file_path, 'rb') as f:
                        riff_size, _ = wav._read_riff_chunk(f)
                        file_size = os.path.getsize(sound_file_path)

                    # Assertion error.
                    assert riff_size == file_size and os.path.getsize(
                        sound_file_path) > 1000, "Bad file!"

                    # Add to list if file is OK!
                    list_files_each_class.append(x.strip())
                except:
                    print('file %s is corrupted!' % sound_file_path)

            else:

                list_files.append(random.choice(list_files_each_class))
                list_files_each_class[:] = []

                # Get the path to the sound file
                sound_file_path = os.path.join(self.audio_dir,
                                               x.strip().split()[1])

                try:
                    ####### Check each file #####
                    with open(sound_file_path, 'rb') as f:
                        riff_size, _ = wav._read_riff_chunk(f)
                        file_size = os.path.getsize(sound_file_path)

                    # Assertion error.
                    assert riff_size == file_size and os.path.getsize(
                        sound_file_path) > 1000, "Bad file!"

                    # Add to list if file is OK!
                    list_files_each_class.append(x.strip())
                except:
                    print('file %s is corrupted!' % sound_file_path)

                # Reseting
                previous_label = current_label

        # For last class
        list_files.append(random.choice(list_files_each_class))
        list_files_each_class[:] = []

        # Save the correct and healthy sound files to a list.
        self.sound_files = list_files
def readmarkers(input_dir, mmap=False):
    df = (pd.read_csv(aeneas_dir + '/' + 'aeneas.csv',
                      encoding='utf-8')).astype(str)
    chapter_list = df['chapter'].unique()

    for each_chapter in chapter_list:
        target_list = df[target][df['chapter'] == str(each_chapter)]
        uniq_target = target_list.unique()

        print((input_dir + '/*' + book_find_string + '*' + '0' +
               str(each_chapter) + audio_type))
        if float(each_chapter) < 10:
            #Get audio
            file = glob.glob(input_dir + '/*' + book_find_string + '*' + '0' +
                             str(each_chapter) + audio_type)[0]

        else:
            #Get audio
            file = glob.glob(input_dir + '/*' + book_find_string + '_' +
                             str(each_chapter) + audio_type)[0]

        #Get verse indices from aeneas.csv
        for each_target in uniq_target:
            indices = [
                i for i, val in enumerate(target_list) if val == each_target
            ]

        # -------------------------Start of cue info extraction------------------------------------------
        file_name = ((file.split('/')[-1]).split('.'))[0]
        write_file = open(output_dir + '/' + file_name + '_cue_info.txt', 'w')
        writer = csv.writer(write_file)
        if hasattr(file, 'read'):
            fid = file
        else:
            fid = open(file, 'rb')
        fsize = s._read_riff_chunk(fid)
        cue = []
        while (fid.tell() < fsize[0]):
            chunk_id = fid.read(4)
            if chunk_id == b'cue ':
                size, numcue = struct.unpack('<ii', fid.read(8))
                for c in range(numcue):
                    id, position, datachunkid, chunkstart, blockstart, sampleoffset = struct.unpack(
                        '<iiiiii', fid.read(24))
                    cue.append(position)
            else:
                s._skip_unknown_chunk(fid, False)
        fid.close()
        cue = [round(x / 44100, 3) for x in cue]

        corrected_cue = list()
        corrected_cue.append(cue[0])
        # Check values in cue info so that the cues are not misplaced
        for i in range1(1, len(cue) - 1):
            if cue[i] > cue[i - 1]: corrected_cue.append(cue[i])

        # Format the cue info. like aud format
        final_cue = list()
        # print(file,corrected_cue)
        for i, each_cue in enumerate(corrected_cue):

            if args.is_verses:
                if i == 0:

                    final_cue.append('0.000' + '\t' + str(each_cue) + '\t' +
                                     str(i))
                    final_cue.append(
                        str(each_cue) + '\t' + str(corrected_cue[i + 1]) +
                        '\t' + str(i + 1))
                elif i > 1:
                    final_cue.append(
                        str(corrected_cue[i - 1]) + '\t' + str(each_cue) +
                        '\t' + str(i))
            else:
                if i == 0:
                    final_cue.append('0.000' + '\t' + str(each_cue))
                else:
                    final_cue.append(
                        str(corrected_cue[i - 1]) + '\t' + str(each_cue))

            if i <= 1: print(final_cue)

        writer.writerows(zip(final_cue))
        write_file.close()
Esempio n. 10
0
from scipy.io.wavfile import _read_riff_chunk
from os.path import getsize

# filename = "..\\Emotion Audio\\01_Neatral\\03-01-01-01-01-01-01.wav"
filename = "C:\\Users\\Tyler\\AppData\\Local\\Programs\\Python\\Python36\\Lib\\site-packages\\pyAudioAnalysis\\data\\speechEmotion\\00.wav"
with open(filename, 'rb') as f:
    riff_size, _ = _read_riff_chunk(f)

print('RIFF size: {}'.format(riff_size))
print('os size:   {}'.format(getsize(filename)))