Giter VIP home page Giter VIP logo

Comments (6)

sevagh avatar sevagh commented on May 27, 2024 1

I won't write the code for you - figure it out.

from pitch-detection.

sevagh avatar sevagh commented on May 27, 2024

I used to have an mp3 example, but I decided I didn't want to maintain it because I don't like working with the ffmpeg/libav codebase.

Here's what the file used to look like:

Some things changed e.g.:

pitchr->resize(codec_ctx->sample_rate, incr);

^ This line is not valid anymore, instead here you can do pitch_alloc::Mpm ma(1000)

double pitch =

^ Here you should do pitch_alloc::mpm(&raw[j], codec_ctx->sample_rate, &ma) - you should also convert &raw[j] (which is *double) to std::vector<double> raw.

Change this:

double raw[l];

to something like:

int l = (double) frame->linesize[0]/ (double) codec_ctx->channels;

std::vector<double> raw;
raw.reserve(l);

for (int i = 0; i < l; i += 1) {
		raw.push_back((double) frame->data[0][codec_ctx->channels*i]);
}

from pitch-detection.

sevagh avatar sevagh commented on May 27, 2024

This example takes an audio file and splits it into 1000-sample pieces - this approximately correlates to splitting the song into units of time e.g.

int64_t tstamp = (double)

"From time 0->time_chunk, pitch was ..."
"From time time_chunk->2*time_chunk, pitch was ..."
...

from pitch-detection.

zyoohv avatar zyoohv commented on May 27, 2024

Thanks for your response.
But I still can not run the code and can not know what's wrong with it.

My compile and install information:

~/pitch-detection# make -j4 && make install
g++ -ansi -pedantic -Werror -Wall -O3 -std=c++17 -fPIC -fext-numeric-literals -ffast-math -flto -shared -o lib/libpitch_detection.so -lffts -Iinclude \
	src/yin.cpp \
	src/mpm.cpp \
	src/autocorrelation.cpp \
	src/parabolic_interpolation.cpp
cp include/pitch_detection.h /usr/include
cp lib/libpitch_detection.so /usr/lib

My code(following your advice, but not make sure about it...):

#include <iostream>
#include "pitch_detector.h"

extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
};

void read_mp3_file(char *path, PitchDetector *pitchr)
{
    // Initialize FFmpeg
    av_register_all();

    AVFrame* frame = av_frame_alloc();
    AVFormatContext* fmt_ctx = NULL;
    avformat_open_input(&fmt_ctx, path, NULL, NULL);
    avformat_find_stream_info(fmt_ctx, NULL);
    AVCodec* cdc = nullptr;
    int stream_idx = av_find_best_stream(fmt_ctx,
                         AVMEDIA_TYPE_AUDIO,
                         -1, -1, &cdc, 0);
    AVStream* astream = fmt_ctx->streams[stream_idx];
    AVCodecContext* codec_ctx = astream->codec;
    codec_ctx->codec = cdc;
    avcodec_open2(codec_ctx, codec_ctx->codec, NULL);

    AVPacket rpkt;
    av_init_packet(&rpkt);

    // int incr = 1000;
    // pitchr->resize(codec_ctx->sample_rate, incr);
    pitch_alloc::Mpm ma(1000)

    // Read the packets in a loop
    while (av_read_frame(fmt_ctx, &rpkt) == 0) {
        if (rpkt.stream_index == astream->index) {
            AVPacket dpkt = rpkt;

            // Audio packets can have multiple audio frames in a single packet
            while (dpkt.size > 0) {
                int gotFrame = 0;
                int result = avcodec_decode_audio4
                         (codec_ctx, frame,
                          &gotFrame, &dpkt);

                if (result >= 0 && gotFrame) {
                    dpkt.size -= result;
                    dpkt.data += result;
                    // We now have a fully decoded audio frame
                    int l = (double) frame->linesize[0]/ (double) codec_ctx->channels;

                    std::vector<double> raw;
                    raw.reserve(l);
                    for (int i = 0; i < l; i += 1) {
                        raw.push_back((double) frame->data[0][codec_ctx->channels*i]);
                    }

                    int64_t tstamp = (double)
                             frame->best_effort_timestamp * (double) astream->time_base.num / (double) astream->time_base.den * 1000.0;

                    for (int j = 0; j < l; j += incr) {
                        if (j >= l) {
                            break;
                        }
                        double pitch = pitch_alloc::mpm(&raw[j], codec_ctx->sample_rate, &ma)
                        if (pitch != -1) {
                            printf("tstamp: %ld\t%f\n", tstamp, pitch);
                        }
                    }
                } else {
                    dpkt.size = 0;
                    dpkt.data = nullptr;
                }
            }
        }
        av_packet_unref(&rpkt);
    }

    av_free(frame);

    avcodec_close(codec_ctx);
    avformat_close_input(&fmt_ctx);
}

