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

Method writeWavetableToFile

Source/Utilities.cpp:971–1055  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

969}
970
971void Utilities::writeWavetableToFile(int index_wavetable) {
972
973 float p_samplerate = 44100.f;
974 float wavetables[1][SUBTABLES_PER_WAVETABLE][WAVETABLE_LENGTH] = {0};
975
976 float max = 0;
977
978 // this flags hackishly if a waveform only uses sine components
979 bool sine_only = m_fourier_coeffs[index_wavetable][0][0] > 0.5f ? true : false;
980
981 double seed_freq = 27.5; // A0
982 // float max = 0.f;
983
984 // loop over subtables
985 for (int index_sub_table = 0; index_sub_table < SUBTABLES_PER_WAVETABLE; ++index_sub_table) {
986
987 // how many harmonics are needed for this subtable
988 int number_of_harmonics = (int)((p_samplerate * 0.5f / seed_freq) - 1);
989
990 // don't allow more than 800 harmonics (for big Samplerates this might
991 // happen)
992 number_of_harmonics = number_of_harmonics > NUMBER_OF_HARMONICS ? NUMBER_OF_HARMONICS : number_of_harmonics;
993
994 for (int index_harmonics = 1; index_harmonics < number_of_harmonics; ++index_harmonics) {
995 for (int index_position = 0; index_position < WAVETABLE_LENGTH; ++index_position) {
996
997 // fill table with //sine harmonics
998 wavetables[0][index_sub_table][index_position] +=
999 m_fourier_coeffs[index_wavetable][0][index_harmonics] *
1000 sin(2.f * PI * index_position * index_harmonics / (float)WAVETABLE_LENGTH) *
1001 m_fourier_coeffs[index_wavetable][1][0]; // last term is normalization
1002 if (!sine_only) {
1003 // cosine
1004 wavetables[0][index_sub_table][index_position] +=
1005 m_fourier_coeffs[index_wavetable][1][index_harmonics] *
1006 cos(2.f * PI * index_position * index_harmonics / (float)WAVETABLE_LENGTH) *
1007 m_fourier_coeffs[index_wavetable][1][0]; // last term is normalization
1008 }
1009 }
1010 }
1011
1012 for (int index_sub_table_arr = 0; index_sub_table_arr < SUBTABLES_PER_WAVETABLE; ++index_sub_table_arr) {
1013 for (int index_position = 0; index_position < WAVETABLE_LENGTH; ++index_position) {
1014 if (fabs(wavetables[0][index_sub_table_arr][index_position]) > max) {
1015 max = fabs(wavetables[0][index_sub_table_arr][index_position]);
1016 }
1017 }
1018 }
1019
1020 // increment seed frequency by minor third = 2^(3/12)
1021 seed_freq *= 1.1892071150;
1022 }
1023
1024 // avoid division by zero
1025 max = max < 0.0001 ? 1 : max;
1026
1027 std::ofstream output_file;
1028 output_file.open("/home/frot/odin2/Source/audio/Oscillators/"

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected