diff --git a/CMakeLists.txt b/CMakeLists.txt index 974224646d96e0aabf480b60c47b030581bc843e..95f9e505b38d62e100afe837d6067b44f3db37a6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,6 +66,10 @@ target_include_directories(Vivy PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/inc) target_compile_options(Vivy PRIVATE -Wall -Wextra -Wshadow -Wnon-virtual-dtor -pedantic + -Wno-unused-private-field # Skip the unused private fields for now + -Wcast-align -Woverloaded-virtual -Wconversion -Wsign-conversion + -Wmisleading-indentation -Wnull-dereference -Wdouble-promotion + -Wformat=2 ) set_target_properties(Vivy PROPERTIES diff --git a/src/AudioUtils.c b/src/AudioUtils.c index 723d20df63c8ae4513db6169040afd63bd934bd7..c2d7f7f4dfbe38b1a26e7850d4849f8f0ceeb1a4 100644 --- a/src/AudioUtils.c +++ b/src/AudioUtils.c @@ -10,14 +10,14 @@ #define MAXPIXVALUE 7 /* Some magix AV magic stuff */ int -decodeAudioFile(const char *path, const int sample_rate, double **const data, int *const size) +decodeAudioFile(const char *path, const int sample_rate, double **const data, size_t *const size) { AVPacket packet; AVFormatContext *format = avformat_alloc_context(); struct SwrContext *swr = swr_alloc(); AVFrame *frame = av_frame_alloc(); int ret_code = -1; - int stream_index = -1; + ssize_t stream_index = -1; AVStream *stream = NULL; AVCodecContext *codec_ctx = NULL; AVCodecParameters *codec_par = NULL; @@ -58,19 +58,19 @@ decodeAudioFile(const char *path, const int sample_rate, double **const data, in codec = avcodec_find_decoder(codec_par->codec_id); if (codec == NULL) { - fprintf(stderr, "Failed to find decoder for stream #%u in file '%s'\n", stream_index, path); + fprintf(stderr, "Failed to find decoder for stream #%zu in file '%s'\n", stream_index, path); goto exit_error; } if (avcodec_open2(codec_ctx, codec, NULL) < 0) { - fprintf(stderr, "Failed to open decoder for stream #%u in file '%s'\n", stream_index, path); + fprintf(stderr, "Failed to open decoder for stream #%zu in file '%s'\n", stream_index, path); goto exit_error; } /* Prepare resampler */ av_opt_set_int(swr, "in_channel_count", codec_ctx->channels, 0); av_opt_set_int(swr, "out_channel_count", 1, 0); - av_opt_set_int(swr, "in_channel_layout", codec_ctx->channel_layout, 0); + av_opt_set_int(swr, "in_channel_layout", (int64_t)codec_ctx->channel_layout, 0); av_opt_set_int(swr, "out_channel_layout", AV_CH_LAYOUT_MONO, 0); av_opt_set_int(swr, "in_sample_rate", codec_ctx->sample_rate, 0); av_opt_set_int(swr, "out_sample_rate", sample_rate, 0); @@ -101,11 +101,13 @@ decodeAudioFile(const char *path, const int sample_rate, double **const data, in /* Resample frames */ double *buffer; av_samples_alloc((uint8_t **)&buffer, NULL, 1, frame->nb_samples, AV_SAMPLE_FMT_DBL, 0); - const int frame_count = + const int frame_count_int = swr_convert(swr, (uint8_t **)&buffer, frame->nb_samples, (const uint8_t **)frame->data, frame->nb_samples); + assert(frame_count_int >= 0); + const size_t frame_count = (size_t)frame_count_int; /* Append resampled frames to data */ - *data = (double *)realloc(*data, (*size + frame->nb_samples) * sizeof(double)); + *data = (double *)realloc(*data, (*size + (size_t)frame->nb_samples) * sizeof(double)); memcpy(*data + *size, buffer, frame_count * sizeof(double)); av_free(buffer); *size += frame_count; @@ -160,7 +162,7 @@ RawImageDataGetHeight(RawImageData *const ptr) unsigned char * RawImageDataDuplicatePixels(RawImageData *const ptr) { - const size_t size = sizeof(unsigned char) * ptr->width * ptr->height / 2; + const size_t size = sizeof(unsigned char) * (size_t)ptr->width * (size_t)ptr->height / 2; unsigned char *ret = (unsigned char *)malloc(size); assert(ret); memcpy(ret, ptr->pixs, size); @@ -168,30 +170,30 @@ RawImageDataDuplicatePixels(RawImageData *const ptr) } RawImageData * -RawImageDataNewFromData(const double *data, const int size) +RawImageDataNewFromData(const double *data, const size_t size) { const int chunk_size = 512; const int overlap = 128; const int decal = chunk_size - overlap; - const int width = (size - chunk_size) / decal; - const int height = chunk_size; + const size_t width = (size - chunk_size) / decal; + const size_t height = chunk_size; unsigned char *pixs = (unsigned char *)malloc(sizeof(unsigned char) * width * height / 2); FFTSample *chunk_data = (FFTSample *)av_malloc_array(2 * chunk_size, sizeof(FFTSample)); RDFTContext *ctx = av_rdft_init((int)log2(chunk_size), DFT_R2C); assert(pixs && chunk_data && ctx); - for (int x = 0, i = 0; i < size - chunk_size; i += decal, ++x) { + for (size_t x = 0, i = 0; i < size - chunk_size; i += decal, ++x) { #pragma omp parallel for - for (int j = 0; j < chunk_size; j++) { - const float curr_dat = data[i + j]; - const double window_modifier = 0.5 * (1 - cos(2 * M_PI * j / (chunk_size - 1))); + for (size_t j = 0; j < chunk_size; j++) { + const double curr_dat = data[i + j]; + const double window_modifier = 0.5 * (1 - cos(2 * M_PI * ((int)j) / (chunk_size - 1))); float value = (float)(window_modifier * curr_dat); // cap values above 1 and below -1 - if (value > 1.0) { + if (value > 1.0f) { value = 1; - } else if (value < -1.0) { + } else if (value < -1.0f) { value = -1; } chunk_data[j] = value; @@ -200,18 +202,18 @@ RawImageDataNewFromData(const double *data, const int size) av_rdft_calc(ctx, chunk_data); #pragma omp parallel for - for (int j = 0; j < chunk_size / 2; j++) { + for (size_t j = 0; j < chunk_size / 2; j++) { const float im = chunk_data[j * 2]; const float re = chunk_data[j * 2 + 1]; - const double mag = sqrt(im * im + re * re); + const float mag = sqrtf(im * im + re * re); pixs[j * width + x] = (unsigned char)(mag)*MAXPIXVALUE; } } RawImageData *ret = (RawImageData *)malloc(sizeof(RawImageData)); RawImageData ret_data = { - .width = width, - .height = height, + .width = (int)width, + .height = (int)height, .pixs = pixs, .chunk_data = chunk_data, .ctx = ctx, diff --git a/src/AudioUtils.h b/src/AudioUtils.h index 92036c21a1ebb9fa09c6e2138b00494c9b76380d..9ff514c2952cf2b052ea4934cb8a609277fe7fc2 100644 --- a/src/AudioUtils.h +++ b/src/AudioUtils.h @@ -7,10 +7,10 @@ extern "C" { #include <libavcodec/avcodec.h> -int decodeAudioFile(const char *path, const int sample_rate, double **const data, int *const size); +int decodeAudioFile(const char *path, const int sample_rate, double **const data, size_t *const size); typedef struct ___RawImageData RawImageData; -RawImageData *RawImageDataNewFromData(const double *data, const int size); +RawImageData *RawImageDataNewFromData(const double *data, const size_t size); void RawImageDataFree(RawImageData *const); int RawImageDataGetWidth(RawImageData *const); int RawImageDataGetHeight(RawImageData *const); diff --git a/src/AudioVisualizer.cc b/src/AudioVisualizer.cc index a4c664d427ba6342468d2b5bb27a4f0d9175449e..0503720c40b311dddc6138ac35f019b19b452c69 100644 --- a/src/AudioVisualizer.cc +++ b/src/AudioVisualizer.cc @@ -31,7 +31,7 @@ AudioVisualizer::fromFile(const QString &filename) const int sample_rate = 44100; double *data = nullptr; - int size = 0; + size_t size = 0; int rc = decodeAudioFile(filename.toStdString().c_str(), sample_rate, &data, &size); auto data_deleter = [](double *ptr) -> void { if (ptr != nullptr) diff --git a/src/TimingView.cc b/src/TimingView.cc index 37827aa9c93daba1be30882140e81922b856555c..81bd28563ffc0e2cb5e21961335e962ae1b14d70 100644 --- a/src/TimingView.cc +++ b/src/TimingView.cc @@ -11,6 +11,7 @@ #include <QMouseEvent> #include <QPainter> #include <QAbstractScrollArea> +#include <QtGlobal> #define TO_ADD_TO_IMAGE_HEIGHT 2 /* Used for alignement */ @@ -23,3 +24,20 @@ TimingView::TimingView(QImage img, quint64 soundLength, QWidget *parent) noexcep setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); setScene(scene); } + +void +TimingView::mousePressEvent(QMouseEvent *event) noexcept +{ + QPoint pos = event->pos(); + pos.rx() += horizontalScrollBar()->value(); + int x = event->pos().x() + horizontalScrollBar()->value(); + + QGraphicsItem *got; + if ((got = scene->itemAt(pos, QTransform())) == nullptr || got == scene->bg()) { + const int height = static_cast<int>(scene->height()); + scene->addItem(new TimingBar(QLine(x, 0, x, height), + event->button() == Qt::LeftButton ? startColour : endColour)); + } + + QGraphicsView::mousePressEvent(event); +}