int main() {
    PitchDetector *p = new PitchDetector();
    read_mp3_file("./sample.mp3", p)
    return 0;
}

My compile command and result:

~/project# g++ -lpitch_detection main.cpp
main.cpp:2:10: fatal error: pitch_detector.h: No such file or directory
 #include "pitch_detector.h"
          ^~~~~~~~~~~~~~~~~~
compilation terminated.

My dir structure:

root@aab6489bfc23:~# tree
.
|-- pitch-detection
|   |-- LICENSE
|   ...
|-- project
|   `-- main.cpp
`-- src
    `-- Dockerfile

from pitch-detection.

sevagh avatar sevagh commented on May 27, 2024
~/project# g++ -lpitch_detection main.cpp
main.cpp:2:10: fatal error: pitch_detector.h: No such file or directory
 #include "pitch_detector.h"
          ^~~~~~~~~~~~~~~~~~
compilation terminated.

The name changed of the .h file: https://github.com/sevagh/pitch-detection#usage

from pitch-detection.

zyoohv avatar zyoohv commented on May 27, 2024

Very Thanks!

But I still can not run my code:

...
int main() {
    PitchDetector *p = new PitchDetector();
    read_mp3_file("./sample.mp3", p)
    return 0;
}

It has error:

~/project# g++ -lpitch_detection main.cpp
main.cpp:11:32: error: 'PitchDetector' has not been declared
 void read_mp3_file(char *path, PitchDetector *pitchr)
                                ^~~~~~~~~~~~~
main.cpp: In function 'void read_mp3_file(char*, int*)':
main.cpp:25:42: warning: 'AVStream::codec' is deprecated [-Wdeprecated-declarations]
     AVCodecContext* codec_ctx = astream->codec;
                                          ^~~~~
In file included from main.cpp:7:0:
/usr/include/x86_64-linux-gnu/libavformat/avformat.h:893:21: note: declared here
     AVCodecContext *codec;
                     ^~~~~
main.cpp:25:42: warning: 'AVStream::codec' is deprecated [-Wdeprecated-declarations]
     AVCodecContext* codec_ctx = astream->codec;
                                          ^~~~~
In file included from main.cpp:7:0:
/usr/include/x86_64-linux-gnu/libavformat/avformat.h:893:21: note: declared here
     AVCodecContext *codec;
                     ^~~~~
main.cpp:25:42: warning: 'AVStream::codec' is deprecated [-Wdeprecated-declarations]
     AVCodecContext* codec_ctx = astream->codec;
                                          ^~~~~
In file included from main.cpp:7:0:
/usr/include/x86_64-linux-gnu/libavformat/avformat.h:893:21: note: declared here
     AVCodecContext *codec;
                     ^~~~~
main.cpp:37:5: error: expected ',' or ';' before 'while'
     while (av_read_frame(fmt_ctx, &rpkt) == 0) {
     ^~~~~
main.cpp: In function 'int main()':
main.cpp:88:5: error: 'PitchDetector' was not declared in this scope
     PitchDetector *p = new PitchDetector();
     ^~~~~~~~~~~~~
main.cpp:88:20: error: 'p' was not declared in this scope
     PitchDetector *p = new PitchDetector();
                    ^
main.cpp:88:28: error: expected type-specifier before 'PitchDetector'
     PitchDetector *p = new PitchDetector();

from pitch-detection.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.