Exemple #1
0
def create_text_grid(text_grid_path, label, num_of_frames):
    """
    create TextGrid files from the labels_path file
    :param num_of_frames:
    :param label:
    :param text_grid_path:
    """

    # should be different for every file format
    FRAME_RATE = 10
    MILLISEC_2_SEC = 0.001
    FRAME_CONCAT_ONSET = 0
    FRAME_CONCAT_OFFSET = 0
    v_onset = (label[0] + FRAME_CONCAT_ONSET)
    v_offset = (label[1] + FRAME_CONCAT_OFFSET)
    length = num_of_frames * FRAME_RATE * MILLISEC_2_SEC

    # build TextGrid
    text_grid = TextGrid()
    vowels_tier = IntervalTier(name='Duration', minTime=0.0, maxTime=float(length))
    vowels_tier.addInterval(Interval(0, float(v_onset) * FRAME_RATE * MILLISEC_2_SEC, ""))
    vowels_tier.addInterval(
            Interval(float(v_onset) * FRAME_RATE * MILLISEC_2_SEC, float(v_offset) * FRAME_RATE * MILLISEC_2_SEC, ""))
    vowels_tier.addInterval(Interval(float(v_offset) * FRAME_RATE * MILLISEC_2_SEC, float(length), ""))

    text_grid.append(vowels_tier)
    text_grid.write(text_grid_path)
Exemple #2
0
def create_text_grid(text_grid_path, label, num_of_frames):
    """
    create TextGrid files from the labels_path file
    :param num_of_frames:
    :param label:
    :param text_grid_path:
    """

    # should be different for every file format
    FRAME_RATE = 10
    MILLISEC_2_SEC = 0.001
    FRAME_CONCAT_ONSET = 0
    FRAME_CONCAT_OFFSET = 0
    v_onset = (label[0] + FRAME_CONCAT_ONSET)
    v_offset = (label[1] + FRAME_CONCAT_OFFSET)
    length = num_of_frames * FRAME_RATE * MILLISEC_2_SEC

    # build TextGrid
    text_grid = TextGrid()
    vowels_tier = IntervalTier(name='Duration',
                               minTime=0.0,
                               maxTime=float(length))
    vowels_tier.addInterval(
        Interval(0,
                 float(v_onset) * FRAME_RATE * MILLISEC_2_SEC, ""))
    vowels_tier.addInterval(
        Interval(
            float(v_onset) * FRAME_RATE * MILLISEC_2_SEC,
            float(v_offset) * FRAME_RATE * MILLISEC_2_SEC, ""))
    vowels_tier.addInterval(
        Interval(
            float(v_offset) * FRAME_RATE * MILLISEC_2_SEC, float(length), ""))

    text_grid.append(vowels_tier)
    text_grid.write(text_grid_path)
