Skip to content

Commit

Permalink
Release version 0.01 of audio-recognition: Finish codes on linux ubuntu
Browse files Browse the repository at this point in the history
  • Loading branch information
jhyume committed Feb 5, 2015
1 parent 18e3abb commit a0a5eb6
Show file tree
Hide file tree
Showing 16 changed files with 351 additions and 63 deletions.
20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
CINC = -Iinclude
SRC = src
BIN = bin
CC = g++ -Wall -O3 -Wno-unused-result $(CINC)

all: ${BIN}/build ${BIN}/recog

# Generates a compiler for an d_lang language,
${BIN}/build: $(SRC)/build.cpp $(SRC)/hash.cpp $(SRC)/fingerprint.cpp $(SRC)/wav.cpp $(SRC)/bmp.cpp
mkdir -p ${BIN}
$(CC) -o $(BIN)/build $(SRC)/build.cpp $(SRC)/hash.cpp $(SRC)/fingerprint.cpp $(SRC)/bmp.cpp $(SRC)/wav.cpp -ll -lm -lfftw3

${BIN}/recog: $(SRC)/recog.cpp $(SRC)/hash.cpp $(SRC)/fingerprint.cpp $(SRC)/wav.cpp $(SRC)/bmp.cpp
mkdir -p ${BIN}
$(CC) -o $(BIN)/recog $(SRC)/recog.cpp $(SRC)/hash.cpp $(SRC)/fingerprint.cpp $(SRC)/bmp.cpp $(SRC)/wav.cpp -ll -lm -lfftw3

clean:
rm -f *.BAK *.o core *~* *.a
rm -f $(BIN)/build
rm -f $(BIN)/recog
37 changes: 30 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
Robust Music/Audio Recognition Algorithm in C++
Music/Audio Recognition Application written in C++
===============================================

Features:
-----
* Robust Audio Recognition
* High efficiency (recognize in less than 0.1 second per song)
* Memory reduction ( 4G is enough for 10000 songs)
* The Data Structure now support 2,500,000 songs and each song less than 7 minutes.
* return the TIME POINT of the cut song in original song.

Dependencies:
-----
* fftw3:
If under Debian/Ubuntu, run `apt-get install libfftw3-dev`.

Compile & Run:
-----
* extract songs to ./bin/ dir.
* `make` to produce two excutive files in ./bin/
* The songs should be .wav format.
* The songs should be contained in ./bin/samples/
* You may download the songs.
link:
password:
* run `cd bin`, `./build` and it'll produce a file called `database`
* run `./recog ${filename}` to recog the songs that in samples list.

TODO:
-----
- Transfer the codes from windows to linux.
- Android app to test the algorithm interactively.
- The Http request/response and such hustle and bustle.
- ...
- Docs

DONE:
-----
- Solve the BaiduMusic download tools.
- All the codes in Windows.
- Transfer the codes from windows to linux.
- Solve the BaiduMusic download tools to update new songs automatically.

