MCPcopy Create free account
hub / github.com/TheWaveWarden/odin2 / writeSampleTableToFile

Method writeSampleTableToFile

Source/Utilities.cpp:1209–1339  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1207}
1208
1209void Utilities::writeSampleTableToFile(const std::string &p_filename) {
1210
1211 // load in data
1212 std::string filename = "/home/frot/odin2/samples/" + p_filename;
1213 File file(filename);
1214 WavAudioFormat wavAudioFormat;
1215 AudioFormatReader *audioFormatReader = wavAudioFormat.createReaderFor(file.createInputStream().get(), true);
1216 float *wavData[2]; // new int *[3];
1217 wavData[0] = new float[audioFormatReader->lengthInSamples];
1218 wavData[1] = new float[audioFormatReader->lengthInSamples];
1219 audioFormatReader->read(wavData, 2, 0, audioFormatReader->lengthInSamples);
1220 int n_samples = audioFormatReader->lengthInSamples;
1221#define WAVE wavData[0]
1222
1223 // remove offsets from beginning start by adding linear * cos function
1224 float difference = WAVE[n_samples - 1] - WAVE[0];
1225
1226 for (int i = 0; i < n_samples; ++i) {
1227 WAVE[i] -= difference * (cos((2 * M_PI * i) / (float)n_samples)) * (float)i / (float)(n_samples);
1228 }
1229
1230 // CREATE WAVEFILE
1231
1232 float wavedraw_coefficients[SIN_AND_COS][NUMBER_OF_HARMONICS];
1233
1234 float step_width = 2 * PI / (float)n_samples;
1235
1236 for (int harmonic = 1; harmonic < NUMBER_OF_HARMONICS; ++harmonic) {
1237
1238 float coeff_sine = 0.f;
1239 float coeff_cosine = 0.f;
1240
1241 for (int segment = 0; segment < n_samples; ++segment) {
1242 // use either const sections or linear sections
1243
1244 // wrap function value at end to start
1245 float segment_end_value = (segment == n_samples - 1) ? WAVE[0] : WAVE[segment + 1];
1246
1247 coeff_sine += lin_segment_one_overtone_sine(
1248 segment * step_width, (segment + 1) * step_width, WAVE[segment], segment_end_value, harmonic);
1249 coeff_cosine += lin_segment_one_overtone_cosine(
1250 segment * step_width, (segment + 1) * step_width, WAVE[segment], segment_end_value, harmonic);
1251 }
1252 wavedraw_coefficients[0][harmonic] = coeff_sine;
1253 wavedraw_coefficients[1][harmonic] = coeff_cosine;
1254 }
1255
1256 // now create the wavetable from the fourier coefficients
1257 double seed_freq = 27.5; // A0
1258 float max = 0.f;
1259 float wavedraw_tables[SUBTABLES_PER_WAVETABLE][WAVETABLE_LENGTH] = {0};
1260 float p_samplerate = 44100;
1261
1262 // loop over subtables
1263 for (int index_sub_table = 0; index_sub_table < SUBTABLES_PER_WAVETABLE; ++index_sub_table) {
1264
1265 // how many harmonics are needed for this subtable
1266 int number_of_harmonics = (int)((p_samplerate * 0.5f / seed_freq) - 1);

Callers

nothing calls this directly

Calls 2

to_string_no_commaFunction · 0.85
readMethod · 0.45

Tested by

no test coverage detected