Comments (6)
I won't write the code for you - figure it out.
from pitch-detection.
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:
pitch-detection/src/mp3read.cpp
Line 4 in bb0c7fd
Some things changed e.g.:
pitch-detection/src/mp3read.cpp
Line 33 in bb0c7fd
^ This line is not valid anymore, instead here you can do pitch_alloc::Mpm ma(1000)
pitch-detection/src/mp3read.cpp
Line 66 in bb0c7fd
^ 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:
pitch-detection/src/mp3read.cpp
Line 53 in bb0c7fd
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.
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.
pitch-detection/src/mp3read.cpp
Line 59 in bb0c7fd
"From time 0->time_chunk, pitch was ..."
"From time time_chunk->2*time_chunk, pitch was ..."
...
from pitch-detection.
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.
~/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.
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)
- Transcriber example HOT 2
- Absorb yin and mpm repos here
- Guitar tuner example HOT 3
- Docs HOT 1
- ma'c
- How can I use it in mac os? HOT 1
- YIN vs McLeod pitch detection accuracy HOT 7
- Implement pYIN (probabilistic YIN) HOT 6
- Implement SWIPE HOT 6
- Implement CREPE HOT 2
- C API
- wrong results with different buffer sizes in the sinewave example HOT 4
- Add Aud-SWIPE-P (parallel SWIPE)
- Android/Oboe tutorial/doc
- the error of the project HOT 6
- Additional compiler flags maybe needed HOT 2
- Using autocorrelation instead of calculating NSDF in MPM HOT 5
- Create a GitHub Action that builds and runs unit tests HOT 5
- systematic comparison of methods HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pitch-detection.