~mrp/aerodramus

585d24726a42c86d79222a7ac40a28523e4f5dbb — Mark Penner 8 months ago 5edda5c
use floats when reading files

work around some floating point files have maximum values < 1.0
which get truncated to 0 when read as integers
http://www.mega-nerd.com/libsndfile/api.html#note2
4 files changed, 8 insertions(+), 8 deletions(-)

M autotests/lib/aerodramus.test.cpp
M src/audiofile.h
M src/lib/aerodramus.cpp
M src/lib/aerodramus.h
M autotests/lib/aerodramus.test.cpp => autotests/lib/aerodramus.test.cpp +1 -1
@@ 54,7 54,7 @@ void testCalc::test_zero_cross_histogram()
    QTemporaryFile file;
    file.open();
    Aerodramus::write_file(file.fileName().toStdString(), Aerodramus::generate_signal(200, 20000, 20, 44100), 44100);
    std::vector<int> raw_samples = Aerodramus::read_file(file.fileName().toStdString());
    std::vector<float> raw_samples = Aerodramus::read_file(file.fileName().toStdString());
    expected = {449, 567, 712, 898, 1130, 1422, 1791, 2254, 2838, 3573, 4498, 5663, 7129, 8974, 11299, 14224, 17907, 22543, 28381, 35728};
    QCOMPARE_EQ(Aerodramus::zero_cross_histogram(raw_samples, 20), expected);
}

M src/audiofile.h => src/audiofile.h +1 -1
@@ 42,7 42,7 @@ Q_SIGNALS:
    void waveSeriesChanged();

private:
    std::vector<int> m_data{};
    std::vector<float> m_data{};
    QUrl m_fileName{};
    QLineSeries *m_waveSeries{};
    int m_samples{};

M src/lib/aerodramus.cpp => src/lib/aerodramus.cpp +4 -4
@@ 13,10 13,10 @@

#include <sndfile.hh>

std::vector<int> Aerodramus::read_file(std::string file_name)
std::vector<float> Aerodramus::read_file(std::string file_name)
{
    SndfileHandle file(file_name);
    std::vector<int> data(file.frames() * file.channels());
    std::vector<float> data(file.frames() * file.channels());
    file.read(&data.front(), file.frames());
    return data;
}


@@ 63,10 63,10 @@ double Aerodramus::watts_needed_to_point_source(double sensitivity, double dista
    return watts_needed;
}

std::vector<int> Aerodramus::zero_cross_histogram(const std::vector<int> &samples, int bins)
std::vector<int> Aerodramus::zero_cross_histogram(const std::vector<float> &samples, int bins)
{
    std::vector<int> hist{};
    auto crosses_zero = [](int x, int y) {
    auto crosses_zero = [](float x, float y) {
        return std::signbit(x) != std::signbit(y);
    };
    auto crossings = samples | std::views::pairwise_transform(crosses_zero);

M src/lib/aerodramus.h => src/lib/aerodramus.h +2 -2
@@ 9,12 9,12 @@

namespace Aerodramus
{
std::vector<int> read_file(std::string file_name);
std::vector<float> read_file(std::string file_name);
bool write_file(std::string file_name, std::vector<int> frames, int sample_rate);

std::vector<int> generate_signal(int start_freq, int end_freq, float seconds, int sample_rate);
double dB_from_point_source(double sensitivity, double distance, double watts);
double watts_needed_to_point_source(double sensitivity, double distance, double dB);
std::vector<int> zero_cross_histogram(const std::vector<int> &samples, int bins);
std::vector<int> zero_cross_histogram(const std::vector<float> &samples, int bins);
}; // namespace Aerodramus
#endif