// reset resets the configuration // void Coordinator::reset() { if (getConfiguration()) { // reset the sound files for (unsigned i = 0; i < sound.size(); i++) { if (sound[i]->relFileName() && strcmp(soundFile((ModelSound)i), sound[i]->relFileName())) sound[i]->change(soundFile((ModelSound)i)); } } }
void WaveformWidget::setSoundFile(QString filename) { QPainterPath waveformMax(QPointF(0,0)); QPainterPath waveformMin(QPointF(0,0)); SndfileHandle soundFile(filename.toStdString()); if (soundFile.error()) qDebug() << soundFile.strError(); QDesktopWidget *desk = QApplication::desktop(); int totalPixelCount = (desk->screenGeometry().width()*desk->screenCount()); sf_count_t framesPerPixel = soundFile.frames()/totalPixelCount; Q_ASSERT(framesPerPixel >= 1); float samples[framesPerPixel*soundFile.channels()]; sf_count_t totalFrames = 0; while (soundFile.readf(samples, framesPerPixel) != 0) { float maximum = 0; float minimum = 0; for (int f=0; f<framesPerPixel; f++) { for (int c=0; c<soundFile.channels(); c++) { maximum = samples[f*c+c] > maximum ? samples[f*c+c] : maximum; minimum = samples[f*c+c] < minimum ? samples[f*c+c] : minimum; } } waveformMax.lineTo(double(totalFrames)/(framesPerPixel*totalPixelCount), maximum); waveformMin.lineTo(double(totalFrames)/(framesPerPixel*totalPixelCount), minimum); totalFrames += framesPerPixel; } maxWave.swap(waveformMax); minWave.swap(waveformMin); }
bool KNotify::notifyBySound(const QString &sound, const QString &appname, int eventId) { if(sound.isEmpty()) { soundFinished(eventId, NoSoundFile); return false; } bool external = d->useExternal && !d->externalPlayer.isEmpty(); // get file name QString soundFile(sound); if(QFileInfo(sound).isRelative()) { QString search = QString("%1/sounds/%2").arg(appname).arg(sound); soundFile = KGlobal::instance()->dirs()->findResource("data", search); if(soundFile.isEmpty()) soundFile = locate("sound", sound); } if(soundFile.isEmpty()) { soundFinished(eventId, NoSoundFile); return false; } // kdDebug() << "KNotify::notifyBySound - trying to play file " << soundFile << endl; if(!external) { #ifdef WITH_PULSEAUDIO d->pulsePlayer.play(soundFile.utf8()); return true; #else soundFinished(eventId, NoSoundSupport); return false; #endif } else if(!d->externalPlayer.isEmpty()) { // use an external player to play the sound KProcess *proc = d->externalPlayerProc; if(!proc) { proc = d->externalPlayerProc = new KProcess; connect(proc, SIGNAL(processExited(KProcess *)), SLOT(slotPlayerProcessExited(KProcess *))); } if(proc->isRunning()) { soundFinished(eventId, PlayerBusy); return false; // Skip } proc->clearArguments(); (*proc) << d->externalPlayer << QFile::encodeName(soundFile); d->externalPlayerEventId = eventId; proc->start(KProcess::NotifyOnExit); return true; } soundFinished(eventId, Unknown); return false; }
/** * Plays a sound definition file (*.sdf) through PC speaker. SDF files * should reside in the gfiles dir. The only params passed to function are * filename and false if playback is unabortable, true if it is abortable. If no * extension then .SDF is appended. A full path to file may be specified to * override gfiles dir. Format of file is: * * <freq> <duration in ms> [pause_delay in ms] * 1000 1000 50 * * Returns 1 if sucessful, else returns 0. The pause_delay is optional and * is used to insert silences between tones. */ bool play_sdf(const std::string soundFileName, bool abortable) { WWIV_ASSERT(!soundFileName.empty()); std::string fullPathName; // append gfilesdir if no path specified if (soundFileName.find(WWIV_FILE_SEPERATOR_CHAR) == std::string::npos) { std::ostringstream ss; ss << syscfg.gfilesdir << soundFileName; fullPathName = ss.str(); } else { fullPathName = soundFileName; } // append .SDF if no extension specified if (fullPathName.find('.') == std::string::npos) { fullPathName += ".sdf"; } // Must Exist if (!WFile::Exists(fullPathName)) { return false; } // must be able to open read-only WTextFile soundFile(fullPathName, "rt"); if (!soundFile.IsOpen()) { return false; } // scan each line, ignore lines with words<2 std::string soundLine; while (soundFile.ReadLine(&soundLine)) { if (abortable && bkbhit()) { break; } int nw = wordcount(soundLine.c_str(), DELIMS_WHITE); if (nw >= 2) { int freq = atoi(extractword(1, soundLine, DELIMS_WHITE)); int dur = atoi(extractword(2, soundLine, DELIMS_WHITE)); // only play if freq and duration > 0 if (freq > 0 && dur > 0) { int nPauseDelay = 0; if (nw > 2) { nPauseDelay = atoi(extractword(3, soundLine, DELIMS_WHITE)); } WWIV_Sound(freq, dur); if (nPauseDelay > 0) { WWIV_Delay(nPauseDelay); } } } } soundFile.Close(); return true; }
MOboolean moSound::SupportedFile(moText p_filename) { moFile soundFile(p_filename); if ( soundFile.GetExtension()==".mp3" || soundFile.GetExtension()==".wav" || soundFile.GetExtension()==".m4a" || soundFile.GetExtension()==".ogg" ) { return true; } return true; }
void Module::AddSound(std::string filename, size_t soundId) { // Get file location std::string fileLocation(filename); // Open file std::ifstream soundFile(fileLocation); // Check file validity if(!soundFile.good()) throw - 1; soundFile.seekg (0, std::ios::end); // Get file size in bytes int fileSize = soundFile.tellg(); short* soundData = new short[fileSize]; soundFile.seekg(0, std::ios::beg); int i = 0; while(soundFile.read((char*)&soundData[i], sizeof(short))) i++; SoundParams sndParameters; sndParameters.length = fileSize*sizeof(char)/sizeof(short); sndParameters.data = soundData; sndParameters.id = soundId; // Add sound to lib sndParams.push_back(sndParameters); // Close file soundFile.close(); return; }
/* * Load wave file function. No need for ALUT with this */ bool Wave::LoadToOpenALBuffer(const char* filename, ALuint* buffer, ALsizei* size, ALsizei* frequency, ALenum* format) { //Local Declarations WAVE_Format wave_format; RIFF_Header riff_header; WAVE_Data wave_data; unsigned char* data; // Need to implement //FILE* soundFile = fopen(filename, "rb"); DDFile soundFile(filename); if (!DDFile::FileExists(filename)) { dsprintf("ERROR: Failed to load. Couldn't find file [%s].\n", filename); return false; } soundFile.LoadFileIntoBuffer(); void* readPtr = (void*)soundFile.Buffer(); // Read in the first chunk into the struct //size_t readCount = fread(&riff_header, sizeof(RIFF_Header), 1, soundFile); // dsprintf("ReadPtr1 %p.\n", readPtr); soundFile.Read(&riff_header, sizeof(RIFF_Header), readPtr); // dsprintf("ReadPtr2 %p.\n", readPtr); //check for RIFF and WAVE tag in memeory if ((riff_header.chunkID[0] != 'R' || riff_header.chunkID[1] != 'I' || riff_header.chunkID[2] != 'F' || riff_header.chunkID[3] != 'F') && (riff_header.format[0] != 'W' || riff_header.format[1] != 'A' || riff_header.format[2] != 'V' || riff_header.format[3] != 'E')) { dsprintf("ERROR: Failed to load. Wave header tag missing.\n"); return false; } //Read in the 2nd chunk for the wave info //readCount = fread(&wave_format, sizeof(WAVE_Format), 1, soundFile); soundFile.Read(&wave_format, sizeof(WAVE_Format), readPtr); //check for fmt tag in memory if (wave_format.subChunkID[0] != 'f' || wave_format.subChunkID[1] != 'm' || wave_format.subChunkID[2] != 't' || wave_format.subChunkID[3] != ' ') { dsprintf("ERROR: Failed to load. Wave fmt tag missing.\n"); return false; } //check for extra parameters; if (wave_format.subChunkSize > 16) { dsprintf("Warning: Wave.cpp untested code running\n"); //fseek(soundFile, sizeof(short), SEEK_CUR); soundFile.Seek((int)sizeof(short), readPtr); } //Read in the the last byte of data before the sound file //fread(&wave_data, sizeof(WAVE_Data), 1, soundFile); soundFile.Read(&wave_data, sizeof(WAVE_Data), readPtr); //check for data tag in memory if (wave_data.subChunkID[0] != 'd' || wave_data.subChunkID[1] != 'a' || wave_data.subChunkID[2] != 't' || wave_data.subChunkID[3] != 'a') { dsprintf("ERROR: Failed to load. Data tag missing.\n"); return false; } //Allocate memory for data data = new unsigned char[wave_data.subChunk2Size]; // Read in the sound data into the soundData variable if (!soundFile.Read(data, wave_data.subChunk2Size, /*1,*/ readPtr)) { dsprintf("ERROR: Failed to read header into struct.\n"); delete [] data; data = NULL; return false; } //Now we set the variables that we passed in with the //data from the structs *size = wave_data.subChunk2Size; *frequency = wave_format.sampleRate; //The format is worked out by looking at the number of //channels and the bits per sample. if(wave_format.numChannels == 1) { if(wave_format.bitsPerSample == 8) { *format = AL_FORMAT_MONO8; } else if(wave_format.bitsPerSample == 16) { *format = AL_FORMAT_MONO16; } } else if(wave_format.numChannels == 2) { if(wave_format.bitsPerSample == 8 ) { *format = AL_FORMAT_STEREO8; } else if(wave_format.bitsPerSample == 16) { *format = AL_FORMAT_STEREO16; } } //create our openAL buffer and check for success alGenBuffers(1, buffer); //errorCheck(); //now we put our data into the openAL buffer and //check for success alBufferData(*buffer, *format, (void*) data, *size, *frequency); delete [] data; // errorCheck(); //clean up and return true if successful //fclose(soundFile); return true; }
std::string DataJockey::computeDescriptors(std::string inFileName, unsigned int windowSize){ float *window = new float[windowSize]; float *monoBuf = new float[windowSize]; float *monoBufWindowed = new float[windowSize]; float *doubleMonoBuf = new float[windowSize * 2]; float inbuf[PCM_READ_SIZE]; unsigned int frames_read; unsigned int channels; std::vector<float> audioData; std::map<std::string, std::vector<float> * > descriptors; std::vector<float> centroidList; std::vector<float> irregularityJList; std::vector<float> irregularityKList; //std::vector<float> spectralMeanList; //std::vector<float> spectralVarianceList; //std::vector<float> f0List; std::vector<float> spectralSlopeList; std::vector<float> spectralSpreadList; std::vector<float> harmonicityList; SoundFile soundFile(inFileName.c_str()); //check to make sure soundfile exists if(!soundFile){ std::string str("cannot open input soundfile: "); str.append(inFileName); throw std::runtime_error(str); } channels = soundFile.channels(); //read the sound file into memory and make it mono //read in the audio data and write to the output file while((frames_read = soundFile.readf(inbuf, PCM_READ_SIZE / channels)) != 0){ for(unsigned int i = 0; i < frames_read; i++){ float sum = 0; for(unsigned int j = 0; j < channels; j++) sum += inbuf[i * channels + j]; audioData.push_back(sum / channels); } } //generate the window [triangle] window[windowSize / 2] = 1.0; for(unsigned int i = 0; i < windowSize / 2; i++) window[windowSize - i - 1] = window[i] = ((float) i) * 2 / windowSize; //xtract_init_fft(windowSize, XTRACT_SPECTRUM); for(unsigned int i = 0; ((i + 1) * windowSize) < audioData.size(); i += 1){ float argv[3]; //window the b.s. and copy it to the fftInBuf for(unsigned int j = 0; j < windowSize; j++){ //mix the 2 channels [we just care about mono] //doubleMonoBuf[j + (i % 2) * windowSize] = //monoBuf[j] = (audioInBuf[j * 2] + audioInBuf[j * 2 + 1]) / 2.0; doubleMonoBuf[j + (i % 2) * windowSize] = monoBuf[j] = audioData[j + i * windowSize]; monoBufWindowed[j] = window[j] * monoBuf[j]; } //compute spectrum float spectrum[windowSize]; argv[0] = soundFile.samplerate() / (float)windowSize; argv[1] = XTRACT_MAGNITUDE_SPECTRUM; argv[2] = 0; xtract[XTRACT_SPECTRUM](monoBufWindowed, windowSize, argv, spectrum); // float peakSpectrum[windowSize + 1]; // //compute peak spectrum // argv[0] = soundFile->samplerate() / ((float)windowSize / 2); // //XXX what should this value be? // argv[1] = 0.1; // xtract[XTRACT_PEAK_SPECTRUM](spectrum, windowSize / 2, argv, peakSpectrum); //compute spectral centroid float centroid; xtract[XTRACT_SPECTRAL_CENTROID](spectrum, windowSize, NULL, ¢roid); if(isnan(centroid) || isinf(centroid)){ //cout << "centroid (nan/inf): " << float(i) / soundFile->samplerate() << std::endl; centroid = 0; } centroidList.push_back(centroid); //vector_extensions::insert_sorted(centroidList, centroid); ////compute spectralMean //float spectralMean; //xtract[XTRACT_SPECTRAL_MEAN](spectrum, windowSize, NULL, &spectralMean); //if(isnan(spectralMean) || isinf(spectralMean)){ ////cout << "spectralMean (nan/inf): " << float(i) / soundFile->samplerate() << std::endl; //spectralMean = 0; //} //vector_extensions::insert_sorted(spectralMeanList, spectralMean); ////compute spectralVariance //float spectralVariance; //xtract[XTRACT_SPECTRAL_VARIANCE](spectrum, windowSize, &spectralMean, &spectralVariance); //if(isnan(spectralVariance) || isinf(spectralVariance)){ ////cout << "spectralVariance (nan/inf): " << float(i) / soundFile->samplerate() << std::endl; //spectralVariance = 0; //} //vector_extensions::insert_sorted(spectralVarianceList, spectralVariance); //compute irregularityJ float irregularityJ; xtract[XTRACT_IRREGULARITY_J](spectrum, windowSize/ 2, NULL, &irregularityJ); if(isnan(irregularityJ) || isinf(irregularityJ)) irregularityJ = 0; irregularityJList.push_back(irregularityJ); //vector_extensions::insert_sorted(irregularityJList, irregularityJ); //compute irregularityK float irregularityK; xtract[XTRACT_IRREGULARITY_K](spectrum, windowSize/ 2, NULL, &irregularityK); if(isnan(irregularityK) || isinf(irregularityK)) irregularityK = 0; //vector_extensions::insert_sorted(irregularityKList, irregularityK); irregularityKList.push_back(irregularityK); /* //compute f0 float f0; argv[0] = soundFile->samplerate(); xtract[XTRACT_FAILSAFE_F0](monoBuf, windowSize, argv, &f0); vector_extensions::insert_sorted(f0List, f0); */ //compute spectralSlope float spectralSlope; xtract[XTRACT_SPECTRAL_SLOPE](spectrum, windowSize, NULL, &spectralSlope); if(isnan(spectralSlope) || isinf(spectralSlope)) spectralSlope = 0; //vector_extensions::insert_sorted(spectralSlopeList, spectralSlope); spectralSlopeList.push_back(spectralSlope); //compute spectralSpread float spectralSpread; xtract[XTRACT_SPREAD](spectrum, windowSize/2, ¢roid, &spectralSpread); if(isnan(spectralSpread) || isinf(spectralSpread)) spectralSpread = 0; //vector_extensions::insert_sorted(spectralSpreadList, spectralSpread); spectralSpreadList.push_back(spectralSpread); if((i % (2 * (1 + SKIP_HARM_WINDOWS))) == 1){ //compute the harmonicity //file:///home/alex/doc/harmonicity-email-easy.html //RB(k) = SUM (x(n) * x(n+k)) //H11 = MAX (RB(k)) / RB(0) //where the maximum is taken over k in the range [1, MAX], where MAX should //be larger than any likely period of the harmonic component. double sum; double max_sum = -1000000000000000.0f; for(unsigned int k = 1; k < windowSize; k++){ sum = 0; for(unsigned int n = 0; n < windowSize; n++){ sum += doubleMonoBuf[n] * doubleMonoBuf[n + k]; } if(sum > max_sum) max_sum = sum; } sum = 0; for(unsigned int n = 0; n < windowSize; n++){ sum += pow(doubleMonoBuf[n], 2); } if (sum > 0){ if (max_sum < sum){ harmonicityList.push_back((float)(max_sum / sum)); //vector_extensions::insert_sorted(harmonicityList, (float)(max_sum / sum)); } } } } ////set the precision //cout.setf(0,ios::floatfield); std::ostringstream outStringStream; outStringStream.width(std::numeric_limits< float >::digits10); //output descriptor name\twindow size\thop size //values0 value1 //average median //output the values and compute the average and median outStringStream << "spectral centroid\t" << windowSize << "\t" << windowSize << std::endl; double sum = 0; for(std::vector<float>::iterator it = centroidList.begin(); it != centroidList.end(); it++){ outStringStream << *it << "\t"; sum += *it; } outStringStream << std::endl; outStringStream << sum / centroidList.size() << "\t" << median(centroidList) << std::endl; sum = 0; outStringStream << "harmonicity\t" << windowSize << "\t" << windowSize * (1 + SKIP_HARM_WINDOWS) << std::endl; for(std::vector<float>::iterator it = harmonicityList.begin(); it != harmonicityList.end(); it++){ outStringStream << *it << "\t"; sum += *it; } outStringStream << std::endl; outStringStream << sum / harmonicityList.size() << "\t" << median(harmonicityList) << std::endl; sum = 0; outStringStream << "spectral irregularityJ\t" << windowSize << "\t" << windowSize << std::endl; for(std::vector<float>::iterator it = irregularityJList.begin(); it != irregularityJList.end(); it++){ outStringStream << *it << "\t"; sum += *it; } outStringStream << std::endl; outStringStream << sum / irregularityJList.size() << "\t" << median(irregularityJList) << std::endl; sum = 0; outStringStream << "spectral irregularityK\t" << windowSize << "\t" << windowSize << std::endl; for(std::vector<float>::iterator it = irregularityKList.begin(); it != irregularityKList.end(); it++){ outStringStream << *it << "\t"; sum += *it; } outStringStream << std::endl; outStringStream << sum / irregularityKList.size() << "\t" << median(irregularityKList) << std::endl; sum = 0; outStringStream << "spectral slope\t" << windowSize << "\t" << windowSize << std::endl; for(std::vector<float>::iterator it = spectralSlopeList.begin(); it != spectralSlopeList.end(); it++){ outStringStream << *it << "\t"; sum += *it; } outStringStream << std::endl; outStringStream << sum / spectralSlopeList.size() << "\t" << median(spectralSlopeList) << std::endl; sum = 0; outStringStream << "spectral spread\t" << windowSize << "\t" << windowSize << std::endl; for(std::vector<float>::iterator it = spectralSpreadList.begin(); it != spectralSpreadList.end(); it++){ outStringStream << *it << "\t"; sum += *it; } outStringStream << std::endl; outStringStream << sum / spectralSpreadList.size() << "\t" << median(spectralSpreadList) << std::endl; return outStringStream.str(); }