void CSLAbstComponent::recordOnOff() { if (recrding) { // stop recording and write output file recrding = false; // reset flag checked in audioDeviceIOCallback if (gFileBuffer->mNumFrames == 0) return; string outName = CGestalt::sndFileName(); // write to snd file logMsg("Write %5.3f sec (%d ksamp) to file \"%s\"\n", ((float) gSampIndex / CGestalt::frameRate()), gSampIndex / 1000, outName.c_str()); SoundFile * ioFile = new SoundFile(outName); ioFile->openForWrite(kSoundFileFormatAIFF, 2); ioFile->writeBuffer(*gFileBuffer); // snd file write of record buffer ioFile->close(); delete ioFile; } else { // start recording if ( ! gFileBuffer) { // allocate buffer first time gFileBuffer = new Buffer(2, SAMPS_TO_WRITE + CGestalt::maxBufferFrames()); gFileBuffer->allocateBuffers(); } recrding = true; // set flag checked in audioDeviceIOCallback } gSampIndex = 0; // reset write ptr gFileBuffer->mNumFrames = gSampIndex; }
//////////////////////////////////////////////////////////// /// Create a new sound from a file, for writing //////////////////////////////////////////////////////////// SoundFile* SoundFile::CreateWrite(const std::wstring& Filename, unsigned int ChannelsCount, unsigned int SampleRate) { // Create the file according to its type SoundFile* File = NULL; if (SoundFileOgg::IsFileSupported(Filename, false)) File = new SoundFileOgg; else if (SoundFileDefault::IsFileSupported(Filename, false)) File = new SoundFileDefault; // Open it for writing if (File) { if (File->OpenWrite(Filename, ChannelsCount, SampleRate)) { File->myFilename = L""; File->myData = NULL; File->mySize = 0; File->myNbSamples = 0; File->myChannelsCount = ChannelsCount; File->mySampleRate = SampleRate; } else { delete File; File = NULL; } } return File; }
//////////////////////////////////////////////////////////// /// Create a new sound from a file in memory, for reading //////////////////////////////////////////////////////////// SoundFile* SoundFile::CreateRead(const char* Data, std::size_t SizeInMemory) { // Create the file according to its type SoundFile* File = NULL; if (SoundFileOgg::IsFileSupported(Data, SizeInMemory)) File = new SoundFileOgg; else if (SoundFileDefault::IsFileSupported(Data, SizeInMemory)) File = new SoundFileDefault; // Open it for reading if (File) { std::size_t SamplesCount; unsigned int ChannelsCount; unsigned int SampleRate; if (File->OpenRead(Data, SizeInMemory, SamplesCount, ChannelsCount, SampleRate)) { File->myFilename = L""; File->myData = Data; File->mySize = SizeInMemory; File->myNbSamples = SamplesCount; File->myChannelsCount = ChannelsCount; File->mySampleRate = SampleRate; } else { delete File; File = NULL; } } return File; }
TimeStretcher::TimeStretcher(const SoundFile &soundFile) { this->soundFile = soundFile; channels = soundFile.getChannels(); inputSamples = soundFile.getSamples(); stretchInBufL.resize(maxProcessSize); stretchInBufR.resize(maxProcessSize); stretchInBuf.resize(channels); stretchInBuf[0] = &(stretchInBufL[0]); stretchInBuf[1] = &(stretchInBufR[0]); stretchOutBufL.resize(maxProcessSize); stretchOutBufR.resize(maxProcessSize); stretchOutBuf.resize(channels); stretchOutBuf[0] = &(stretchOutBufL[0]); stretchOutBuf[1] = &(stretchOutBufR[0]); rubberband = new RubberBand::RubberBandStretcher( soundFile.getSampleRate(), channels, RubberBand::RubberBandStretcher::DefaultOptions | RubberBand::RubberBandStretcher::OptionProcessRealTime); rubberband->setMaxProcessSize(maxProcessSize); playheadPos = 0; }
static VALUE SoundFile_is_finished(VALUE self) { SoundFile *soundfile; Data_Get_Struct(self, SoundFile, soundfile); return (soundfile->is_finished())? Qtrue : Qfalse; }
static VALUE SoundFile_restart(VALUE self) { SoundFile *ugen; Data_Get_Struct(self, SoundFile, ugen); ugen->reset(); return Qtrue; }
//////////////////////////////////////////////////////////// /// Create a new sound from a file, for reading //////////////////////////////////////////////////////////// SoundFile* SoundFile::CreateRead(const std::wstring& Filename) { // Create the file according to its type SoundFile* File = NULL; if (SoundFileOgg::IsFileSupported(Filename, true)) File = new SoundFileOgg; else if (SoundFileDefault::IsFileSupported(Filename, true)) File = new SoundFileDefault; // Open it for reading if (File) { std::size_t SamplesCount; unsigned int ChannelsCount; unsigned int SampleRate; if (File->OpenRead(Filename, SamplesCount, ChannelsCount, SampleRate)) { File->myFilename = Filename; File->myData = NULL; File->mySize = 0; File->myNbSamples = SamplesCount; File->myChannelsCount = ChannelsCount; File->mySampleRate = SampleRate; } else { delete File; File = NULL; } } return File; }
static VALUE SoundFile_duration(VALUE self) { SoundFile *ugen; Data_Get_Struct(self, SoundFile, ugen); StkFloat duration = ugen->durationInSeconds(); return rb_float_new(duration); }
static VALUE SoundFile_duration_samples(VALUE self) { SoundFile *ugen; Data_Get_Struct(self, SoundFile, ugen); unsigned long duration = ugen->durationInSamples(); return LONG2NUM(duration); }
void ISoundDevice::play(const char *filename, Dumais::Sound::SoundFormat format) { if (this->getSampleSize() == 0) return; LOG("Adding " << filename << " in sound device queue"); SoundFile *file = new SoundFile(); if (file->open(filename,format)) { this->mSoundQueue.put(file); this->setWorking(); } else { delete file; } }
void ConcatService::concatFiles(vector<SoundFile*> soundFiles) { SoundFile soundFile = *soundFiles[0]; int i; for(i = 1; i < numberOfSoundFiles; i++) { soundFile += soundFiles[i]; } if(outputFileName.compare(" ")) { if((outputFileName.find(".cs229") == string::npos) ) { __throw_invalid_argument("Invalid output file, must use an output file that ends with .cs229"); } } soundFile.writeCS229File(outputFileName); }
void GData::saveActiveFile() { SoundFile *s = getActiveSoundFile(); if(!s) return; if(s->saved()) return; if(audioThread.playSoundFile() == s || audioThread.recSoundFile() == s) { stop(); } int val = saveFile(s, saveFileAsk(s->filename)); if(val == 0) { //success emit channelsChanged(); } else if(val == -1) { QMessageBox::warning(mainWindow, "Error", QString("Error saving file '") + QString(s->filename) + QString("'"), QMessageBox::Ok, Qt::NoButton); } }
void ReorderDialog::fillWidget(int selection){ ui->listWidget->clear(); QStringList strings; const QList<SoundFile *> *files = list->getList(); QList<SoundFile *>::const_iterator iter; for(iter = files->begin(); iter != files->end(); iter++){ SoundFile *file = *iter; strings << file->getDescription(); } ui->listWidget->addItems(strings); ui->listWidget->setSelectionMode(QAbstractItemView::SingleSelection); ui->listWidget->setCurrentRow(selection); }
static VALUE SoundFile_set_rate(VALUE self, VALUE rate) { // get the Unit instance SoundFile *ugen; Data_Get_Struct(self, SoundFile, ugen); if(TYPE(rate) == T_FLOAT || TYPE(rate) == T_FIXNUM){ float rate_f = (float) NUM2DBL(rate); // set the frequency ugen->setRate(rate_f); // store what was set in the instance variable rb_iv_set(self, "@rate", rate); } return rate; }
void AudioManager::LoadMusic( const tstring& path, const tstring& name, float32 volume, uint8 channel ) { Logger::GetInstance()->Log(mSoundService != nullptr, _T("Sound Service is invalid."), STARENGINE_LOG_TAG); if(mMusicList.find(name) != mMusicList.end()) { Logger::GetInstance()->Log(LogLevel::Warning, _T("Sound Service: The music file '") + name + _T("' is already loaded."), STARENGINE_LOG_TAG); return; } auto pathit = mMusicPathList.find(path); if(pathit != mMusicPathList.end()) { star::Logger::GetInstance()->Log(LogLevel::Warning, _T("Sound Service : Sound File Path Already Exists"), STARENGINE_LOG_TAG); tstring nameold = pathit->second; auto nameit = mMusicList.find(nameold); if(nameit != mMusicList.end()) { star::Logger::GetInstance()->Log(LogLevel::Warning, _T("Sound Service: Found sound file of old path, making copy for new name"), STARENGINE_LOG_TAG); mMusicList[name] = nameit->second; return; } mMusicPathList.erase(pathit); return; } SoundFile* music = new SoundFile(path, channel); music->SetCompleteVolume( volume, GetChannelVolume(channel), GetVolume() ); mMusicList[name] = music; mMusicPathList[path] = name; return; }
static VALUE SoundFile_open(VALUE self, VALUE filename) { // We want a string here... Check_Type(filename, T_STRING); // get the Unit instance SoundFile *ugen; Data_Get_Struct(self, SoundFile, ugen); // Open the file, rethrowing errors as necessary try { ugen->open(StringValuePtr(filename)); } catch(StkError &error) { return self; } return self; }
static VALUE SoundFile_initialize(int argc, VALUE *argv, VALUE self) { VALUE options; /* One mandatory, one optional argument */ rb_scan_args(argc, argv, "01", &options); SoundFile *soundfileugen; Data_Get_Struct(self, SoundFile, soundfileugen); if(options != Qnil){ Check_Type(options, T_HASH); VALUE filename = rb_hash_aref(options, ID2SYM(rb_intern("file"))); soundfileugen->open(StringValuePtr(filename)); } return self; }
SoundFile* AudioOutputSample::loadSndfile(const QString &filename) { SoundFile *sf; // Create the filehandle and do a quick check if everything is ok sf = new SoundFile(filename); if (! sf->isOpen()) { qWarning() << "File " << filename << " failed to open"; delete sf; return NULL; } if (sf->error() != SF_ERR_NO_ERROR) { qWarning() << "File " << filename << " couldn't be loaded: " << sf->strError(); delete sf; return NULL; } if (sf->channels() <= 0 || sf->channels() > 2) { qWarning() << "File " << filename << " contains " << sf->channels() << " Channels, only 1 or 2 are supported."; delete sf; return NULL; } return sf; }
SoundFileListDialog::SoundFileListDialog(QWidget *parent, SoundFileList *list) : QDialog(parent), ui(new Ui::SoundFileListDialog) { qDebug("Entering SoundFileListDialog::SoundFileListDialog()..."); ui->setupUi(this); this->list = list; QStringList strings; const QList<SoundFile *> *files = list->getList(); QList<SoundFile *>::const_iterator iter; for(iter = files->begin(); iter != files->end(); iter++){ SoundFile *file = *iter; strings << file->getDescription(); } ui->listWidget->addItems(strings); ui->listWidget->setSelectionMode(QAbstractItemView::SingleSelection); connect(ui->listWidget, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(accept())); }
ALuint SoundManager::load_file_into_buffer(SoundFile& file) { ALenum format = get_sample_format(file); ALuint buffer; alGenBuffers(1, &buffer); check_al_error("Couldn't create audio buffer: "); std::unique_ptr<char[]> samples(new char[file.size]); file.read(samples.get(), file.size); alBufferData(buffer, format, samples.get(), static_cast<ALsizei>(file.size), static_cast<ALsizei>(file.rate)); check_al_error("Couldn't fill audio buffer: "); return buffer; }
void AbcFile::writeToCs229File(string outputFile) { SoundFile * soundFile = createSoundFile(); soundFile->writeCS229File(outputFile); }
SoundFile* AbcFile::createSoundFile() { SoundFile * soundFile = new SoundFile(); soundFile->setBitDepth(bitRate); soundFile->setSampleRate(sampleRate); soundFile->setNumberOfChannels((int)instruments.size()); int samplesPerCount = (int)ceil(60.0*sampleRate/tempo); for(int i = 0; i < instruments.size(); i++) { SoundFile * otherChannels = new SoundFile(); otherChannels->setBitDepth(bitRate); otherChannels->setSampleRate(sampleRate); otherChannels->setNumberOfChannels((int)instruments.size()); SoundGenerator::SoundBuilder builder; builder.setAttack(instruments[i]->getAttack()).setDecay(instruments[i]->getDecay()).setRelease(instruments[i]->getRelease()).setDuration((instruments[i]->findTotalCount() * 60 / tempo)).setSampleRate(sampleRate).setSustain(instruments[i]->getSustain()); int numSamples = 0; for(int j = 0; j < instruments[i]->getScore().size(); j++) { SoundFile * tempFile = new SoundFile(); tempFile->setBitDepth(bitRate); tempFile->setSampleRate(sampleRate); SoundGenerator soundGenerator = builder.setDuration((instruments[i]->getScore()[j]->getCount() * 60 / tempo)).setBitDepth(bitRate).build(); tempFile->setNumberOfChannels((int)instruments.size()); int waveForm = instruments[i]->getWaveform(); if(waveForm == 1) { numSamples +=sineWave(instruments[i]->getScore()[j],samplesPerCount,tempFile); } else if(waveForm == 2) { numSamples +=sawtoothWave(instruments[i]->getScore()[j],samplesPerCount,tempFile); } else if (waveForm == 3) { numSamples +=triangleWave(instruments[i]->getScore()[j],samplesPerCount,tempFile); } else { numSamples +=pulseWave(instruments[i]->getScore()[j],samplesPerCount,tempFile); } soundGenerator.handleEnvelop(tempFile); if(i == 0) { *soundFile += tempFile; } else { *otherChannels += tempFile; } } for (int k = numSamples + 1; k < getMaxSamples(instruments); ++k) { SampleLine *sampleLine = new SampleLine(); sampleLine->addNewChannel(0); if(i == 0) { soundFile->addSample(sampleLine); } else { otherChannels->addSample(sampleLine); } } if(std::find(mute.begin(),mute.end(),i) != mute.end()) { if(i == 0) { *soundFile * 0; } else { *otherChannels * 0; } } if(i != 0) { *soundFile | otherChannels; } } return soundFile; }
int main(int argc, char* argv[]) { #ifdef TESTS cout << "===== RUNNING TESTS =====" << endl; RegressionTest::runAllTests(); TestConvolver::runAllTests(); cout << "==== TESTS COMPLETED ====" << endl; system("pause"); #endif /* Ensure that valid dry, IR, and output file names are provided */ if (! checkArgs(argc, argv)) return 1; /* Read wave files */ SoundFile* dry = SoundFile::create(argv[1]); SoundFile* ir = SoundFile::create(argv[2]); /* End program if any input file is corrupted or nonexistant */ if (dry == nullptr || ir == nullptr) return 1; int dryDataSize = dry->getDataSize(); int irDataSize = ir->getDataSize(); cout << "DR Size: " << dryDataSize << " IR Size: " << irDataSize << endl; int P; // Length of result short* result; // Resulting data of convolution to write to file /* Normalize the input data to -1 and +1 */ double* dryNormalized = new double[dryDataSize]; double* irNormalized = new double[irDataSize]; Convolver::dataToSignal(dry->getData(), dryDataSize, dry->getAbsMinValue(), dryNormalized); Convolver::dataToSignal(ir->getData(), irDataSize, ir->getAbsMinValue(), irNormalized); if (ir->getNumChannels() == 1) { P = dryDataSize + irDataSize - 1; /* Perform time domain convolution */ result = new short[P]; #ifndef FFT Convolver::convolve(dryNormalized, dryDataSize, irNormalized, irDataSize, result, P); #else Convolver::fftConvolve(dryNormalized, dryDataSize, irNormalized, irDataSize, result, P); #endif /* Save resulting .wav file */ SoundFile::save(argv[3], ir->getNumChannels(), dry->getBitsPerSample(), dry->getSampleRate(), result, P); } else if (ir->getNumChannels() == 2) { /* Calculate half size of impulse response ONCE and store in halfM */ int halfM = irDataSize >> 1; /* P = N+(M/2)-1, where P is the length of each half of this two-channel convolution */ P = dryDataSize + halfM - 1; /* Split normalized stereo impulse response into left and right channels */ double* irLeftNormalized = new double[halfM]; double* irRightNormalized = new double[halfM]; for (int i = 0, i2 = 0; i < halfM; i++) { i2 = i << 1; irLeftNormalized[i] = irNormalized[i2]; irRightNormalized[i] = irNormalized[i2 + 1]; } /* Perform time domain convolution */ short* resultLeft = new short[P]; short* resultRight = new short[P]; #ifndef FFT Convolver::convolve(dryNormalized, dryDataSize, irLeftNormalized, halfM, resultLeft, P); Convolver::convolve(dryNormalized, dryDataSize, irRightNormalized, halfM, resultRight, P); #else Convolver::fftConvolve(dryNormalized, dryDataSize, irLeftNormalized, halfM, resultLeft, P); Convolver::fftConvolve(dryNormalized, dryDataSize, irRightNormalized, halfM, resultRight, P); #endif /* Interleave left and right channel data */ result = new short[P*2]; for (int i = 0, halfI = 0; i < P*2; i+=2) { halfI = i >> 1; result[i] = resultLeft[halfI]; result[i+1] = resultRight[halfI]; } /* Save resulting .wav file */ SoundFile::save(argv[3], ir->getNumChannels(), dry->getBitsPerSample(), dry->getSampleRate(), result, P*2); delete[] irLeftNormalized; delete[] irRightNormalized; delete[] resultLeft; delete[] resultRight; }
static VALUE SoundFile_name(VALUE self) { SoundFile *ugen; Data_Get_Struct(self, SoundFile, ugen); return rb_str_new2(ugen->name()); }