File size: 4,814 Bytes
c7e47b2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | #!/usr/bin/env python
"""
fill_tony_file.py
Fill in missing time frames in a Tony file, write the output to file.
Created by Rachel Bittner <rmb456@nyu.edu>
and Justin Salamon <justin.salamon@nyu.edu>
This code is released as part of the MedleyDB library for working
with the MedleyDB dataset: http://marl.smusic.nyu.edu/medleydb.
This code is a component of the work presented in the following publication:
R. Bittner, J. Salamon, M. Tierney, M. Mauch, C. Cannam and J. P. Bello,
"MedleyDB: A Multitrack Dataset for Annotation-Intensive MIR Research", in
15th International Society for Music Information Retrieval Conference,
Taipei, Taiwan, Oct. 2014.
"""
import os
import numpy as np
import csv
import argparse
HOP = 256.0 # Tony default hop size
FS = 44100.0 # Tony default output sample rate
def read_tony_file(fpath):
"""Read a tony-generated csv file.
Parameters
----------
fpath : str
Path to tony-generated csv file.
Returns
-------
tony_f0 : array
Array of tony-generated f0 values and corresponding time stamps.
"""
assert os.path.exists(fpath), "File path does not exist."
with open(fpath) as f_handle:
tony_f0 = []
linereader = csv.reader(f_handle)
for line in linereader:
tony_f0.append([float(val) for val in line[:2]])
return tony_f0
def get_time_stamps(total_duration):
"""Get a list of evenly spaced time stamps based on Tony's parameters.
Parameters
----------
total_duration : float
Length (in seconds) of annotation's corresponding audio file.
Returns
-------
time_stamps : array
Array of evenly spaced time stamps spanning the entire annotation.
"""
n_stamps = int(np.ceil((total_duration*FS)/HOP))
return np.arange(n_stamps)*(HOP/FS)
def make_blank_f0_sequence(total_duration):
"""Make a complete f0 sequence filled with 0's.
First column is time, second column is frequency.
Parameters
----------
total_duration : float
Length (in seconds) of annotation's corresponding audio file.
Returns
-------
f0_sequence : array
Array of filled in f0 values and corresponding time stamps.
"""
time_stamps = get_time_stamps(total_duration)
f0_sequence = np.zeros((len(time_stamps), 2))
f0_sequence[:, 0] = time_stamps
return f0_sequence
def sec_to_idx(time_in_seconds, sample_rate=FS, hop=HOP):
"""Compute the array index of a given time stamp.
Parameters
----------
time_in_seconds : float
Time stamp, in seconds.
sample_rate : float
Annotation sample rate.
hop : float
Annotation hop size.
Returns
-------
array_idx : int
Index of time stamp in filled f0 array.
"""
return int(np.round(time_in_seconds*sample_rate/hop))
def write_f0_to_csv(f0_sequence, output_file_path):
"""Write f0 sequence to a csv file.
Parameters
----------
f0_sequence : array
Filled f0 sequence with corresponding time stamps.
output_file_path : str
Path to save csv file.
"""
assert len(f0_sequence) != 0, "f0 sequence is empty."
with open(output_file_path, "wb") as fpath:
writer = csv.writer(fpath)
writer.writerows(f0_sequence)
def main(args):
"""Fill in missing time frames in a Tony file, write the output to file.
Parameters
----------
tony_csv_file : str
Path to tony-generated csv file.
output_file_path : str
Path to save location of filled-in file.
duration : float
Length (in seconds) of the annotation's corresponding audio file.
"""
tony_f0 = read_tony_file(args.tonyfile)
assert tony_f0, "Tony file does not exist."
start_idx = sec_to_idx(0)
end_idx = sec_to_idx(args.duration)
f0_sequence = make_blank_f0_sequence(args.duration)
for time_freq in tony_f0:
time = time_freq[0]
freq = time_freq[1]
time_idx = sec_to_idx(time)
if time_idx >= start_idx and time_idx < end_idx:
f0_sequence[time_idx][1] = freq
write_f0_to_csv(f0_sequence, args.outputfile)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Fill in missing time frames in a Tony file,"
"write the output to file.")
parser.add_argument("tonyfile",
type=str,
help="Path to tony ouput csv file.")
parser.add_argument("outputfile",
type=str,
help="Path to save location of filled in file.")
parser.add_argument("duration",
type=float,
help="Length (in seconds) of annotation audio file.")
main(parser.parse_args())
|