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 ofxAudioSample::load(string tmpPath, float _hSampleRate) { myPath = ofToDataPath(tmpPath,true).c_str(); SndfileHandle sndFile = SndfileHandle(myPath); myFormat = sndFile.format(); myChannels = sndFile.channels(); mySampleRate = sndFile.samplerate(); resampligFactor = _hSampleRate/mySampleRate; speed = mainSpeed/resampligFactor; bufferSize = 4096 * myChannels; readBuffer = new float[bufferSize]; ofVec2f _wF; int readcount; int readpointer; // convert all multichannel files to mono by averaging the channels float monoAverage; while(readcount = sndFile.readf(readBuffer, 4096)){ readpointer = 0; _wF.set(0,0); for (int i = 0; i < readcount; i++) { // for each frame... monoAverage = 0; for(int j = 0; j < myChannels; j++) { monoAverage += readBuffer[readpointer + j]; } monoAverage /= myChannels; readpointer += myChannels; // add the averaged sample to our vector of samples samples.push_back(monoAverage); // add to the waveform data _wF.x = MIN(_wF.x, monoAverage); _wF.y = MAX(_wF.y, monoAverage); } _waveForm.push_back(_wF); } position = 0; }
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); } }
int main () { // Damit das Programm funktioniert, muss eine 16Bit PCM Wave-Datei im // gleichen Ordner liegen ! const char * fname = "test.flac" ; // Soundfile-Handle aus der libsndfile-Bibliothek SndfileHandle file = SndfileHandle (fname) ; // Alle möglichen Infos über die Audio-Datei ausgeben ! std::cout << "Reading file: " << fname << std::endl; std::cout << "File format: " << file.format() << std::endl; std::cout << "PCM 16 BIT: " << (SF_FORMAT_WAV | SF_FORMAT_PCM_16) << std::endl; std::cout << "Samples in file: " << file.frames() << std::endl; std::cout << "Samplerate " << file.samplerate() << std::endl; std::cout << "Channels: " << file.channels() << std::endl; // Die RtAudio-Klasse ist gleichermassen dac und adc, wird hier aber nur als dac verwendet ! RtAudio dac; if ( dac.getDeviceCount() < 1 ) { std::cout << "\nNo audio devices found!\n"; return 0; } // Output params ... RtAudio::StreamParameters parameters; parameters.deviceId = dac.getDefaultOutputDevice(); parameters.nChannels = 2; parameters.firstChannel = 0; unsigned int sampleRate = 44100; // ACHTUNG! Frames != Samples // ein Frame = Samples für alle Kanäle // d.h. |Samples| = Kanäle*Frames ! unsigned int bufferFrames = 1024; // Da wir 16 Bit PCM-Daten lesen, sollte als Datenformat RTAUDIO_SINT16 genutzt // werden. // Als Daten wird der Callback-Struktur hier das Soundfile-Handle übergeben. // Sollte man in einer "ernsthaften" Lösung anders machen ! // Inkompatible Formate können übrigens "interessante" Effekte ergeben ! try { dac.openStream( ¶meters, NULL, RTAUDIO_SINT16, sampleRate, &bufferFrames, &fplay, (void *)&file); dac.startStream(); } catch ( RtAudioError& e ) { e.printMessage(); return 0; } char input; std::cout << "\nPlaying ... press <enter> to quit.\n"; std::cin.get( input ); try { // Stop the stream dac.stopStream(); } catch (RtAudioError& e) { e.printMessage(); } if ( dac.isStreamOpen() ) dac.closeStream(); return 0 ; }