void ofApp::setup() { ofSetVerticalSync(true); string filename = "Serato/Serato_CD.aif";// "TraktorMK2/Traktor_MK2_Scribble.wav"; string absoluteFilename = ofToDataPath(filename, true); SndfileHandle myf = SndfileHandle(absoluteFilename.c_str()); bufferFrames = myf.frames(); int n = bufferFrames * myf.channels(); floatBuffer.resize(n); curBuffer.resize(n); myf.read(&floatBuffer[0], n); bufferPosition = 0; ttmPosition = 0; relativePosition = 0; relativeTtm.resize(ofGetWidth()); absoluteTtm.resize(ofGetWidth()); pitchTtm.resize(ofGetWidth()); string timecode = "serato_cd"; // "serato_cd" "serato_a" "traktor_a" float speed = 1.0; // 1.0 is 33 1/3, 1.35 is 45 rpm int sampleRate = 44100; // myf.samplerate() timecoder_init(&timecoder, timecode.c_str(), speed, sampleRate); //timecoder_monitor_init(&timecoder, MIXXX_VINYL_SCOPE_SIZE); bufferSize = 256; exporting = true; soundStream.setup(this, 2, 0, sampleRate, bufferSize, 4); }
//-------------------------------------------------------------- void testApp::guiEvent(nativeWidget & widget){ ofDisableDataPath(); ofEnableDataPath(); if (widget.name == "newColor"){ float hue = ofRandom(0,255); float sat = ofRandom(190,230); float bri = ofRandom(220,238); color.setHsb(hue, sat, bri); } if (widget.name == "repeatSound"){ if (audioSamples.size() > 0){ counter = 0; bPlaying = true; } } if (widget.name == "textBox" || widget.name == "textBox2" || widget.name == "textBox3" ){ string time = ofGetTimestampString(); string fileName = time + ".aiff"; string fileNameMp3 = time + ".mp3"; string toSay = *((string *)widget.variablePtr); string command = "say -o " + ofToDataPath(fileName) + " " + "\"" + toSay + "\"" + " --data-format=BEI32@44100"; // big endian int 32 bit samples 44100 sample rate system(command.c_str()); ofSleepMillis(100); // sometimes really long files need time to save out. SndfileHandle myf = SndfileHandle(ofToDataPath(fileName).c_str()); float * data = new float[int(myf.frames())]; myf.read (data, int(myf.frames())); audioSamples.clear(); audioSamples.reserve(int(myf.frames())); for (int i = 0; i < int(myf.frames()); i++){ audioSamples.push_back(data[i]); } delete [] data; bPlaying = true; counter = 0; } computeMessageColors(); }
static void read_file (const char * fname) { static short buffer [BUFFER_LEN] ; SndfileHandle file ; file = SndfileHandle (fname) ; printf ("Opened file '%s'\n", fname) ; printf (" Sample rate : %d\n", file.samplerate ()) ; printf (" Channels : %d\n", file.channels ()) ; file.read (buffer, BUFFER_LEN) ; puts ("") ; /* RAII takes care of destroying SndfileHandle object. */ } /* read_file */
void Sound::initData(SndfileHandle sndFile) { nChannels = sndFile.channels(); nFrames = sndFile.frames(); int length = nChannels * nFrames; float buffer [length]; sndFile.read(buffer, length); data.resize(nChannels, std::vector<float>()); for(int channelNum = 0; channelNum < nChannels; ++channelNum) { data[channelNum].resize(nFrames, 0.0); for(int frameNum = 0; frameNum < nFrames; ++frameNum) { int i = frameNum * nChannels + channelNum; data[channelNum][frameNum] = buffer[i]; } } dataInitialized = true; }
/** General loading function. This is used by the both the load Samples and load Groundtruth */ void AudioManager::loadFiles (string dirname, vector<Sample> &into) { vector<string> filenames = list_all_files(dirname); for (vector<string>::iterator it = filenames.begin(); it != filenames.end(); ++it) { string filename = *it; if (filename.find(".wav") == string::npos) continue; Sample sample; SndfileHandle ff = SndfileHandle(filename); assert(ff.channels() == 1); assert(ff.samplerate() == 8000); double * signal = new double [ff.frames()]; ff.read(signal, ff.frames()); sample.station = ""; sample.offset = 0; sample.filename = filename; sample.audio = signal; sample.length = ff.frames(); into.push_back(sample); } }
/* * Method read input data from file */ int* read_file(string file_name, int *frames_length) { //file handler SndfileHandle inputFile; //read file from input WAV file inputFile = SndfileHandle(file_name); //get number of frames int frames = inputFile.frames(); //set input buffer int *buffer; buffer = new int[frames]; //read data from input file inputFile.read(buffer, frames); *frames_length = frames; return buffer; }
int main(int argc, char** argv) { if (argc != 2) { printHelp(); return EXIT_FAILURE; } SndfileHandle inputFile; inputFile = SndfileHandle(argv[1]); int framesCount = inputFile.frames(); int *buffer; buffer = new int[framesCount]; inputFile.read(buffer, inputFile.frames()); #ifdef DEBUG cerr<<"Samples size: "<<framesCount<<endl; #endif double firstAngle; double deltaAngle; string decoded = ""; /* finding angle of a sin between two samples in the input signal */ firstAngle = asin(((double)buffer[0]) / AMPLITUDE); deltaAngle = asin(((double)buffer[1]) / AMPLITUDE) - firstAngle; #ifdef DEBUG cerr<<"deltaAngle: "<<deltaAngle<<" = "<<deltaAngle*180/PI<<"°"<<endl; #endif double actAngle = firstAngle; double deltaX1 = deltaAngle; double deltaX4 = deltaAngle; /* QPSK bauds mapping values */ double expectedAngle1 = addAngle(0, 3*PI/4.0); double expectedAngle2 = addAngle(0, PI/4.0); double expectedAngle3 = addAngle(0, 5*PI/4.0); double expectedAngle4 = addAngle(0, 7*PI/4.0); int changes = 0; int firstChange = 0; int secondChange = 0; int thirdChange = 0; double expectedAngle1b = addNormalize(0, expectedAngle1); double expectedAngle4b = addNormalize(0, expectedAngle4); double *expectedAngle = &expectedAngle1b; /* while 3 changes in sync part of the signal signal */ while (changes < 3) { #ifdef DEBUG cerr<<"loop: "<<thirdChange<<endl; cerr<<"actAngle: "<<actAngle<<" = "<<actAngle*180/PI<<"° "<<sin(actAngle)<<endl; cerr<<"expected: "<<*expectedAngle<<" = "<<*expectedAngle*180/PI<<"° "<<sin(*expectedAngle)<<endl; #endif /* we have a change in a baud */ if (!((actAngle > *expectedAngle - (PI/20))&&(actAngle < *expectedAngle + (PI/20)))) { if (changes == 0) { expectedAngle = &expectedAngle4b; } else if (changes == 1) { expectedAngle = &expectedAngle1b; } changes++; } /* counting samples till first change */ if (changes < 1) { firstChange++; } /* counting samples till second change */ if (changes < 2) { secondChange++; } /* counting samples till third change */ if (changes < 3) { thirdChange++; } /* thirdChange is also a counter for this loop */ actAngle = asin(((double)buffer[thirdChange]) / AMPLITUDE); expectedAngle1b = addNormalizeChangeDelta(expectedAngle1b, &deltaX1); expectedAngle4b = addNormalizeChangeDelta(expectedAngle4b, &deltaX4); } /* preparing values for voting */ secondChange /= 2; thirdChange /=3; int samplesPerBaud = 0; /* voting for samles per baud... we should have at least 2 same * values to claim it as a samples per baud */ if (firstChange == secondChange) { samplesPerBaud = firstChange; } else if (secondChange == thirdChange) { samplesPerBaud = secondChange; } else if (firstChange == thirdChange) { samplesPerBaud = firstChange; } #ifdef DEBUG cerr<<"SPB: "<<samplesPerBaud<< " 1st: "<<firstChange<<" 2nd: "<<secondChange<<" 3rd: "<<thirdChange<<endl; #endif double res1 = 0; double res2 = 0; double res3 = 0; double res4 = 0; /* Iteraes through the all samples. For each baud we are looking * for the sin values that differs the least from actual baud */ for (int i = 0, s = samplesPerBaud; i < framesCount; i++, s--) { /* counting differences for each sin */ res1 += fabs(buffer[i] - sin(expectedAngle1)); res2 += fabs(buffer[i] - sin(expectedAngle2)); res3 += fabs(buffer[i] - sin(expectedAngle3)); res4 += fabs(buffer[i] - sin(expectedAngle4)); /* we have processed a baud and we are looking for the * least change */ if (s == 0 || i == framesCount-1) { s = samplesPerBaud; double res12; double res34; int resId12 = 0; int resId34 = 0; if (res1 < res2) { res12 = res1; resId12 = 1; } else { res12 = res2; resId12 = 2; } if (res3 < res4) { res34 = res3; resId34 = 3; } else { res34 = res4; resId34 = 4; } int resId; if (res12 < res34) { resId = resId12; } else { resId = resId34; } switch(resId) { case 1: decoded += "00"; break; case 2: decoded += "01"; break; case 3: decoded += "10"; break; case 4: decoded += "11"; break; default: break; } res1 = 0; res2 = 0; res3 = 0; res4 = 0; } expectedAngle1 = addAngle(expectedAngle1, -deltaAngle); expectedAngle2 = addAngle(expectedAngle2, -deltaAngle); expectedAngle3 = addAngle(expectedAngle3, -deltaAngle); expectedAngle4 = addAngle(expectedAngle4, -deltaAngle); } #ifdef DEBUG cerr<<decoded<<endl; cerr<<decoded.substr(8); #endif /* writting to an output file */ string outName = string(argv[1]); outName = outName.replace(outName.end()-3, outName.end(), "txt"); ofstream outFile(outName); if (!outFile.is_open()) { cerr<<"Can not open output file\n"; return EXIT_FAILURE; } outFile<<decoded.substr(8); outFile.close(); delete [] buffer; return EXIT_SUCCESS; }