+PBD::Signal1<void,boost::shared_ptr<Source> > SourceFactory::SourceCreated;
+Glib::Threads::Cond SourceFactory::PeaksToBuild;
+Glib::Threads::Mutex SourceFactory::peak_building_lock;
+std::list<boost::weak_ptr<AudioSource> > SourceFactory::files_with_peaks;
+
+static int active_threads = 0;
+
+static void
+peak_thread_work ()
+{
+ SessionEvent::create_per_thread_pool (X_("PeakFile Builder "), 64);
+
+ while (true) {
+
+ SourceFactory::peak_building_lock.lock ();
+
+ wait:
+ if (SourceFactory::files_with_peaks.empty()) {
+ SourceFactory::PeaksToBuild.wait (SourceFactory::peak_building_lock);
+ }
+
+ if (SourceFactory::files_with_peaks.empty()) {
+ goto wait;
+ }
+
+ boost::shared_ptr<AudioSource> as (SourceFactory::files_with_peaks.front().lock());
+ SourceFactory::files_with_peaks.pop_front ();
+ ++active_threads;
+ SourceFactory::peak_building_lock.unlock ();
+
+ if (!as) {
+ continue;
+ }
+
+ as->setup_peakfile ();
+ SourceFactory::peak_building_lock.lock ();
+ --active_threads;
+ SourceFactory::peak_building_lock.unlock ();
+ }
+}
+
+int
+SourceFactory::peak_work_queue_length ()
+{
+ // ideally we'd loop over the queue and check for duplicates
+ // and existing valid peak-files..
+ return SourceFactory::files_with_peaks.size () + active_threads;
+}
+
+void
+SourceFactory::init ()
+{
+ for (int n = 0; n < 2; ++n) {
+ Glib::Threads::Thread::create (sigc::ptr_fun (::peak_thread_work));
+ }
+}
+
+int
+SourceFactory::setup_peakfile (boost::shared_ptr<Source> s, bool async)
+{
+ boost::shared_ptr<AudioSource> as (boost::dynamic_pointer_cast<AudioSource> (s));
+
+ if (as) {
+
+ // immediately set 'peakfile-path' for empty and NoPeakFile sources
+ if (async && !as->empty() && !(as->flags() & Source::NoPeakFile)) {
+
+ Glib::Threads::Mutex::Lock lm (peak_building_lock);
+ files_with_peaks.push_back (boost::weak_ptr<AudioSource> (as));
+ PeaksToBuild.broadcast ();
+
+ } else {
+
+ if (as->setup_peakfile ()) {
+ error << string_compose("SourceFactory: could not set up peakfile for %1", as->name()) << endmsg;
+ return -1;
+ }
+ }
+ }
+
+ return 0;
+}
+
+boost::shared_ptr<Source>
+SourceFactory::createSilent (Session& s, const XMLNode& node, samplecnt_t nframes, float sr)
+{
+ Source* src = new SilentFileSource (s, node, nframes, sr);
+#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
+ // boost_debug_shared_ptr_mark_interesting (src, "Source");
+#endif
+ boost::shared_ptr<Source> ret (src);
+ // no analysis data - the file is non-existent
+ SourceCreated (ret);
+ return ret;
+}