Exemple #3
0
def create_db(audio_path, textgrid_path, output_path):
    voiced_path = output_path + 'voiced/'
    prevoiced_path = output_path + 'prevoiced/'
    c = 0
    for item in os.listdir(audio_path):
        tg_file_path = textgrid_path + item.replace('.wav', '.TextGrid')
        if item.endswith('.wav') and os.path.exists(tg_file_path):
            c += 1
            print('Processing item: %d, file name: %s' % (c, item))

            tg = TextGrid()
            tg.read(tg_file_path)
            tier = tg._TextGrid__tiers[1]

            print('Creating output dirs ...')
            # create the relevant dirs
            if not os.path.exists(voiced_path):
                os.mkdir(voiced_path)
            if not os.path.exists(prevoiced_path):
                os.mkdir(prevoiced_path)

            for i, interval in enumerate(tier._IntervalTier__intervals):
                if 'ne' in interval._Interval__mark:
                    # gap = 0.02
                    start_vot = interval._Interval__xmin
                    end_vot = interval._Interval__xmax
                    gap = round((end_vot - start_vot) * 2, 2)
                    gap = max(0.05, gap)
                    start = max(0, start_vot - gap)
                    end = end_vot + gap
                    output_name = os.path.abspath(prevoiced_path) + '/' + str(
                        i) + '_' + item
                    utils.crop_wav(os.path.abspath(audio_path + item), start,
                                   end, output_name)

                    # write the text grid
                    length = end - start
                    new_tg = TextGrid()
                    vot_tier = IntervalTier(name='VOT',
                                            xmin=0.0,
                                            xmax=float(length))
                    vot_tier.append(Interval(0, start_vot - start, ""))
                    vot_tier.append(
                        Interval(start_vot - start, end_vot - start, "neg"))
                    vot_tier.append(
                        Interval(end_vot - start, float(length), ""))

                    new_tg.append(vot_tier)
                    new_tg.write(output_name.replace('.wav', '.TextGrid'))

                elif 'v' in interval._Interval__mark:
                    # gap = 0.02
                    start_vot = interval._Interval__xmin
                    end_vot = interval._Interval__xmax
                    gap = round((end_vot - start_vot) * 2, 2)
                    gap = max(0.05, gap)
                    start = max(0, start_vot - gap)
                    end = end_vot + gap
                    output_name = os.path.abspath(voiced_path) + '/' + str(
                        i) + '_' + item
                    utils.crop_wav(os.path.abspath(audio_path + item), start,
                                   end, output_name)

                    # write the text grid
                    length = end - start
                    new_tg = TextGrid()
                    vot_tier = IntervalTier(name='VOT',
                                            xmin=0.0,
                                            xmax=float(length))
                    vot_tier.append(Interval(0, start_vot - start, ""))
                    vot_tier.append(
                        Interval(start_vot - start, end_vot - start, "v"))
                    vot_tier.append(
                        Interval(end_vot - start, float(length), ""))

                    new_tg.append(vot_tier)
                    new_tg.write(output_name.replace('.wav', '.TextGrid'))
            print('Done.')
    return None
Exemple #4
0
        end = t._TextGrid__xmax
        length = end - start

        # ========= merge and save ========= #
        text_grid = TextGrid()

        # == DCM == #
        onset = orig_manual_annotations[i][1] - (manual_annotations[i][1] -
                                                 dcm_annotations[i][1])
        offset = orig_manual_annotations[i][2] - (manual_annotations[i][2] -
                                                  dcm_annotations[i][2]) - 0.01
        dcm_tier = IntervalTier(name='DCM', xmin=0.0, xmax=float(length))
        dcm_tier.append(Interval(0, float(onset), ""))
        dcm_tier.append(Interval(float(onset), float(offset), "vowel"))
        dcm_tier.append(Interval(float(offset), float(length), ""))
        text_grid.append(dcm_tier)

        # == DCM NC == #
        onset = orig_manual_annotations[i][1] - (manual_annotations[i][1] -
                                                 dcm_nc_annotations[i][1])
        offset = orig_manual_annotations[i][2] - (
            manual_annotations[i][2] - dcm_nc_annotations[i][2]) - 0.01
        dcm_nc_tier = IntervalTier(name='DCM-NC', xmin=0.0, xmax=float(length))
        dcm_nc_tier.append(Interval(0, float(onset), ""))
        dcm_nc_tier.append(Interval(float(onset), float(offset), "vowel"))
        dcm_nc_tier.append(Interval(float(offset), float(length), ""))
        text_grid.append(dcm_nc_tier)

        # == FAVE == #
        onset = orig_manual_annotations[i][1] - (manual_annotations[i][1] -
                                                 fave_annotations[i][1])
        t.read(manual_path_orig+orig_manual_annotations[i][0])
        start = t._TextGrid__xmin
        end = t._TextGrid__xmax
        length = end - start

        # ========= merge and save ========= #
        text_grid = TextGrid()

        # == DCM == #
        onset = orig_manual_annotations[i][1] - (manual_annotations[i][1] - dcm_annotations[i][1])
        offset = orig_manual_annotations[i][2] - (manual_annotations[i][2] - dcm_annotations[i][2]) - 0.01
        dcm_tier = IntervalTier(name='DCM', xmin=0.0, xmax=float(length))
        dcm_tier.append(Interval(0, float(onset), ""))
        dcm_tier.append(Interval(float(onset), float(offset), "vowel"))
        dcm_tier.append(Interval(float(offset), float(length), ""))
        text_grid.append(dcm_tier)

        # == DCM NC == #
        onset = orig_manual_annotations[i][1] - (manual_annotations[i][1] - dcm_nc_annotations[i][1])
        offset = orig_manual_annotations[i][2] - (manual_annotations[i][2] - dcm_nc_annotations[i][2]) - 0.01
        dcm_nc_tier = IntervalTier(name='DCM-NC', xmin=0.0, xmax=float(length))
        dcm_nc_tier.append(Interval(0, float(onset), ""))
        dcm_nc_tier.append(Interval(float(onset), float(offset), "vowel"))
        dcm_nc_tier.append(Interval(float(offset), float(length), ""))
        text_grid.append(dcm_nc_tier)

        # == FAVE == #
        onset = orig_manual_annotations[i][1] - (manual_annotations[i][1] - fave_annotations[i][1])
        offset = orig_manual_annotations[i][2] - (manual_annotations[i][2] - fave_annotations[i][2]) - 0.01
        fave_tier = IntervalTier(name='FAVE', xmin=0.0, xmax=float(length))
        fave_tier.append(Interval(0, float(onset), ""))
