static int buffer_read_verify(SndfileHandle const & sf, size_t min_length, size_t samplerate)
{
    if (!sf)
        return -1;
    if (sf.frames() < min_length)
        return -2; /* no more frames to read */
    if (sf.samplerate() != samplerate)
        return -3; /* sample rate mismatch */
    return 0;
}
Esempio n. 2
0
//--------------------------------------------------------------
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;
    
}
Esempio n. 3
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 */
Esempio n. 4
0
/**
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( &parameters, 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 ;

}