My partner and I finshed this project on Windows8 and got the *First Prize* delivered by [National Engineering Laboratory for Speech and Language Informatinon Processing](http://nelslip.ustc.edu.cn/html/yunews/detail_2014_05/30/191.shtml).
At the begining, it's a COMPETITION. My partner and I finshed this project on Windows8 and won the *First Prize* delivered by [National Engineering Laboratory for Speech and Language Informatinon Processing](http://nelslip.ustc.edu.cn/html/yunews/detail_2014_05/30/191.shtml).
Feel free to contact me yjh199511 at gmail
30 changes: 30 additions & 0 deletions include/bmp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef BMP_H
#define BMP_H

#define BMP_TEST

#include <iostream>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#define checkborder(x,y) (x>=0 && x<W && y>=0 && y<H)
using namespace std;

class TBMP{
private:
char **bitmap;
FILE *fp;
public:
int W, H;
int MODE;
//int R, G, B;
int Grey;
TBMP(const char *filename, int w, int h, int mode);
~TBMP();
int setcolor(int grey);
int drawpoint(int x, int y);
int drawline(int x0, int y0, int x1, int y1);
int drawrect(int x0, int y0, int x1, int y1);
};

#endif
File renamed without changes.
2 changes: 1 addition & 1 deletion src/fingerprint.h → include/fingerprint.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <fftw3.h>
#include <assert.h>
#include <wav.h>
#include <Hash.h>
#include <hash.h>
#include <bmp.h>
#include <stdint.h>

Expand Down
30 changes: 15 additions & 15 deletions src/Hash.h → include/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
#define OverFlowThreshold 1<<28
using namespace std;

//Hash的key类型,用于iFluBuild
//Hash的key类型,用于Build
//(f1, f2_f1, t)
struct HashKeyInfo{
size_t* start;
size_t length;
HashKeyInfo* next; //指针用于扩容
};
//Hash的key类型,用于iFluSelect
//Hash的key类型,用于Recog
struct HashKeyTable{
size_t *start;
size_t length;
Expand All @@ -43,26 +43,26 @@ class THash{
HashKeyInfo *key_info;
HashKeyTable *key_table;

THash::THash();
THash::~THash();
void THash::ReBuildInit();
THash();
~THash();
void ReBuildInit();
/************************************ Functions for build tracks.(iFlyBuild) ************************************************/
//此函数用于对于iFlyBuild的情况,从wav到Hash_Table. //Finished.
void THash::BuildInit();
void THash::BuildUnInit();
void BuildInit();
void BuildUnInit();
//加歌名,更新歌曲数。 //Finished.
void THash::AddSongList(const char *filename);
void AddSongList(const char *filename);
//往Value的内存块里加数据,更新Key_table. //Finished.
void THash::InsertHash(size_t f1, size_t f2_f1, size_t t, size_t id, size_t offset);
void InsertHash(size_t f1, size_t f2_f1, size_t t, size_t id, size_t offset);
//将Hash表往文件里刷(不是刷整个内存,这样会在iFlySelect里浪费内存空间)//Finished
void THash::Hash2File(const char* filename);
void Hash2File(const char* filename);

/************************************ Functions for select tracks.(iFlySelect) ************************************************/
size_t* THash::GetHash(size_t f1, size_t f2_f1, size_t t);
void THash::File2Hash(const char* filename);
size_t* GetHash(size_t f1, size_t f2_f1, size_t t);
void File2Hash(const char* filename);
//Functions for vote and save the top voted id to QueryId.
void THash::VoteInit();
void THash::Vote(size_t f1, size_t f2_f1, size_t t, size_t offset);
size_t THash::VoteResult(size_t &offset);
void VoteInit();
void Vote(size_t f1, size_t f2_f1, size_t t, size_t offset);
size_t VoteResult(size_t &offset);
};
#endif // _HashFunc_h_
File renamed without changes.
2 changes: 1 addition & 1 deletion src/wav.h → include/wav.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <iostream>
#include <stdint.h>
#include <assert.h>
#include "stdafx.h"
#include "stdio.h"
//#define WAV_TEST
using namespace std;

Expand Down
Binary file added references/Audio Fingerprinting.pdf
Binary file not shown.
Binary file added references/Wang03-shazam.pdf
Binary file not shown.
165 changes: 165 additions & 0 deletions src/bmp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#include "bmp.h"

TBMP::TBMP(const char *filename, int w, int h, int mode){
#ifdef BMP_TEST
char bufferc[4];
/* char ch = 255; */
unsigned long buffer4;
unsigned short buffer2;

fp = fopen(filename, "wb");
if (fp == NULL) printf("!!");
W = w;
H = h;
MODE = mode%2;
if (W%4!=0) W=(W/4+1)*4;

int i, j;
bitmap = (char **)malloc(H * sizeof(char *));
if (bitmap == NULL) printf("00");
for(i=0; i<H; i++){
bitmap[i] = (char *)malloc(W * sizeof(char));
assert(bitmap[i]!=NULL);
}
for (j=0; j<H; j++)
for (i=0; i<W; i++){
bitmap[j][i] = 255;
}

//bitmap-file header (14 Bytes)
fwrite("BM", 1, 2, fp); //0000h 2 bfType
buffer4 = W*H + 1078;
fwrite(&buffer4, 4, 1, fp); //0002h 4 bfSize
buffer4 = 0;
fwrite(&buffer4, 1, 4, fp); //0006h 4 bfReserved
buffer4 = 1078;
fwrite(&buffer4, 4, 1, fp); //000Ah 4 bfOffbits

//bitmap-information header (40 Bytes)
buffer4 = 0x28;
fwrite(&buffer4, 4, 1, fp); //000Eh 4 biSize
buffer4 = W;
fwrite(&buffer4, 4, 1, fp); //0012h 4 biWidth
buffer4 = H;
fwrite(&buffer4, 4, 1, fp); //0016h 4 biHeight
buffer2 = 1;
fwrite(&buffer2, 2, 1, fp); //001Ah 2 biPlanes
buffer2 = 8;
fwrite(&buffer2, 2, 1, fp); //001Ch 2 biBitCount (8bit 256色)
buffer4 = 0;
fwrite(&buffer4, 4, 1, fp); //001Eh 4 biCompression
buffer4 = 0;
fwrite(&buffer4, 4, 1, fp); //0022h 4 biSizeImage
buffer4 = 0;
fwrite(&buffer4, 4, 1, fp); //0026h 4 biXPelsPerMeter
buffer4 = 0;
fwrite(&buffer4, 4, 1, fp); //002Ah 4 biYPelsPerMeter
buffer4 = 256;
fwrite(&buffer4, 4, 1, fp); //002Eh 4 biClrUsed (256色调色板)
buffer4 = 256;
fwrite(&buffer4, 4, 1, fp); //0032h 4 biClrImportant (256个重要颜色索引)

//color table (1024 Bytes)
bufferc[3] = 0;
for (i=0; i<256; i++){
bufferc[0] = i;
bufferc[1] = i;
bufferc[2] = i;
fwrite(bufferc, 1, 4, fp);
}
#endif
}

TBMP::~TBMP(){
#ifdef BMP_TEST
//data (W*H Bytes)
for (int j=H-1; j>=0; j--)
fwrite(bitmap[j], 1, W, fp);
fclose(fp);
//realease resources
#endif
}

int TBMP::setcolor(int grey){
#ifdef BMP_TEST
if (grey<0 || grey>255)
return 1;

Grey = grey;
return 0;
#endif
}

int TBMP::drawpoint(int x, int y){
#ifdef BMP_TEST
if (!checkborder(x,y))
return 1;

if (MODE)
bitmap[H-1-y][x] = Grey;
else
bitmap[y][x] = Grey;
return 0;
#endif
}

int TBMP::drawline(int x0, int y0, int x1, int y1){
#ifdef BMP_TEST
if (!checkborder(x0, y0) || !checkborder(x1, y1))
return 1;
if (x0==x1 && y0==y1)
return drawpoint(x0, y0);

int xs, ys, xe, ye;
int i, j;
double t;
if (abs(x0-x1) >= abs(y0-y1)){
if (x0 < x1){
xs = x0;
ys = y0;
xe = x1;
ye = y1;
}
else{
xs = x1;
ys = y1;
xe = x0;
ye = y0;
}
t = (double)(ye - ys) / (double)(xe - xs);
for (i=xs; i<=xe; i++){
j = (int)((i - xs) * t + ys);
drawpoint(i,j);
//bitmap[j][i] = Grey;
}
}
else{
if (y0 < y1){
xs = x0;
ys = y0;
xe = x1;
ye = y1;
}
else{
xs = x1;
ys = y1;
xe = x0;
ye = y0;
}
t = (double)(xe - xs) / (double)(ye - ys);
for (j=ys; j<=ye; j++){
i = (int)((j-ys) * t + xs);
drawpoint(i,j);
//bitmap[j][i] =Grey;
}
}
return 0;
#endif
}

int TBMP::drawrect(int x0, int y0, int x1, int y1){
#ifdef BMP_TEST
return drawline(x0, y0, x0, y1) || drawline(x0, y0, x1, y0) \
|| drawline(x1, y0, x1, y1) || drawline(x0, y1, x1, y1);
#endif
}
Loading

0 comments on commit a0a5eb6

Please sign in to comment.