Skip to content
This repository has been archived by the owner on Mar 28, 2022. It is now read-only.

First Attempt C++14 and Visual 2015 support #85

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
*.o
*.dylib
*.so
echoprint-codegen
src/libcodegen.so.4.1.1

Debug
*.opendb
*~
23 changes: 11 additions & 12 deletions src/AudioBufferInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,26 @@
// Copyright 2011 The Echo Nest Corporation. All rights reserved.
//


#ifndef AUDIOBUFFERINPUT_H
#define AUDIOBUFFERINPUT_H

#include "Common.h"
#include <iostream>
#include <string>
#ifndef AUDIOBUFFERINPUT_H
#define AUDIOBUFFERINPUT_H
#include "Params.h"
#include "AudioStreamInput.h"


class AudioBufferInput : public AudioStreamInput {
public:
AudioBufferInput();
std::string GetName() {return "direct buffer";}
void SaveBuffer(const char*filename);
void SetBuffer(const float* pBuffer, uint numSamples);
protected:
std::string GetCommandLine(const char*){return "";}
private:
public:
AudioBufferInput();
std::string GetName() {return "direct buffer";}
void SaveBuffer(const char*filename);
void SetBuffer(const float* pBuffer, uint numSamples);
protected:
std::string GetCommandLine(const char*)
{return "";}
};

#endif


Expand Down
125 changes: 63 additions & 62 deletions src/AudioStreamInput.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,106 +27,107 @@
using std::string;

