MonoOfflineNetworkPlayer.cxx

Go to the documentation of this file.
00001 #include "MonoOfflineNetworkPlayer.hxx"
00002 #include "MonoAudioFileReader.hxx"
00003 #include "MonoAudioFileWriter.hxx"
00004 #include <fstream>
00005 
00006 namespace CLAM
00007 {
00008 
00009 bool MonoOfflineNetworkPlayer::IsWorking()
00010 {
00011         CacheSourcesAndSinks();
00012         return _filenames.size()!=GetNSinks()+GetNSources();
00013 }
00014 
00015 std::string MonoOfflineNetworkPlayer::NonWorkingReason()
00016 {
00017         std::stringstream ss;
00018         ss << GetNSources()    << " inputs and "
00019                 << GetNSinks() << " outputs needed but just "
00020                 << _filenames.size() << " files provided" << std::ends;
00021         return ss.str();
00022 }
00023 
00024 void MonoOfflineNetworkPlayer::Start()
00025 {
00026         if ( IsPlaying() )
00027                 return;
00028         BePlaying();
00029 
00030         CacheSourcesAndSinks();
00031 
00032         const int frameSize = 512;
00033         const int sampleRate = 48000; //TODO deduce from inputs.
00034 
00035         std::vector<Audio> audioBuffers(_filenames.size());
00036         std::vector<MonoAudioFileReader*> readers;
00037         MonoAudioFileReaderConfig readercfg;
00038         unsigned fileIndex=0;
00039         for (unsigned i=0; i<GetNSources(); i++)
00040         {
00041                 CLAM_ASSERT(fileIndex<_filenames.size(),
00042                         "Not all the network inputs could be fullfiled. Have you checked the IsWorking() method?");
00043                 {
00044                         std::ifstream checkfile(_filenames[fileIndex].c_str());
00045                         CLAM_ASSERT(checkfile.is_open(), std::string(std::string("Could not open one of the input files: ")+_filenames[fileIndex]).c_str());
00046                 }
00047                 readercfg.SetSourceFile(_filenames[fileIndex]);
00048                 readercfg.SetLoop(_enableLoopInputWavs);
00049                 MonoAudioFileReader * fileReader = new MonoAudioFileReader(readercfg);
00050                 fileReader->GetOutPort("Samples Read").SetSize( frameSize );
00051                 fileReader->GetOutPort("Samples Read").SetHop( frameSize );
00052                 readers.push_back(fileReader);
00053                 audioBuffers[fileIndex].SetSize( frameSize );
00054 
00055                 TData * buffer = &audioBuffers[fileIndex].GetBuffer()[0];
00056                 SetSourceBuffer(i, buffer, frameSize);
00057                 std::cout << " In " << SourceName(i) << ":" << _filenames[fileIndex] << std::endl;
00058                 fileIndex++;
00059         }
00060 
00061         std::vector<MonoAudioFileWriter*> writers;
00062         MonoAudioFileWriterConfig writercfg;
00063         for (unsigned i=0; i<GetNSinks(); i++)
00064         {
00065                 if (fileIndex>=_filenames.size())
00066                 {
00067                         std::cerr <<
00068                         "Not all the network outputs could be fullfiled.";
00069                         break;
00070                 }
00071 
00072                 writercfg.SetTargetFile(_filenames[fileIndex]);
00073                 writercfg.SetSampleRate(sampleRate);
00074                 MonoAudioFileWriter * fileWriter = new MonoAudioFileWriter(writercfg);
00075                 writers.push_back(fileWriter);
00076                 audioBuffers[fileIndex].SetSize( frameSize );
00077 
00078                 TData * buffer = &audioBuffers[fileIndex].GetBuffer()[0];
00079                 SetSinkBuffer(i, buffer, frameSize);
00080                 std::cout << " Out: " << SinkName(i) << ":" << _filenames[fileIndex] << std::endl;
00081                 fileIndex++;
00082         }
00083 
00084         for (unsigned i = 0; i<readers.size(); i++)
00085                 readers[i]->Start();
00086         for (unsigned i = 0; i<writers.size(); i++)
00087                 writers[i]->Start();
00088 
00089         long iterationIndex = 0;
00090         bool timeLimitedMode = _resultWavsTime > 0.001;
00091         while ( true )
00092         {
00093                 std::cout << "." << std::flush;
00094                 unsigned audioIndex =0;
00095                 bool someInputFinished=false;
00096                 for (unsigned i = 0; i<readers.size(); i++)
00097                         someInputFinished |= !readers[i]->Do(audioBuffers[audioIndex++]);
00098                 if (someInputFinished and not _enableLoopInputWavs ) break;
00099                 GetNetwork().Do();
00100                 for (unsigned i = 0; i<writers.size(); i++)
00101                         writers[i]->Do(audioBuffers[audioIndex++]);
00102                 if (timeLimitedMode and float(iterationIndex*frameSize)/sampleRate > _resultWavsTime)
00103                 {
00104                         std::cout << "REACHED MAX TIME - finalizing"<< std::endl;
00105                         break;
00106                 }
00107                 iterationIndex++;
00108         }
00109 
00110         for (unsigned i = 0; i<readers.size(); i++)
00111                 readers[i]->Stop();
00112         for (unsigned i = 0; i<writers.size(); i++)
00113                 writers[i]->Stop();
00114 }
00115 
00116 void MonoOfflineNetworkPlayer::Stop()
00117 {
00118         if ( IsStopped() ) return;
00119         BeStopped();
00120         //TODO close files
00121 }
00122 
00123 void MonoOfflineNetworkPlayer::ProcessInputFile()
00124 {
00125         GetNetwork().Do();
00126 }
00127 
00128 void MonoOfflineNetworkPlayer::AddInputFile( const std::string& filename )
00129 {
00130         _filenames.push_back(filename);
00131 }
00132 
00133 void MonoOfflineNetworkPlayer::AddOutputFile( const std::string& filename )
00134 {
00135         _filenames.push_back(filename);
00136 }
00137 
00138 } //namespace
00139 
Generated by  doxygen 1.6.3