| 1 |
//--------------------------------------------------------------------- |
| 2 |
// name: feature-extract.ck |
| 3 |
// desc: sets up automated extraction over a training dataset |
| 4 |
// this example can be adapted to other labeled audio dataset |
| 5 |
// |
| 6 |
// version: need chuck version 1.5.0.0 or higher |
| 7 |
// sorting: part of ChAI (ChucK for AI) |
| 8 |
// |
| 9 |
// DATA: by default, this uses the GTZAN dataset: |
| 10 |
// download and unzip updated version: |
| 11 |
// https://chuck.stanford.edu/chai/data/gtzan/ |
| 12 |
// |
| 13 |
// USAGE: this is purely for feature extraction on training data, so... |
| 14 |
// run chuck in non-real-time mode (this actually can be much |
| 15 |
// faster than real-time mode, since it doesn't synch to audio): |
| 16 |
// |
| 17 |
// extract and print |
| 18 |
// > chuck --silent 2-extract.ck |
| 19 |
// |
| 20 |
// extract and write to FILE |
| 21 |
// > chuck --silent 2-extract.ck:FILE |
| 22 |
// |
| 23 |
// date: Spring 2023 |
| 24 |
// authors: Ge Wang (https://ccrma.stanford.edu/~ge/) |
| 25 |
// Yikai Li |
| 26 |
//--------------------------------------------------------------------- |
| 27 |
|
| 28 |
|
| 29 |
// output file (if empty, will print to console) |
| 30 |
"" => string OUTPUT_FILE; |
| 31 |
// get from arguments |
| 32 |
if( me.args() > 0 ) me.arg(0) => OUTPUT_FILE; |
| 33 |
|
| 34 |
// check |
| 35 |
if( Machine.silent() == false ) |
| 36 |
{ |
| 37 |
// print helpful message |
| 38 |
<<< "-----------------", "" >>>; |
| 39 |
<<< "[feature-extract]: chuck is currently running in REAL-TIME mode;", "" >>>; |
| 40 |
<<< "[feature-extract]: this step has no audio; may run much faster in SILENT mode!", "" >>>; |
| 41 |
<<< "[feature-extract]: to run in SILENT mode, restart chuck with --silent flag", "" >>>; |
| 42 |
<<< "-----------------", "" >>>; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
//--------------------------------------------------------------------- |
| 47 |
// analysis network -- this determines which feature will be extracted |
| 48 |
//--------------------------------------------------------------------- |
| 49 |
// audio input into a FFT |
| 50 |
SndBuf audioFile => FFT fft; |
| 51 |
// a thing for collecting multiple features into one vector |
| 52 |
FeatureCollector combo => blackhole; |
| 53 |
// add spectral feature: Centroid |
| 54 |
fft =^ Centroid centroid =^ combo; |
| 55 |
// add spectral feature: Flux |
| 56 |
fft =^ Flux flux =^ combo; |
| 57 |
// add spectral feature: RMS |
| 58 |
fft =^ RMS rms =^ combo; |
| 59 |
// add spectral feature: MFCC |
| 60 |
fft =^ MFCC mfcc =^ combo; |
| 61 |
|
| 62 |
|
| 63 |
//--------------------------------------------------------------------- |
| 64 |
// setting analysis parameters -- important for tuning your extraction |
| 65 |
//--------------------------------------------------------------------- |
| 66 |
// set number of coefficients in MFCC (how many we get out) |
| 67 |
// 13 is a commonly used value; using less here for printing |
| 68 |
20 => mfcc.numCoeffs; |
| 69 |
// set number of mel filters in MFCC |
| 70 |
10 => mfcc.numFilters; |
| 71 |
|
| 72 |
// do one .upchuck() so FeatureCollector knows how many total dimension |
| 73 |
combo.upchuck(); |
| 74 |
// get number of total feature dimensions |
| 75 |
combo.fvals().size() => int NUM_DIMENSIONS; |
| 76 |
|
| 77 |
// set FFT size |
| 78 |
2048 => fft.size; |
| 79 |
// set window type and size |
| 80 |
Windowing.hann(fft.size()) => fft.window; |
| 81 |
// our hop size (how often to perform analysis) |
| 82 |
2048::samp => dur HOP; |
| 83 |
|
| 84 |
|
| 85 |
//--------------------------------------------------------------------- |
| 86 |
// training data -- preparation specific to a train dataset |
| 87 |
//--------------------------------------------------------------------- |
| 88 |
// labels (and filename roots) |
| 89 |
["blues", "classical", "country", "disco", "hiphop", |
| 90 |
"jazz", "metal", "pop", "reggae", "rock"] @=> string labels[]; |
| 91 |
// how many per label |
| 92 |
100 => int NUM_EXAMPLES_PER_LABEL; |
| 93 |
// how much time to aggregate features for each file |
| 94 |
10::second => dur EXTRACT_TIME; |
| 95 |
// given EXTRACT_TIME and HOP, how many frames per file? |
| 96 |
(EXTRACT_TIME / HOP) $ int => int numFrames; |
| 97 |
// relative path |
| 98 |
"data/gtzan/genres_original/" => string PATH; |
| 99 |
|
| 100 |
// a feature frame |
| 101 |
float featureFrame[NUM_DIMENSIONS]; |
| 102 |
// how many input files |
| 103 |
0 => int NUM_FILES; |
| 104 |
|
| 105 |
// output reference, default is error stream (cherr) |
| 106 |
cherr @=> IO @ theOut; |
| 107 |
// instantiate |
| 108 |
FileIO fout; |
| 109 |
// output file |
| 110 |
if( OUTPUT_FILE != "" ) |
| 111 |
{ |
| 112 |
// print |
| 113 |
<<< "opening file for output:", OUTPUT_FILE >>>; |
| 114 |
// open |
| 115 |
fout.open( OUTPUT_FILE, FileIO.WRITE ); |
| 116 |
// test |
| 117 |
if( !fout.good() ) |
| 118 |
{ |
| 119 |
<<< "cannot open file for writing...", "" >>>; |
| 120 |
me.exit(); |
| 121 |
} |
| 122 |
// override |
| 123 |
fout @=> theOut; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
//--------------------------------------------------------------------- |
| 128 |
// extraction -- iterating over entire training dataset |
| 129 |
//--------------------------------------------------------------------- |
| 130 |
|
| 131 |
// filename |
| 132 |
string filename; |
| 133 |
// loop over labels |
| 134 |
for( int i; i < labels.cap(); i++) |
| 135 |
{ |
| 136 |
// get current label |
| 137 |
labels[i] => string label; |
| 138 |
// loop over examples under each label |
| 139 |
for( int j; j < NUM_EXAMPLES_PER_LABEL; j++ ) |
| 140 |
{ |
| 141 |
// construct filepath |
| 142 |
me.dir() + PATH + label + "/" + label + ".000" + (j<10?"0":"") + j + ".wav" => filename; |
| 143 |
// extract the file |
| 144 |
if( !extractFeatures( filename, label, theOut ) ) |
| 145 |
{ |
| 146 |
// issue warning |
| 147 |
cherr <= "PROBLEM during extraction: " <= filename <= IO.newline(); |
| 148 |
// bail out |
| 149 |
me.exit(); |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
//--------------------------------------------------------------------- |
| 156 |
// function: extract and print features from a single file |
| 157 |
//--------------------------------------------------------------------- |
| 158 |
fun int extractFeatures( string inputFilePath, string label, IO out ) |
| 159 |
{ |
| 160 |
// increment |
| 161 |
NUM_FILES++; |
| 162 |
// log |
| 163 |
cherr <= "[" <= NUM_FILES <= "] extracting features: " <= inputFilePath <= IO.newline(); |
| 164 |
|
| 165 |
// load by block to speed up IO |
| 166 |
2048 => audioFile.chunks; |
| 167 |
// read the audio file |
| 168 |
inputFilePath => audioFile.read; |
| 169 |
// zero out |
| 170 |
featureFrame.zero(); |
| 171 |
|
| 172 |
// let one FFT-size of time pass (to buffer) |
| 173 |
fft.size()::samp => now; |
| 174 |
// loop over frames |
| 175 |
for( int i; i < numFrames; i++ ) |
| 176 |
{ |
| 177 |
//------------------------------------------------------------- |
| 178 |
// a single upchuck() will trigger analysis on everything |
| 179 |
// connected upstream from combo via the upchuck operator (=^) |
| 180 |
// the total number of output dimensions is the sum of |
| 181 |
// dimensions of all the connected unit analyzers |
| 182 |
//------------------------------------------------------------- |
| 183 |
combo.upchuck(); |
| 184 |
// for each dimension |
| 185 |
for( int d; d < NUM_DIMENSIONS; d++ ) |
| 186 |
{ |
| 187 |
// copy |
| 188 |
combo.fval(d) +=> featureFrame[d]; |
| 189 |
} |
| 190 |
// advance time |
| 191 |
HOP => now; |
| 192 |
} |
| 193 |
|
| 194 |
//------------------------------------------------------------- |
| 195 |
// average into a single feature vector per file |
| 196 |
// NOTE: this can be easily modified to N feature vectors |
| 197 |
// spread out over the length of an audio file; for now |
| 198 |
// we will just do one feature vector per file |
| 199 |
//------------------------------------------------------------- |
| 200 |
for( int d; d < NUM_DIMENSIONS; d++ ) |
| 201 |
{ |
| 202 |
// average by total number of frames |
| 203 |
numFrames /=> featureFrame[d]; |
| 204 |
// print the MFCC results |
| 205 |
out <= featureFrame[d] <= " "; |
| 206 |
} |
| 207 |
// print label name and endline |
| 208 |
out <= label <= IO.newline(); |
| 209 |
|
| 210 |
// done |
| 211 |
return true; |
| 212 |
} |