namespace FFMPEG {
// Do we think FFmpeg will read this as an audio file?
bool IsAudioFile(const char* pFileName) {
static const char* supportedExtensions[] = {".mp3", ".m4a", ".mp4", ".aif", ".aiff", ".flac", ".au", ".wav", ".aac", ".flv"};
// Not an exhaustive list. ogg and rm could be added if tested.
for (uint i = 0; i < NELEM(supportedExtensions); i++) {
if (File::ends_with(pFileName, supportedExtensions[i]))
return true;
}
return false;
// Do we think FFmpeg will read this as an audio file?
bool IsAudioFile(const char* pFileName) {
static const char* supportedExtensions[] = {".mp3", ".m4a", ".mp4", ".aif", ".aiff", ".flac", ".au", ".wav", ".aac", ".flv"};
// Not an exhaustive list. ogg and rm could be added if tested.
for (uint i = 0; i < NELEM(supportedExtensions); i++) {
if (File::ends_with(pFileName, supportedExtensions[i]))
return true;
}
return false;
}
}

bool AudioStreamInput::IsSupported(const char *path) {
return true; // Take a crack at anything, by default. The worst thing that will happen is that we fail.
return true; // Take a crack at anything, by default. The worst thing that will happen is that we fail.
}

AudioStreamInput::AudioStreamInput() : _pSamples(NULL), _NumberSamples(0), _Offset_s(0), _Seconds(0) {}
AudioStreamInput::AudioStreamInput() : _pSamples(nullptr), _NumberSamples(0), _Offset_s(0), _Seconds(0) {}

AudioStreamInput::~AudioStreamInput() {
if (_pSamples != NULL)
delete [] _pSamples, _pSamples = NULL;
delete [] _pSamples;
_pSamples = nullptr;
}


bool AudioStreamInput::ProcessFile(const char* filename, int offset_s/*=0*/, int seconds/*=0*/) {
if (!File::Exists(filename) || !IsSupported(filename))
return false;
if (!File::Exists(filename) || !IsSupported(filename))
return false;

_Offset_s = offset_s;
_Seconds = seconds;
std::string message = GetCommandLine(filename);
_Offset_s = offset_s;
_Seconds = seconds;
std::string message = GetCommandLine(filename);

FILE* fp = popen(message.c_str(), POPEN_MODE);
bool ok = (fp != NULL);
if (ok)
FILE* fp = popen(message.c_str(), POPEN_MODE);
bool ok = (fp != nullptr);
if (ok)
{
bool did_work = ProcessFilePointer(fp);
bool succeeded = !pclose(fp);
ok = did_work && succeeded;
bool did_work = ProcessFilePointer(fp);
bool succeeded = !pclose(fp);
ok = did_work && succeeded;
}
else
fprintf(stderr, "AudioStreamInput::ProcessFile can't open %s\n", filename);
else
fprintf(stderr, "AudioStreamInput::ProcessFile can't open %s\n", filename);

return ok;
return ok;
}

// reads raw signed 16-bit shorts from a file
bool AudioStreamInput::ProcessRawFile(const char* rawFilename) {
FILE* fp = fopen(rawFilename, "r"); // TODO: Windows
bool ok = (fp != NULL);
if (ok)
FILE* fp = fopen(rawFilename, "r"); // TODO: Windows
bool ok = (fp != nullptr);
if (ok)
{
ok = ProcessFilePointer(fp);
fclose(fp);
ok = ProcessFilePointer(fp);
fclose(fp);
}

return ok;
return ok;
}

// reads raw signed 16-bit shorts from stdin, for example:
// ffmpeg -i fille.mp3 -f s16le -ac 1 -ar 11025 - | TestAudioSTreamInput
bool AudioStreamInput::ProcessStandardInput(void) {
// TODO - Windows will explodey at not setting O_BINARY on stdin.
return ProcessFilePointer(stdin);
// TODO - Windows will explodey at not setting O_BINARY on stdin.
return ProcessFilePointer(stdin);
}

bool AudioStreamInput::ProcessFilePointer(FILE* pFile) {
std::vector<short*> vChunks;
uint nSamplesPerChunk = (uint) Params::AudioStreamInput::SamplingRate * Params::AudioStreamInput::SecondsPerChunk;
uint samplesRead = 0;
do {
short* pChunk = new short[nSamplesPerChunk];
samplesRead = fread(pChunk, sizeof (short), nSamplesPerChunk, pFile);
_NumberSamples += samplesRead;
vChunks.push_back(pChunk);
} while (samplesRead > 0);

// Convert from shorts to 16-bit floats and copy into sample buffer.
uint sampleCounter = 0;
_pSamples = new float[_NumberSamples];
uint samplesLeft = _NumberSamples;
for (uint i = 0; i < vChunks.size(); i++)
std::vector<short*> vChunks;

uint nSamplesPerChunk = static_cast<uint>(Params::AudioStreamInput::SamplingRate * Params::AudioStreamInput::SecondsPerChunk);
uint samplesRead = 0;
do {
short* pChunk = new short[nSamplesPerChunk];
samplesRead = fread(pChunk, sizeof (short), nSamplesPerChunk, pFile);
_NumberSamples += samplesRead;
vChunks.push_back(pChunk);
} while (samplesRead > 0);

// Convert from shorts to 16-bit floats and copy into sample buffer.
uint sampleCounter = 0;
_pSamples = new float[_NumberSamples];
uint samplesLeft = _NumberSamples;
for (uint i = 0; i < vChunks.size(); i++)
{
short* pChunk = vChunks[i];
uint numSamples = samplesLeft < nSamplesPerChunk ? samplesLeft : nSamplesPerChunk;
short* pChunk = vChunks[i];
uint numSamples = samplesLeft < nSamplesPerChunk ? samplesLeft : nSamplesPerChunk;

for (uint j = 0; j < numSamples; j++)
_pSamples[sampleCounter++] = (float) pChunk[j] / 32768.0f;
for (uint j = 0; j < numSamples; j++)
_pSamples[sampleCounter++] = (float) pChunk[j] / 32768.0f;

samplesLeft -= numSamples;
delete [] pChunk, vChunks[i] = NULL;
samplesLeft -= numSamples;
delete [] pChunk, vChunks[i] = nullptr;
}
assert(samplesLeft == 0);
assert(samplesLeft == 0);

int error = ferror(pFile);
bool success = error == 0;
int error = ferror(pFile);
bool success = error == 0;

if (!success)
perror("ProcessFilePointer error");
return success;
if (!success)
perror("ProcessFilePointer error");
return success;
}


2 changes: 1 addition & 1 deletion src/AudioStreamInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <math.h>
#include "File.h"
#if defined(_WIN32) && !defined(__MINGW32__)
#define snprintf _snprintf
#define snprintf _snprintf_s
#define DEVNULL "nul"
#else
#define DEVNULL "/dev/null"
Expand Down
13 changes: 4 additions & 9 deletions src/Codegen.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,20 @@ Codegen::Codegen(const float* pcm, unsigned int numSamples, int start_offset) {
if (Params::AudioStreamInput::MaxSamples < (uint)numSamples)
throw std::runtime_error("File was too big\n");

Whitening *pWhitening = new Whitening(pcm, numSamples);
std::unique_ptr<Whitening> pWhitening (new Whitening(pcm, numSamples));
pWhitening->Compute();

AudioBufferInput *pAudio = new AudioBufferInput();
std::unique_ptr<AudioBufferInput> pAudio (new AudioBufferInput());
pAudio->SetBuffer(pWhitening->getWhitenedSamples(), pWhitening->getNumSamples());

SubbandAnalysis *pSubbandAnalysis = new SubbandAnalysis(pAudio);
std::unique_ptr<SubbandAnalysis> pSubbandAnalysis (new SubbandAnalysis(pAudio->getSamples(), pAudio->getNumSamples()));
pSubbandAnalysis->Compute();

Fingerprint *pFingerprint = new Fingerprint(pSubbandAnalysis, start_offset);
std::unique_ptr<Fingerprint> pFingerprint (new Fingerprint(std::move(pSubbandAnalysis), start_offset));
pFingerprint->Compute();

_CodeString = createCodeString(pFingerprint->getCodes());
_NumCodes = pFingerprint->getCodes().size();

delete pFingerprint;
delete pSubbandAnalysis;
delete pWhitening;
delete pAudio;
}

string Codegen::createCodeString(vector<FPCode> vCodes) {
Expand Down
10 changes: 1 addition & 9 deletions src/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#ifndef COMMON_H
#define COMMON_H



#include <assert.h>
#ifndef _WIN32
#include <sys/time.h>
Expand All @@ -28,17 +26,11 @@
#include <stdio.h>
#include <stdarg.h>


#ifndef NULL
#define NULL 0
#endif


// Returns the current date in seconds. The precision is in microseconds.
static inline double now (void) {
struct timeval tv;
double now;
gettimeofday (&tv, NULL);
gettimeofday (&tv, nullptr);
now = 1e-6 * tv.tv_usec + tv.tv_sec;
return now;
}
Expand Down
4 changes: 2 additions & 2 deletions src/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ bool WriteStuffToFile(const char* filename)
class File {
public:
File(const char* filename){_f = fopen(filename, "w");};
~File(){fclose(_f); _f = NULL;}
operator bool(){return _f != NULL;}
~File(){fclose(_f); _f = nullptr;}
operator bool(){return _f != nullptr;}
operator FILE*(){return _f;}
static bool Exists(const char* filename){return (access(filename, F_OK) == 0);}
static bool ends_with(const char* filename, const char* ending) {
Expand Down
7 changes: 4 additions & 3 deletions src/Fingerprint.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ unsigned int MurmurHash2 ( const void * key, int len, unsigned int seed ) {
return h;
}

Fingerprint::Fingerprint(SubbandAnalysis* pSubbandAnalysis, int offset)
: _pSubbandAnalysis(pSubbandAnalysis), _Offset(offset) { }
Fingerprint::Fingerprint(std::unique_ptr<SubbandAnalysis> pSubbandAnalysis, int offset)
: _pSubbandAnalysis(std::move(pSubbandAnalysis)), _Offset(offset)
{ }


uint Fingerprint::adaptiveOnsets(int ttarg, matrix_u&out, uint*&onset_counter_for_band) {
Expand Down Expand Up @@ -171,7 +172,7 @@ uint Fingerprint::adaptiveOnsets(int ttarg, matrix_u&out, uint*&onset_counter_fo
// dan is going to beat me if i call this "decimated_time_for_frame" like i want to
uint Fingerprint::quantized_time_for_frame_delta(uint frame_delta) {
double time_for_frame_delta = (double)frame_delta / ((double)Params::AudioStreamInput::SamplingRate / 32.0);
return ((int)floor((time_for_frame_delta * 1000.0) / (float)QUANTIZE_DT_S) * QUANTIZE_DT_S) / floor(QUANTIZE_DT_S*1000.0);
return (static_cast<int>(floor((time_for_frame_delta * 1000.0) / (float)QUANTIZE_DT_S) * QUANTIZE_DT_S) / floor(QUANTIZE_DT_S*1000.0));
}

uint Fingerprint::quantized_time_for_frame_absolute(uint frame) {
Expand Down
6 changes: 3 additions & 3 deletions src/Fingerprint.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ unsigned int MurmurHash2 ( const void * key, int len, unsigned int seed );

class Fingerprint {
public:
Fingerprint(std::unique_ptr<SubbandAnalysis> pSubbandAnalysis, int offset);
uint quantized_time_for_frame_delta(uint frame_delta);
uint quantized_time_for_frame_absolute(uint frame);
Fingerprint(SubbandAnalysis* pSubbandAnalysis, int offset);
void Compute();
uint adaptiveOnsets(int ttarg, matrix_u&out, uint*&onset_counter_for_band) ;
std::vector<FPCode>& getCodes(){return _Codes;}
std::vector<FPCode>& getCodes() {return _Codes;}
protected:
SubbandAnalysis *_pSubbandAnalysis;
std::unique_ptr<SubbandAnalysis> _pSubbandAnalysis;
int _Offset;
std::vector<FPCode> _Codes;
};
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ OPTFLAGS=-O3 -DBOOST_UBLAS_NDEBUG -DNDEBUG
BOOST_CFLAGS=-I/usr/local/include/boost-1_35
TAGLIB_CFLAGS=`taglib-config --cflags`
TAGLIB_LIBS=`taglib-config --libs`
CXXFLAGS=-Wall $(BOOST_CFLAGS) $(TAGLIB_CFLAGS) -fPIC $(OPTFLAGS)
CXXFLAGS=-std=c++11 -Wall $(BOOST_CFLAGS) $(TAGLIB_CFLAGS) -fPIC $(OPTFLAGS)
CFLAGS=-Wall -fPIC $(OPTFLAGS)
LDFLAGS=$(TAGLIB_LIBS) -lz -lpthread $(OPTFLAGS)
LIBNAME=libcodegen.so
Expand Down
4 changes: 2 additions & 2 deletions src/MatrixUtility.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace MatrixUtility {

bool TextFileOutput(const matrix_f& A, const char* filename) {
FILE *matrix_file = fopen(filename, "w");
bool success = (matrix_file != NULL);
bool success = (matrix_file != nullptr);
if (success) {
const float *d = &A.data()[0];
for (uint i = 0; i < A.size1(); i++) {
Expand All @@ -29,7 +29,7 @@ bool TextFileOutput(const matrix_f& A, const char* filename) {

bool FileOutput(const matrix_f& A, const char* filename) {
FILE *matrix_file = fopen(filename, "wb");
bool success = (matrix_file != NULL);
bool success = (matrix_file != nullptr);
if (success) {
uint mm = A.size1();
uint mn = A.size2();
Expand Down
8 changes: 4 additions & 4 deletions src/Metadata.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ Metadata::Metadata(const string& file) : _Filename(file), _Artist(""), _Album(""
// TODO: Consider removing the path from the filename -- not sure if we can do this in a platform-independent way.
TagLib::FileRef f(_Filename.c_str());

TagLib::Tag* tag = f.isNull() ? NULL : f.tag();
if (tag != NULL) {
TagLib::Tag* tag = f.isNull() ? nullptr : f.tag();
if (tag) {
_Artist = tag->artist().to8Bit(true);
_Album = tag->album().to8Bit(true);
_Title = tag->title().to8Bit(true);
_Genre = tag->genre().to8Bit(true);
}

TagLib::AudioProperties* properties = f.isNull() ? NULL : f.audioProperties();
if (properties != NULL) {
TagLib::AudioProperties* properties = f.isNull() ? nullptr : f.audioProperties();
if (properties) {
_Bitrate = properties->bitrate();
_SampleRate = properties->sampleRate();
_Seconds = properties->length();
Expand Down
Loading