1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /***************************************************************************
3  *            drumkitloader.h
4  *
5  *  Thu Jan 17 20:54:13 CET 2013
6  *  Copyright 2013 Bent Bisballe Nyeng
7  *  deva@aasimon.org
8  ****************************************************************************/
9 
10 /*
11  *  This file is part of DrumGizmo.
12  *
13  *  DrumGizmo is free software; you can redistribute it and/or modify
14  *  it under the terms of the GNU Lesser General Public License as published by
15  *  the Free Software Foundation; either version 3 of the License, or
16  *  (at your option) any later version.
17  *
18  *  DrumGizmo is distributed in the hope that it will be useful,
19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *  GNU Lesser General Public License for more details.
22  *
23  *  You should have received a copy of the GNU Lesser General Public License
24  *  along with DrumGizmo; if not, write to the Free Software
25  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
26  */
27 #pragma once
28 
29 #include <string>
30 #include <list>
31 #include <mutex>
32 #include <vector>
33 
34 #include "thread.h"
35 #include "sem.h"
36 
37 #include "drumkit.h"
38 #include "settings.h"
39 #include "audioinputengine.h"
40 #include "audiocache.h"
41 
42 struct DrumkitDOM;
43 struct InstrumentDOM;
44 
45 //! This class is responsible for loading the drumkits in its own thread.
46 //! All interaction calls are simply modifying queues and not doing any
47 //! work in-sync with the caller.
48 //! This means that if loadKit(...) is called, one cannot assume that the
49 //! drumkit has actually been loaded when the call returns.
50 class DrumKitLoader
51 	: public Thread
52 {
53 public:
54 	DrumKitLoader(Settings& settings, DrumKit& kit, AudioInputEngine& ie,
55 	              Random& rand, AudioCache& audio_cache);
56 
57 	~DrumKitLoader();
58 
59 	//! Starts the loader thread.
60 	void init();
61 
62 	//! Signal the loader thread to stop and waits for the threads to merge
63 	//! before returning.
64 	void deinit();
65 
66 	bool loadkit(const std::string& file);
67 
68 	//! Signal the loader to start loading all audio files contained in the kit.
69 	//! All other AudioFiles in queue will be removed before the new ones are
70 	//! scheduled.
71 	void loadKitAudio(const DrumKit& kit);
72 
73 	//! Skip all queued AudioFiles.
74 	void skip();
75 
76 	//! Set the framesize which will be used to preloading samples in next
77 	//! loadKit call.
78 	void setFrameSize(std::size_t framesize);
79 
80 protected:
81 	void thread_main();
82 
83 	Semaphore run_semaphore;
84 	Semaphore semaphore;
85 	Semaphore framesize_semaphore;
86 	std::mutex mutex;
87 	volatile bool running{false};
88 	std::list<AudioFile*> load_queue;
89 	std::size_t framesize{0};
90 	Settings& settings;
91 	SettingsGetter getter;
92 	DrumKit& kit;
93 	AudioInputEngine& ie;
94 	//MemChecker memchecker;
95 	Random& rand;
96 	AudioCache& audio_cache;
97 	std::size_t preload_samples{std::numeric_limits<std::size_t>::max()};
98 
99 	LogFunction logger;
100 };
101