void CtrlrFileDownloader::run() { ScopedPointer <InputStream> is (fileToDownload.createInputStream (false)); const int totalLength = is->getTotalLength(); int bytesSoFar = 0; const String packageFile = CtrlrUpdateManager::getMyPackage(); if (outputFile.exists()) { outputFile.deleteFile(); outputFile.create(); } while (! (is->isExhausted())) { if (threadShouldExit()) { return; } MemoryBlock buffer(8192); const int num = is->read (buffer.getData(), 8192); if (num == 0) break; bytesSoFar += num; outputFile.appendData(buffer.getData(), buffer.getSize()); setStatusMessage ("File: " + packageFile + "\nTotal size: " + File::descriptionOfSizeInBytes (totalLength) + " Got size: " + File::descriptionOfSizeInBytes (bytesSoFar)); setProgress (bytesSoFar / (double)totalLength); } }
void YSE::INTERNAL::soundFile::loadNonStreaming() { // load non streaming sounds in one go ScopedPointer<AudioFormatReader> reader; File file; if (IO().getActive()) { // will be deleted by AudioFormatReader customFileReader * cfr = new customFileReader; cfr->create(fileName.c_str()); reader = SOUND::Manager().getReader(cfr); } else { file = File::getCurrentWorkingDirectory().getChildFile(juce::String(fileName)); reader = SOUND::Manager().getReader(file); } if (reader != nullptr) { _fileBuffer.setSize(reader->numChannels, (Int)reader->lengthInSamples); reader->read(&_fileBuffer, 0, (Int)reader->lengthInSamples, 0, true, true); // sample rate adjustment _sampleRateAdjustment = static_cast<Flt>(reader->sampleRate) / static_cast<Flt>(SAMPLERATE); _length = _fileBuffer.getNumSamples(); _buffer = _fileBuffer.getArrayOfReadPointers(); _channels = _fileBuffer.getNumChannels(); // file is ready for use now state = READY; } else { LogImpl().emit(E_FILEREADER, "Unable to read " + file.getFullPathName().toStdString()); state = INVALID; } }
Oscillator::Oscillator (double sampleRate) : sampRate(sampleRate), squareBuffer(1, 22071), sawBuffer(1, 21984), sawDownBuffer(1, 21984) { currentPhase = 0.0; currentSample = 0.0; stepSize = 0; squareBuffer.clear(); sawBuffer.clear(); sawDownBuffer.clear(); sharedMemory.enter(); AudioFormatManager formatManager; formatManager.registerBasicFormats(); ScopedPointer <AudioFormatReader> squareReader (formatManager.createReaderFor(new MemoryInputStream (MainBinaryData::squarewave20500_wav, MainBinaryData::squarewave20500_wavSize, false))); squareReader->read(&squareBuffer, 0, squareReader->lengthInSamples, 0, true, false); squareNumSamples = squareReader->lengthInSamples; ScopedPointer <AudioFormatReader> sawReader (formatManager.createReaderFor(new MemoryInputStream (MainBinaryData::sawwave20500_wav, MainBinaryData::sawwave20500_wavSize, false))); sawReader->read(&sawBuffer, 0, sawReader->lengthInSamples, 0, true, false); ScopedPointer <AudioFormatReader> sawDownReader (formatManager.createReaderFor(new MemoryInputStream (MainBinaryData::sawdownwave20500_wav, MainBinaryData::sawdownwave20500_wavSize, false))); sawDownReader->read(&sawDownBuffer, 0, sawDownReader->lengthInSamples, 0, true, false); sawNumSamples = sawReader->lengthInSamples; sharedMemory.exit(); }
AudioSampleBuffer EssentiaExtractor::audioFileToSampleBuffer(const File audioFile) { //Read audio into buffer ScopedPointer<AudioFormatReader> reader; reader = formatManager->createReaderFor(audioFile); AudioSampleBuffer buffer(reader->numChannels, reader->lengthInSamples); reader->read(&buffer, 0, reader->lengthInSamples, 0, true, true); return buffer; }
// Read an audio file and resample void BirdID::readAudioFileResampled(const File &audioFile_, float targetSampleRate) { //Creating a reader for the file, depending on its format ScopedPointer<AudioFormatReader> fileReader = formatManager.createReaderFor(audioFile_); //int numChannels = fileReader->numChannels; int64 numSamples = fileReader->lengthInSamples; ScopedPointer<AudioSampleBuffer> leftChannelBuffer = new AudioSampleBuffer(1,static_cast<int>(numSamples)); leftChannelBuffer->clear(); float sampleRate = static_cast<float>(fileReader->sampleRate); float conversionFactor = sampleRate/targetSampleRate; resampledAudioLength = static_cast<int>(floorf(numSamples/conversionFactor)); // Initialize destination buffer resampledAudio = new float[resampledAudioLength]; // Cheating by reading only one channel //if(numChannels == 1) //{ // Read audio fileReader->read(leftChannelBuffer,0,static_cast<int>(numSamples),0,true,false); // Resample interpolator->process(conversionFactor,leftChannelBuffer->getReadPointer(0),resampledAudio,resampledAudioLength); // Normalize resampledAudio float max = resampledAudio[0]; for(int i=1;i<resampledAudioLength;i++) { if(fabs(resampledAudio[i])>fabs(max)) { max = resampledAudio[i]; } } max = fabs(max); for(int i=0;i<resampledAudioLength;i++) { resampledAudio[i] /= max; } }
bool FileWatcherThread::readDataFile(AudioSampleBuffer *buffer, File file) { AudioFormatManager formatManager; formatManager.registerBasicFormats(); ScopedPointer<AudioFormatReader> reader = formatManager.createReaderFor(file); if (reader != 0) { int sampleCount = (int) reader->lengthInSamples; buffer->setSize(kNumSampleChannels, sampleCount, false, true, false); reader->read(buffer, 0, sampleCount, 0, true, true); if (reader->numChannels != kNumSampleChannels) { printf("File is mono, copying data to second channel\n"); buffer->copyFrom(1, 0, *buffer, 0, 0, sampleCount); } return true; } return false; }
void FileLogger::trimFileSize (int maxFileSizeBytes) const { if (maxFileSizeBytes <= 0) { logFile.deleteFile(); } else { const int64 fileSize = logFile.getSize(); if (fileSize > maxFileSizeBytes) { ScopedPointer <FileInputStream> in (logFile.createInputStream()); jassert (in != nullptr); if (in != nullptr) { in->setPosition (fileSize - maxFileSizeBytes); String content; { MemoryBlock contentToSave; contentToSave.setSize ((size_t) maxFileSizeBytes + 4); contentToSave.fillWith (0); in->read (contentToSave.getData(), maxFileSizeBytes); in = nullptr; content = contentToSave.toString(); } int newStart = 0; while (newStart < fileSize && content[newStart] != '\n' && content[newStart] != '\r') ++newStart; logFile.deleteFile(); logFile.appendText (content.substring (newStart), false, false); } } } }
//============================================================================== bool AudioLassoAudioProcessor::setAudioFile(const String& audioFileAbsPath) { ScopedPointer<File> audioFile = new File(audioFileAbsPath); if (!audioFile->existsAsFile()) { std::cout << "!audioFile->existsAsFile()" << std::endl; return false; } ScopedPointer<AudioFormatReader> audioFormatReader = audioFormatManager->createReaderFor(*audioFile); int numChannels = audioFormatReader->numChannels; int numSamples = audioFormatReader->lengthInSamples; float sampleRate = audioFormatReader->sampleRate; AudioSampleBuffer* sourceAudio = new AudioSampleBuffer(numChannels, numSamples); audioFormatReader->read(sourceAudio, 0, numSamples, 0, true, true); const CriticalSection& lock = getCallbackLock(); lock.enter(); specHandler->setSourceAudio(sourceAudio, sampleRate); lock.exit(); return true; }
void YSE::INTERNAL::soundFile::run() { if (_streaming) { if (source == nullptr) { if (IO().getActive()) { // will be deleted by AudioFormatReader customFileReader * cfr = new customFileReader; cfr->create(fileName.c_str()); streamReader = SOUND::Manager().getReader(cfr); } else { streamReader = SOUND::Manager().getReader(file); } } else { streamReader = SOUND::Manager().getReader(source); } if (streamReader != nullptr) { _buffer.setSize(streamReader->numChannels, STREAM_BUFFERSIZE); // sample rate adjustment _sampleRateAdjustment = static_cast<Flt>(streamReader->sampleRate) / static_cast<Flt>(SAMPLERATE); _length = (Int)streamReader->lengthInSamples; _streamPos = 0; fillStream(false); // file is ready for use now state = READY; return; } else { LogImpl().emit(E_FILEREADER, "Unable to read " + file.getFullPathName().toStdString()); state = INVALID; return; } } // load non streaming sounds in one go ScopedPointer<AudioFormatReader> reader; if (source == nullptr) { if (IO().getActive()) { // will be deleted by AudioFormatReader customFileReader * cfr = new customFileReader; cfr->create(fileName.c_str()); reader = SOUND::Manager().getReader(cfr); } else { reader = SOUND::Manager().getReader(file); } } else { reader = SOUND::Manager().getReader(source); } if (reader != nullptr) { _buffer.setSize(reader->numChannels, (Int)reader->lengthInSamples); reader->read(&_buffer, 0, (Int)reader->lengthInSamples, 0, true, true); // sample rate adjustment _sampleRateAdjustment = static_cast<Flt>(reader->sampleRate) / static_cast<Flt>(SAMPLERATE); _length = _buffer.getNumSamples(); // file is ready for use now state = READY; return; } else { LogImpl().emit(E_FILEREADER, "Unable to read " + file.getFullPathName().toStdString()); state = INVALID; return; } }