def create_db(audio_path, textgrid_path, output_path):
    voiced_path = output_path+'voiced/'
    prevoiced_path = output_path+'prevoiced/'
    c = 0
    for item in os.listdir(audio_path):
        tg_file_path = textgrid_path + item.replace('.wav', '.TextGrid')
        if item.endswith('.wav') and os.path.exists(tg_file_path):
            c += 1
            print('Processing item: %d, file name: %s' % (c, item))

            tg = TextGrid()
            tg.read(tg_file_path)
            tier = tg._TextGrid__tiers[1]

            print('Creating output dirs ...')
            # create the relevant dirs
            if not os.path.exists(voiced_path):
                os.mkdir(voiced_path)
            if not os.path.exists(prevoiced_path):
                os.mkdir(prevoiced_path)

            for i, interval in enumerate(tier._IntervalTier__intervals):
                if 'ne' in interval._Interval__mark:
                    # gap = 0.02
                    start_vot = interval._Interval__xmin
                    end_vot = interval._Interval__xmax
                    gap = round((end_vot - start_vot)*2, 2)
                    gap = max(0.05, gap)
                    start = max(0, start_vot - gap)
                    end = end_vot + gap
                    output_name = os.path.abspath(prevoiced_path) + '/' + str(i) + '_' + item
                    utils.crop_wav(os.path.abspath(audio_path + item), start, end,
                                   output_name)

                    # write the text grid
                    length = end - start
                    new_tg = TextGrid()
                    vot_tier = IntervalTier(name='VOT', xmin=0.0, xmax=float(length))
                    vot_tier.append(Interval(0, start_vot - start, ""))
                    vot_tier.append(Interval(start_vot - start, end_vot - start, "neg"))
                    vot_tier.append(Interval(end_vot - start, float(length), ""))

                    new_tg.append(vot_tier)
                    new_tg.write(output_name.replace('.wav', '.TextGrid'))

                elif 'v' in interval._Interval__mark:
                    # gap = 0.02
                    start_vot = interval._Interval__xmin
                    end_vot = interval._Interval__xmax
                    gap = round((end_vot - start_vot)*2, 2)
                    gap = max(0.05, gap)
                    start = max(0, start_vot - gap)
                    end = end_vot + gap
                    output_name = os.path.abspath(voiced_path) + '/' + str(i) + '_' + item
                    utils.crop_wav(os.path.abspath(audio_path + item), start, end,
                                   output_name)

                    # write the text grid
                    length = end - start
                    new_tg = TextGrid()
                    vot_tier = IntervalTier(name='VOT', xmin=0.0, xmax=float(length))
                    vot_tier.append(Interval(0, start_vot - start, ""))
                    vot_tier.append(Interval(start_vot - start, end_vot - start, "v"))
                    vot_tier.append(Interval(end_vot - start, float(length), ""))

                    new_tg.append(vot_tier)
                    new_tg.write(output_name.replace('.wav', '.TextGrid'))
            print('Done.')
    return None