sample_rate_conversion.cpp (5487B)
1 /** 2 * KFR (https://www.kfrlib.com) 3 * Copyright (C) 2016-2023 Dan Cazarin 4 * See LICENSE.txt for details 5 */ 6 7 #include <kfr/base.hpp> 8 #include <kfr/dsp.hpp> 9 #include <kfr/io.hpp> 10 11 using namespace kfr; 12 13 // Define constants for input and output sample rates and the length of the signal 14 constexpr size_t input_sr = 96000; // Input sample rate (96 kHz) 15 constexpr size_t output_sr = 44100; // Output sample rate (44.1 kHz) 16 constexpr size_t len = 96000 * 6; // Length of the signal (6 seconds at 96 kHz) 17 18 int main() 19 { 20 // Print the version of the KFR library being used 21 println(library_version()); 22 23 // Generate a swept sine wave signal with a duration of 'len' samples 24 univector<fbase> swept_sine = swept(0.5, len); 25 26 // -------------------------------------------------------------------------------------- 27 // ----------------------------- High Quality Resampling -------------------------------- 28 // -------------------------------------------------------------------------------------- 29 { 30 // Create a high-quality resampler from input_sr to output_sr 31 auto r = resampler<fbase>(resample_quality::high, output_sr, input_sr); 32 33 // Create a buffer for the resampled signal, taking the resampler delay into account 34 univector<fbase> resampled(len * output_sr / input_sr + r.get_delay()); 35 36 // Perform the resampling process 37 r.process(resampled, swept_sine); 38 39 // Write the resampled signal to a WAV file 40 audio_writer_wav<fbase> writer(open_file_for_writing(KFR_FILEPATH("audio_high_quality.wav")), 41 audio_format{ 1, audio_sample_type::i32, output_sr }); 42 writer.write(resampled.data(), resampled.size()); 43 writer.close(); 44 45 // Save a plot of the high-quality resampled audio 46 plot_save("audio_high_quality", "audio_high_quality.wav", ""); 47 } 48 49 // -------------------------------------------------------------------------------------- 50 // ----------------------------- Normal Quality Resampling ------------------------------ 51 // -------------------------------------------------------------------------------------- 52 { 53 // Create a normal-quality resampler from input_sr to output_sr 54 auto r = resampler<fbase>(resample_quality::normal, output_sr, input_sr); 55 56 // Create a buffer for the resampled signal, taking the resampler delay into account 57 univector<fbase> resampled(len * output_sr / input_sr + r.get_delay()); 58 59 // Perform the resampling process 60 r.process(resampled, swept_sine); 61 62 // Write the resampled signal to a WAV file 63 audio_writer_wav<fbase> writer(open_file_for_writing(KFR_FILEPATH("audio_normal_quality.wav")), 64 audio_format{ 1, audio_sample_type::i32, output_sr }); 65 writer.write(resampled.data(), resampled.size()); 66 writer.close(); 67 68 // Save a plot of the normal-quality resampled audio 69 plot_save("audio_normal_quality", "audio_normal_quality.wav", ""); 70 } 71 72 // -------------------------------------------------------------------------------------- 73 // ----------------------------- Low Quality Resampling --------------------------------- 74 // -------------------------------------------------------------------------------------- 75 { 76 // Create a low-quality resampler from input_sr to output_sr 77 auto r = resampler<fbase>(resample_quality::low, output_sr, input_sr); 78 79 // Create a buffer for the resampled signal, taking the resampler delay into account 80 univector<fbase> resampled(len * output_sr / input_sr + r.get_delay()); 81 82 // Perform the resampling process 83 r.process(resampled, swept_sine); 84 85 // Write the resampled signal to a WAV file 86 audio_writer_wav<fbase> writer(open_file_for_writing(KFR_FILEPATH("audio_low_quality.wav")), 87 audio_format{ 1, audio_sample_type::i32, output_sr }); 88 writer.write(resampled.data(), resampled.size()); 89 writer.close(); 90 91 // Save a plot of the low-quality resampled audio 92 plot_save("audio_low_quality", "audio_low_quality.wav", ""); 93 } 94 95 // -------------------------------------------------------------------------------------- 96 // ----------------------------- Draft Quality Resampling ------------------------------- 97 // -------------------------------------------------------------------------------------- 98 { 99 // Create a draft-quality resampler from input_sr to output_sr 100 auto r = resampler<fbase>(resample_quality::draft, output_sr, input_sr); 101 102 // Create a buffer for the resampled signal, taking the resampler delay into account 103 univector<fbase> resampled(len * output_sr / input_sr + r.get_delay()); 104 105 // Perform the resampling process 106 r.process(resampled, swept_sine); 107 108 // Write the resampled signal to a WAV file 109 audio_writer_wav<fbase> writer(open_file_for_writing(KFR_FILEPATH("audio_draft_quality.wav")), 110 audio_format{ 1, audio_sample_type::i32, output_sr }); 111 writer.write(resampled.data(), resampled.size()); 112 writer.close(); 113 114 // Save a plot of the draft-quality resampled audio 115 plot_save("audio_draft_quality", "audio_draft_quality.wav", ""); 116 } 117 118 println("SVG plots have been saved to svg directory"); 119 120 return 0; 121 }