aRts audio server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

datahandle_impl.cc 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. Copyright (C) 2002 Hans Meine
  3. hans_meine@gmx.net
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. #include <gsl/gsldatahandle.h>
  18. #include <gsl/gslloader.h>
  19. #include <gsl/gslwaveosc.h>
  20. #include "artsflow.h"
  21. #include "stdsynthmodule.h"
  22. #include "debug.h"
  23. #include <algorithm>
  24. #include <cstring>
  25. #include <gslpp/datahandle.h>
  26. using namespace std;
  27. namespace Arts {
  28. static GSL::DataHandle getDHandle(DataHandle handle);
  29. class DataHandle_impl : virtual public DataHandle_skel
  30. {
  31. protected:
  32. GSL::DataHandle dhandle_;
  33. long errno_;
  34. friend GSL::DataHandle getDHandle(DataHandle handle);
  35. public:
  36. DataHandle_impl(GSL::DataHandle dhandle = GSL::DataHandle::null())
  37. : dhandle_(dhandle)
  38. {
  39. errno_ = dhandle_.isNull() ? 0 : dhandle_.open();
  40. }
  41. ~DataHandle_impl()
  42. {
  43. if(dhandle_.isOpen())
  44. dhandle_.close();
  45. }
  46. long valueCount()
  47. {
  48. return dhandle_.valueCount();
  49. }
  50. long bitDepth()
  51. {
  52. return dhandle_.bitDepth();
  53. }
  54. long channelCount()
  55. {
  56. return dhandle_.channelCount();
  57. }
  58. long errorNo()
  59. {
  60. return errno_;
  61. }
  62. GSL::DataHandle createCropped(long headCutValueCount,
  63. long tailCutValueCount)
  64. {
  65. return dhandle_.createCropped(headCutValueCount, tailCutValueCount);
  66. }
  67. GSL::DataHandle createCut(long cutOffset,
  68. long cutValueCount)
  69. {
  70. return dhandle_.createCut(cutOffset, cutValueCount);
  71. }
  72. GSL::DataHandle createReversed()
  73. {
  74. return dhandle_.createReversed();
  75. }
  76. };
  77. class ReversedDataHandle_impl : virtual public DataHandle_impl,
  78. virtual public ReversedDataHandle_skel
  79. {
  80. void init(DataHandle sourceHandle)
  81. {
  82. DataHandle_impl *impl = dynamic_cast<DataHandle_impl *>(sourceHandle._base());
  83. dhandle_ = impl->createReversed();
  84. }
  85. };
  86. class CroppedDataHandle_impl : virtual public DataHandle_impl,
  87. virtual public CroppedDataHandle_skel
  88. {
  89. void init(DataHandle sourceHandle,
  90. long headCutValueCount,
  91. long tailCutValueCount)
  92. {
  93. DataHandle_impl *impl = dynamic_cast<DataHandle_impl *>(sourceHandle._base());
  94. dhandle_ = impl->createCropped(headCutValueCount, tailCutValueCount);
  95. }
  96. };
  97. class CutDataHandle_impl : virtual public DataHandle_impl,
  98. virtual public CutDataHandle_skel
  99. {
  100. void init(DataHandle sourceHandle,
  101. long cutOffset,
  102. long cutValueCount)
  103. {
  104. DataHandle_impl *impl = dynamic_cast<DataHandle_impl *>(sourceHandle._base());
  105. dhandle_ = impl->createCut(cutOffset, cutValueCount);
  106. }
  107. };
  108. static GSL::DataHandle getDHandle(DataHandle handle)
  109. {
  110. DataHandle_impl *impl = dynamic_cast<DataHandle_impl *>(handle._base());
  111. if(impl)
  112. return impl->dhandle_;
  113. return GSL::DataHandle::null();
  114. }
  115. GslWaveChunk* const_wchunk_from_freq(gpointer wchunk_data, gfloat)
  116. {
  117. return (GslWaveChunk*)wchunk_data;
  118. }
  119. // FIXME: Think about whether this is needed as a parameter like mixerFrequency
  120. #define OSCILLATOR_FREQUENCY 440.0f
  121. class DataHandlePlay_impl : virtual public DataHandlePlay_skel,
  122. virtual public StdSynthModule
  123. {
  124. protected:
  125. DataHandle dhandle_;
  126. GSL::DataHandle gslDHandle_;
  127. int errno_;
  128. GslWaveChunk *wchunk_;
  129. GslErrorType wchunkError_;
  130. GslWaveOscData *wosc_;
  131. float mixerFrequency_;
  132. unsigned short channelIndex_;
  133. float speed_;
  134. unsigned long pos_;
  135. bool finished_;
  136. bool paused_;
  137. public:
  138. DataHandlePlay_impl()
  139. : dhandle_(DataHandle::null()),
  140. gslDHandle_(GSL::DataHandle::null()),
  141. wchunk_(NULL),
  142. wosc_(NULL),
  143. mixerFrequency_(0), // FIXME: doc says soundserver's freq
  144. channelIndex_(0),
  145. speed_(1.0f),
  146. pos_(0),
  147. finished_(false),
  148. paused_(false)
  149. {}
  150. ~DataHandlePlay_impl()
  151. {
  152. handle(DataHandle::null());
  153. }
  154. DataHandle handle()
  155. {
  156. return dhandle_;
  157. }
  158. void handle(DataHandle handle)
  159. {
  160. deleteWaveChunk();
  161. if(!gslDHandle_.isNull() && !errno_)
  162. gslDHandle_.close();
  163. dhandle_ = handle;
  164. if(handle.isNull())
  165. gslDHandle_ = GSL::DataHandle::null();
  166. else
  167. {
  168. gslDHandle_ = getDHandle(dhandle_);
  169. if(gslDHandle_.isNull())
  170. {
  171. arts_debug("ERROR: could not get internal GSL::DataHandle!");
  172. finished(true); // FIXME: can this happen?
  173. }
  174. else
  175. {
  176. errno_ = gslDHandle_.open();
  177. if(errno_)
  178. arts_debug("DataHandlePlay got error from GSL::DataHandle.open(): '%s'",
  179. strerror(errno_));
  180. }
  181. }
  182. }
  183. void streamInit()
  184. {
  185. if(!gslDHandle_.isNull() && !wosc_)
  186. {
  187. if(!wchunk_)
  188. createWaveChunk();
  189. configureWaveOsc();
  190. }
  191. }
  192. float mixerFrequency() { return wosc_ ? wosc_->mix_freq : 0; }
  193. void mixerFrequency(float f)
  194. {
  195. if(wchunk_)
  196. arts_warning("DataHandlePlay: cannot change mixerFrequency after start of sound processing!");
  197. if(mixerFrequency() != f)
  198. {
  199. mixerFrequency_ = f;
  200. mixerFrequency_changed(f);
  201. }
  202. }
  203. long channelIndex() { return channelIndex_; }
  204. void channelIndex(long ci)
  205. {
  206. if(channelIndex_ != ci)
  207. {
  208. channelIndex_ = ci;
  209. if(wosc_)
  210. {
  211. GslWaveOscConfig config = wosc_->config;
  212. config.channel = ci;
  213. gsl_wave_osc_config(wosc_, &config);
  214. }
  215. channelIndex_changed(ci);
  216. }
  217. }
  218. float speed() { return speed_; }
  219. void speed(float s)
  220. {
  221. if(speed_ != s)
  222. {
  223. speed_ = s;
  224. if(wosc_)
  225. {
  226. GslWaveOscConfig config = wosc_->config;
  227. config.cfreq = OSCILLATOR_FREQUENCY * speed();
  228. gsl_wave_osc_config(wosc_, &config);
  229. }
  230. speed_changed(s);
  231. }
  232. }
  233. long pos() { return wosc_ ? wosc_->block.offset : 0; }
  234. void pos(long p)
  235. {
  236. if(pos() != p)
  237. {
  238. GslWaveOscConfig config = wosc_->config;
  239. config.start_offset = p;
  240. gsl_wave_osc_config(wosc_, &config);
  241. pos_changed(p);
  242. }
  243. }
  244. bool finished() { return finished_; }
  245. void finished(bool f)
  246. {
  247. if(finished_ != f)
  248. {
  249. finished_ = f;
  250. finished_changed(f);
  251. }
  252. }
  253. bool paused() { return paused_; }
  254. void paused(bool p)
  255. {
  256. paused_ = p;
  257. }
  258. void deleteWaveChunk()
  259. {
  260. deleteWaveOsc();
  261. if(wchunk_)
  262. {
  263. arts_debug("DataHandlePlay_impl: close()ing gsl_wave_chunk");
  264. gsl_wave_chunk_close(wchunk_);
  265. gsl_wave_chunk_unref(wchunk_);
  266. wchunk_ = NULL;
  267. }
  268. }
  269. void createWaveChunk()
  270. {
  271. deleteWaveChunk();
  272. if(!gslDHandle_.isNull() && gslDHandle_.isOpen())
  273. {
  274. GslDataCache *dcache = gslDHandle_.createGslDataCache();
  275. if(!dcache)
  276. {
  277. arts_debug("FATAL: creating data cache failed!");
  278. finished(true); // FIXME: can this happen?
  279. }
  280. else
  281. {
  282. wchunk_ =
  283. gsl_wave_chunk_new(dcache,
  284. OSCILLATOR_FREQUENCY,
  285. mixerFrequency_,
  286. GSL_WAVE_LOOP_NONE,
  287. 0, 0, 0);
  288. arts_debug("DataHandlePlay_impl: open()ing gsl_wave_chunk");
  289. wchunkError_ = gsl_wave_chunk_open(wchunk_);
  290. gsl_data_cache_unref(dcache);
  291. }
  292. }
  293. }
  294. void deleteWaveOsc()
  295. {
  296. if(wosc_)
  297. {
  298. gsl_wave_osc_shutdown(wosc_);
  299. delete wosc_;
  300. wosc_ = NULL;
  301. }
  302. }
  303. void configureWaveOsc()
  304. {
  305. if(wchunk_)
  306. {
  307. GslWaveOscConfig config;
  308. memset(&config, 0, sizeof (GslWaveOscConfig));
  309. config.start_offset = 0;
  310. config.play_dir = 1;
  311. config.wchunk_data = (gpointer)wchunk_;
  312. config.wchunk_from_freq = &const_wchunk_from_freq;
  313. config.channel = channelIndex();
  314. config.cfreq = OSCILLATOR_FREQUENCY * speed();
  315. if(!wosc_)
  316. {
  317. wosc_ = new GslWaveOscData();
  318. gsl_wave_osc_init(wosc_);
  319. }
  320. gsl_wave_osc_config(wosc_, &config);
  321. }
  322. }
  323. void calculateBlock(unsigned long samples)
  324. {
  325. if(!paused_ && wchunk_)
  326. {
  327. if (!gsl_wave_osc_process(wosc_, samples, 0, 0, 0, outvalue))
  328. arts_debug("gsl_wave_osc_process failed.");
  329. finished(wosc_->done);
  330. }
  331. else
  332. for(unsigned long i = 0; i < samples; i++)
  333. outvalue[i] = 0.f;
  334. /*
  335. unsigned long haveSamples = 0;
  336. while(haveSamples != samples)
  337. {
  338. long n = min(gslDHandle_.valueCount() - pos_, samples - haveSamples);
  339. long got = gslDHandle_.isNull() ?
  340. 0 : gslDHandle_.read(pos_, n, &outvalue[haveSamples]);
  341. if(got <= 0)
  342. {
  343. for(unsigned long i = haveSamples; i < samples; i++)
  344. outvalue[i] = 0.f;
  345. break;
  346. }
  347. pos_ += got;
  348. haveSamples += got;
  349. }
  350. finished(pos_ >= gslDHandle_.valueCount());
  351. */
  352. }
  353. DataHandlePlay clone()
  354. {
  355. arts_debug("DataHandlePlay_impl: clone()ing myself");
  356. DataHandlePlay_impl *result = new DataHandlePlay_impl();
  357. result->dhandle_ = dhandle_;
  358. result->gslDHandle_ = gslDHandle_;
  359. result->errno_ = errno_;
  360. createWaveChunk();
  361. if(wchunk_)
  362. {
  363. result->wchunk_ = wchunk_;
  364. gsl_wave_chunk_ref(wchunk_);
  365. result->wchunkError_ = gsl_wave_chunk_open(wchunk_);
  366. }
  367. else
  368. result->wchunkError_ = wchunkError_;
  369. result->mixerFrequency_ = mixerFrequency_;
  370. result->channelIndex_ = channelIndex_;
  371. result->speed_ = speed_;
  372. result->pos_ = pos_;
  373. result->finished_ = finished_;
  374. result->paused_ = paused_;
  375. return DataHandlePlay::_from_base(result);
  376. }
  377. };
  378. class WaveDataHandle_impl : virtual public DataHandle_impl,
  379. virtual public WaveDataHandle_skel
  380. {
  381. GSL::WaveDataHandle wdhandle_;
  382. public:
  383. WaveDataHandle_impl() : wdhandle_(GSL::WaveDataHandle::null()) {}
  384. float mixerFrequency()
  385. {
  386. return wdhandle_.mixerFrequency();
  387. }
  388. float oscillatorFrequency()
  389. {
  390. return wdhandle_.oscillatorFrequency();
  391. }
  392. bool load(const string& filename)
  393. {
  394. return load(filename, 0, 0);
  395. }
  396. bool load(const string& filename,
  397. long waveIndex,
  398. long chunkIndex)
  399. {
  400. wdhandle_ = GSL::WaveDataHandle(filename, waveIndex, chunkIndex);
  401. if(dhandle_.isOpen())
  402. dhandle_.close();
  403. dhandle_ = wdhandle_;
  404. errno_ = dhandle_.isNull() ? 0 : dhandle_.open();
  405. return isLoaded();
  406. }
  407. bool isLoaded()
  408. {
  409. if(wdhandle_.isNull())
  410. return false;
  411. return !wdhandle_.error();
  412. }
  413. DataHandlePlay createPlayer()
  414. {
  415. DataHandlePlay_impl *result= new DataHandlePlay_impl();
  416. result->mixerFrequency(mixerFrequency());
  417. result->handle(WaveDataHandle::_from_base(this->_copy()));
  418. return DataHandlePlay::_from_base(result);
  419. }
  420. };
  421. REGISTER_IMPLEMENTATION(DataHandlePlay_impl);
  422. REGISTER_IMPLEMENTATION(DataHandle_impl);
  423. REGISTER_IMPLEMENTATION(CroppedDataHandle_impl);
  424. REGISTER_IMPLEMENTATION(CutDataHandle_impl);
  425. REGISTER_IMPLEMENTATION(ReversedDataHandle_impl);
  426. REGISTER_IMPLEMENTATION(WaveDataHandle_impl);
  427. }