Ejemplo n.º 1
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;
    
}
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 ;

}