StreamingSounds(int argc, const char * argv[]) : UnitTest(argc, argv) { vector<string> files; string dirIn = stringOption(options, "dirIn"); string dirOut = stringOption(options, "dirOut"); string extension = stringOption(options, "ext"); string fileIn = ""; string fileOut = ""; findFiles(dirIn, files, extension); for (vector<string>::iterator i = files.begin(); i != files.end(); ++i) { fileIn = *i; fileOut = fileIn.substr(0, fileIn.find_first_of('.')) + ".swf"; cout << fileIn << endl; #ifdef WIN32 fileOut = dirOut + "\\" + fileOut; #else fileOut = dirOut + "/" + fileOut; #endif try { FSMovie movie; FSSoundConstructor* soundGenerator = SoundConstructor(); if (soundGenerator->setSoundFromFile(fileIn.c_str()) != TransformUtil::OK) { throw FSException("Could not load sound file"); } float framesPerSecond = 12.0f; movie.setFrameSize(FSBounds(0, 0, 8000, 4000)); movie.setFrameRate(framesPerSecond); movie.add(new FSSetBackgroundColor(FSColorTable::lightblue())); /* * Calculate the time it takes to play the sound and the number of frames this * represents. */ float duration = ((float) soundGenerator->getSamplesPerChannel()) / ((float) soundGenerator->getSampleRate()); int numberOfFrames = (int) (duration * framesPerSecond); /* * Play the Streaming Sound. * * Calculate the number of decoded sound samples played for each frame and * the size, in bytes, of each block compressed sound data. */ int samplesPerBlock = soundGenerator->getSampleRate() / (int) framesPerSecond; int numberOfBlocks = soundGenerator->getSamplesPerChannel() / samplesPerBlock; /* * An FSSoundStreamHeader2 object defines the attributes of the streaming sound. */ movie.add(soundGenerator->streamHeader(samplesPerBlock)); /* * Add a streaming block for each frame so the sound is played as each frame * is displayed. */ for (int i=0; i<numberOfBlocks; i++) { movie.add(soundGenerator->streamBlock(i, samplesPerBlock)); movie.add(new FSShowFrame()); } movie.encodeToFile(fileOut.c_str()); delete soundGenerator; } catch (FSException e) { cerr << e.what(); } } }
StreamingSounds(map< string, string>& options) { string soundFile = stringOption(options, "file"); FSSoundConstructor* soundGenerator = SoundConstructor(); FSMovie movie; int status = TransformUtil::OK; if ((status = soundGenerator->setSoundFromFile(soundFile.c_str())) != TransformUtil::OK) { switch (status) { case TransformUtil::FileNotFound: cout << "Could not find sound file" << endl; break; case TransformUtil::ReadError: cout << "Could not read sound file" << endl; break; case TransformUtil::FormatError: cout << "Could not read sound file" << endl; break; } } if (status == TransformUtil::OK) { float framesPerSecond = 12.0f; /* * Calculate the time it takes to play the sound and the number of frames * this represents. */ float duration = ((float) soundGenerator->getSamplesPerChannel()) / ((float) soundGenerator->getSampleRate()); int numberOfFrames = (int) (duration * framesPerSecond); /* * Calculate the number of decoded sound samples played for each frame and * the size, in bytes, of each block compressed sound data. */ int samplesPerBlock = soundGenerator->getSampleRate() / (int) framesPerSecond; int numberOfBlocks = soundGenerator->getSamplesPerChannel() / samplesPerBlock; /* * Add all the objects together to create the movie. */ movie.setFrameSize(FSBounds(0, 0, 8000, 4000)); movie.setFrameRate(framesPerSecond); movie.add(new FSSetBackgroundColor(FSColorTable::lightblue())); /* * An FSSoundStreamHeader2 object defines the attributes of the streaming sound. */ movie.add(soundGenerator->streamHeader(samplesPerBlock)); /* * Add a streaming block for each frame so the sound is played as each frame * is displayed. */ for (int i=0; i<numberOfBlocks; i++) { movie.add(soundGenerator->streamBlock(i, samplesPerBlock)); movie.add(new FSShowFrame()); } saveMovie(movie, stringOption(options, "resultDir"), "StreamingSounds.swf"); } delete soundGenerator; }