Audio plugin host https://kx.studio/carla
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.

809 lines
21KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2012-2013 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the GPL.txt file
  16. */
  17. // for UINT32_MAX
  18. #define __STDC_LIMIT_MACROS
  19. #include <cstdint>
  20. #include "CarlaNative.hpp"
  21. #include "CarlaMIDI.h"
  22. #include "CarlaString.hpp"
  23. #include "RtList.hpp"
  24. #include <QtCore/QThread>
  25. #include "zynaddsubfx/Misc/Master.h"
  26. #include "zynaddsubfx/Misc/Part.h"
  27. #include "zynaddsubfx/Misc/Util.h"
  28. #ifdef WANT_ZYNADDSUBFX_UI
  29. // FIXME
  30. # ifdef override
  31. # define override_hack
  32. # undef override
  33. # endif
  34. # include "zynaddsubfx/UI/common.H"
  35. # include "zynaddsubfx/UI/MasterUI.h"
  36. # include <FL/Fl_Shared_Image.H>
  37. # include <FL/Fl_Tiled_Image.H>
  38. # include <FL/Fl_Dial.H>
  39. # include <FL/Fl_Theme.H>
  40. # ifdef override_hack
  41. # define override
  42. # undef override_hack
  43. # endif
  44. #endif
  45. #include <ctime>
  46. #include <set>
  47. #include <string>
  48. // Dummy variables and functions for linking purposes
  49. const char* instance_name = nullptr;
  50. class WavFile;
  51. namespace Nio {
  52. bool start(void){return 1;}
  53. void stop(void){}
  54. bool setSource(std::string){return true;}
  55. bool setSink(std::string){return true;}
  56. std::set<std::string> getSources(void){return std::set<std::string>();}
  57. std::set<std::string> getSinks(void){return std::set<std::string>();}
  58. std::string getSource(void){return "";}
  59. std::string getSink(void){return "";}
  60. void waveNew(WavFile*){}
  61. void waveStart(void){}
  62. void waveStop(void){}
  63. void waveEnd(void){}
  64. }
  65. SYNTH_T* synth = nullptr;
  66. #ifdef WANT_ZYNADDSUBFX_UI
  67. #define PIXMAP_PATH "/resources/zynaddsubfx/"
  68. static Fl_Tiled_Image* gModuleBackdrop = nullptr;
  69. static CarlaString gPixmapPath;
  70. extern CarlaString gUiPixmapPath;
  71. void set_module_parameters(Fl_Widget* o)
  72. {
  73. CARLA_ASSERT(gModuleBackdrop != nullptr);
  74. o->box(FL_DOWN_FRAME);
  75. o->align(o->align() | FL_ALIGN_IMAGE_BACKDROP);
  76. o->color(FL_BLACK);
  77. o->labeltype(FL_SHADOW_LABEL);
  78. if (gModuleBackdrop != nullptr)
  79. o->image(gModuleBackdrop);
  80. }
  81. #endif
  82. class ZynAddSubFxPlugin : public PluginDescriptorClass
  83. {
  84. public:
  85. enum Parameters {
  86. PARAMETER_COUNT = 0
  87. };
  88. ZynAddSubFxPlugin(const HostDescriptor* const host)
  89. : PluginDescriptorClass(host),
  90. fMaster(new Master()),
  91. fSampleRate(getSampleRate()),
  92. fIsActive(false),
  93. fIsOffline(false),
  94. fThread(fMaster, host)
  95. {
  96. fThread.start();
  97. maybeInitPrograms(fMaster);
  98. for (int i = 0; i < NUM_MIDI_PARTS; ++i)
  99. fMaster->partonoff(i, 1);
  100. }
  101. ~ZynAddSubFxPlugin() override
  102. {
  103. //ensure that everything has stopped
  104. pthread_mutex_lock(&fMaster->mutex);
  105. pthread_mutex_unlock(&fMaster->mutex);
  106. fThread.stop();
  107. fThread.wait();
  108. delete fMaster;
  109. fMaster = nullptr;
  110. }
  111. protected:
  112. // -------------------------------------------------------------------
  113. // Plugin parameter calls
  114. uint32_t getParameterCount() override
  115. {
  116. return PARAMETER_COUNT;
  117. }
  118. const Parameter* getParameterInfo(const uint32_t index) override
  119. {
  120. CARLA_ASSERT(index < getParameterCount());
  121. //if (index >= PARAMETER_COUNT)
  122. return nullptr;
  123. static Parameter param;
  124. param.ranges.step = PARAMETER_RANGES_DEFAULT_STEP;
  125. param.ranges.stepSmall = PARAMETER_RANGES_DEFAULT_STEP_SMALL;
  126. param.ranges.stepLarge = PARAMETER_RANGES_DEFAULT_STEP_LARGE;
  127. param.scalePointCount = 0;
  128. param.scalePoints = nullptr;
  129. switch (index)
  130. {
  131. #if 0
  132. case PARAMETER_MASTER:
  133. param.hints = PARAMETER_IS_ENABLED | PARAMETER_IS_AUTOMABLE;
  134. param.name = "Master Volume";
  135. param.unit = nullptr;
  136. param.ranges.min = 0.0f;
  137. param.ranges.max = 100.0f;
  138. param.ranges.def = 100.0f;
  139. break;
  140. #endif
  141. }
  142. return &param;
  143. }
  144. float getParameterValue(const uint32_t index) override
  145. {
  146. CARLA_ASSERT(index < getParameterCount());
  147. switch (index)
  148. {
  149. #if 0
  150. case PARAMETER_MASTER:
  151. return fMaster->Pvolume;
  152. #endif
  153. default:
  154. return 0.0f;
  155. }
  156. }
  157. // -------------------------------------------------------------------
  158. // Plugin midi-program calls
  159. uint32_t getMidiProgramCount() override
  160. {
  161. return sPrograms.count();
  162. }
  163. const MidiProgram* getMidiProgramInfo(const uint32_t index) override
  164. {
  165. CARLA_ASSERT(index < getMidiProgramCount());
  166. if (index >= sPrograms.count())
  167. return nullptr;
  168. const ProgramInfo* const pInfo(sPrograms.getAt(index));
  169. static MidiProgram midiProgram;
  170. midiProgram.bank = pInfo->bank;
  171. midiProgram.program = pInfo->prog;
  172. midiProgram.name = pInfo->name;
  173. return &midiProgram;
  174. }
  175. // -------------------------------------------------------------------
  176. // Plugin state calls
  177. void setParameterValue(const uint32_t index, const float value) override
  178. {
  179. CARLA_ASSERT(index < getParameterCount());
  180. switch (index)
  181. {
  182. }
  183. return;
  184. // unused, TODO
  185. (void)value;
  186. }
  187. void setMidiProgram(const uint8_t channel, const uint32_t bank, const uint32_t program) override
  188. {
  189. if (bank >= fMaster->bank.banks.size())
  190. return;
  191. if (program >= BANK_SIZE)
  192. return;
  193. if (fIsOffline || ! fIsActive)
  194. {
  195. loadProgram(fMaster, channel, bank, program);
  196. #ifdef WANT_ZYNADDSUBFX_UI
  197. fThread.uiRepaint();
  198. #endif
  199. }
  200. else
  201. fThread.loadLater(channel, bank, program);
  202. }
  203. void setCustomData(const char* const key, const char* const value) override
  204. {
  205. CARLA_ASSERT(key != nullptr);
  206. CARLA_ASSERT(value != nullptr);
  207. if (std::strcmp(key, "CarlaAlternateFile1") == 0) // xmz
  208. fMaster->loadXML(value);
  209. else if (std::strcmp(key, "CarlaAlternateFile2") == 0) // xiz
  210. fMaster->part[0]->loadXMLinstrument(value);
  211. }
  212. // -------------------------------------------------------------------
  213. // Plugin process calls
  214. void activate() override
  215. {
  216. fIsActive = true;
  217. }
  218. void deactivate() override
  219. {
  220. fIsActive = false;
  221. }
  222. void process(float**, float** const outBuffer, const uint32_t frames, const uint32_t midiEventCount, const MidiEvent* const midiEvents) override
  223. {
  224. if (pthread_mutex_trylock(&fMaster->mutex) != 0)
  225. {
  226. carla_zeroFloat(outBuffer[0], frames);
  227. carla_zeroFloat(outBuffer[1], frames);
  228. return;
  229. }
  230. for (uint32_t i=0; i < midiEventCount; ++i)
  231. {
  232. const MidiEvent* const midiEvent(&midiEvents[i]);
  233. const uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent->data);
  234. const uint8_t channel = MIDI_GET_CHANNEL_FROM_DATA(midiEvent->data);
  235. if (MIDI_IS_STATUS_NOTE_OFF(status))
  236. {
  237. const uint8_t note = midiEvent->data[1];
  238. fMaster->noteOff(channel, note);
  239. }
  240. else if (MIDI_IS_STATUS_NOTE_ON(status))
  241. {
  242. const uint8_t note = midiEvent->data[1];
  243. const uint8_t velo = midiEvent->data[2];
  244. fMaster->noteOn(channel, note, velo);
  245. }
  246. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status))
  247. {
  248. const uint8_t note = midiEvent->data[1];
  249. const uint8_t pressure = midiEvent->data[2];
  250. fMaster->polyphonicAftertouch(channel, note, pressure);
  251. }
  252. else if (MIDI_IS_STATUS_CONTROL_CHANGE(status))
  253. {
  254. const uint8_t control = midiEvent->data[1];
  255. const uint8_t value = midiEvent->data[2];
  256. fMaster->setController(channel, control, value);
  257. }
  258. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
  259. {
  260. const uint8_t lsb = midiEvent->data[1];
  261. const uint8_t msb = midiEvent->data[2];
  262. const int value = ((msb << 7) | lsb) - 8192;
  263. fMaster->setController(channel, C_pitchwheel, value);
  264. }
  265. }
  266. fMaster->GetAudioOutSamples(frames, fSampleRate, outBuffer[0], outBuffer[1]);
  267. pthread_mutex_unlock(&fMaster->mutex);
  268. }
  269. #ifdef WANT_ZYNADDSUBFX_UI
  270. // -------------------------------------------------------------------
  271. // Plugin UI calls
  272. void uiShow(const bool show) override
  273. {
  274. if (show)
  275. fThread.uiShow();
  276. else
  277. fThread.uiHide();
  278. }
  279. #endif
  280. // -------------------------------------------------------------------
  281. // Plugin state calls
  282. char* getState() override
  283. {
  284. config.save();
  285. char* data = nullptr;
  286. fMaster->getalldata(&data);
  287. return data;
  288. }
  289. void setState(const char* const data) override
  290. {
  291. fThread.stopLoadLater();
  292. fMaster->putalldata((char*)data, 0);
  293. fMaster->applyparameters(true);
  294. }
  295. // -------------------------------------------------------------------
  296. // Plugin dispatcher
  297. intptr_t pluginDispatcher(const PluginDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr) override
  298. {
  299. switch (opcode)
  300. {
  301. case PLUGIN_OPCODE_NULL:
  302. break;
  303. case PLUGIN_OPCODE_BUFFER_SIZE_CHANGED:
  304. // TODO
  305. break;
  306. case PLUGIN_OPCODE_SAMPLE_RATE_CHANGED:
  307. // TODO
  308. break;
  309. case PLUGIN_OPCODE_UI_NAME_CHANGED:
  310. #ifdef WANT_ZYNADDSUBFX_UI
  311. // TODO
  312. #endif
  313. break;
  314. }
  315. return 0;
  316. // unused
  317. (void)index;
  318. (void)value;
  319. (void)ptr;
  320. }
  321. // -------------------------------------------------------------------
  322. private:
  323. struct ProgramInfo {
  324. uint32_t bank;
  325. uint32_t prog;
  326. const char* name;
  327. ProgramInfo(uint32_t bank_, uint32_t prog_, const char* name_)
  328. : bank(bank_),
  329. prog(prog_),
  330. name(carla_strdup(name_)) {}
  331. ~ProgramInfo()
  332. {
  333. if (name != nullptr)
  334. {
  335. delete[] name;
  336. name = nullptr;
  337. }
  338. }
  339. #ifdef CARLA_PROPER_CPP11_SUPPORT
  340. ProgramInfo() = delete;
  341. ProgramInfo(ProgramInfo&) = delete;
  342. ProgramInfo(const ProgramInfo&) = delete;
  343. #endif
  344. };
  345. class ZynThread : public QThread
  346. {
  347. public:
  348. ZynThread(Master* const master, const HostDescriptor* const host)
  349. : fMaster(master),
  350. kHost(host),
  351. #ifdef WANT_ZYNADDSUBFX_UI
  352. fUi(nullptr),
  353. fUiClosed(0),
  354. fNextUiAction(-1),
  355. #endif
  356. fQuit(false),
  357. fChangeProgram(false),
  358. fNextChannel(0),
  359. fNextBank(0),
  360. fNextProgram(0)
  361. {
  362. }
  363. ~ZynThread()
  364. {
  365. // must be closed by now
  366. #ifdef WANT_ZYNADDSUBFX_UI
  367. CARLA_ASSERT(fUi == nullptr);
  368. #endif
  369. CARLA_ASSERT(fQuit);
  370. }
  371. void loadLater(const uint8_t channel, const uint32_t bank, const uint32_t program)
  372. {
  373. fNextChannel = channel;
  374. fNextBank = bank;
  375. fNextProgram = program;
  376. fChangeProgram = true;
  377. }
  378. void stopLoadLater()
  379. {
  380. fChangeProgram = false;
  381. fNextChannel = 0;
  382. fNextBank = 0;
  383. fNextProgram = 0;
  384. }
  385. void stop()
  386. {
  387. fQuit = true;
  388. quit();
  389. }
  390. #ifdef WANT_ZYNADDSUBFX_UI
  391. void uiHide()
  392. {
  393. fNextUiAction = 0;
  394. }
  395. void uiShow()
  396. {
  397. fNextUiAction = 1;
  398. }
  399. void uiRepaint()
  400. {
  401. if (fUi != nullptr)
  402. fNextUiAction = 2;
  403. }
  404. #endif
  405. protected:
  406. void run() override
  407. {
  408. while (! fQuit)
  409. {
  410. #ifdef WANT_ZYNADDSUBFX_UI
  411. Fl::lock();
  412. if (fNextUiAction == 2) // repaint
  413. {
  414. CARLA_ASSERT(fUi != nullptr);
  415. if (fUi != nullptr)
  416. fUi->refresh_master_ui();
  417. }
  418. else if (fNextUiAction == 1) // init/show
  419. {
  420. static bool initialized = false;
  421. if (! initialized)
  422. {
  423. initialized = true;
  424. fl_register_images();
  425. Fl_Dial::default_style(Fl_Dial::PIXMAP_DIAL);
  426. if (Fl_Shared_Image* const img = Fl_Shared_Image::get(gPixmapPath + "knob.png"))
  427. Fl_Dial::default_image(img);
  428. if (Fl_Shared_Image* const img = Fl_Shared_Image::get(gPixmapPath + "window_backdrop.png"))
  429. Fl::scheme_bg(new Fl_Tiled_Image(img));
  430. if(Fl_Shared_Image* const img = Fl_Shared_Image::get(gPixmapPath + "module_backdrop.png"))
  431. gModuleBackdrop = new Fl_Tiled_Image(img);
  432. Fl::background(50, 50, 50);
  433. Fl::background2(70, 70, 70);
  434. Fl::foreground(255, 255, 255);
  435. Fl_Theme::set("Cairo");
  436. }
  437. CARLA_ASSERT(fUi == nullptr);
  438. if (fUi == nullptr)
  439. {
  440. fUiClosed = 0;
  441. fUi = new MasterUI(fMaster, &fUiClosed);
  442. fUi->showUI();
  443. }
  444. }
  445. else if (fNextUiAction == 0) // close
  446. {
  447. CARLA_ASSERT(fUi != nullptr);
  448. if (fUi != nullptr)
  449. {
  450. delete fUi;
  451. fUi = nullptr;
  452. }
  453. }
  454. fNextUiAction = -1;
  455. if (fUiClosed != 0)
  456. {
  457. fUiClosed = 0;
  458. fNextUiAction = 0;
  459. kHost->ui_closed(kHost->handle);
  460. }
  461. Fl::check();
  462. Fl::unlock();
  463. #endif
  464. if (fChangeProgram)
  465. {
  466. fChangeProgram = false;
  467. loadProgram(fMaster, fNextChannel, fNextBank, fNextProgram);
  468. fNextChannel = 0;
  469. fNextBank = 0;
  470. fNextProgram = 0;
  471. #ifdef WANT_ZYNADDSUBFX_UI
  472. if (fUi != nullptr)
  473. {
  474. Fl::lock();
  475. fUi->refresh_master_ui();
  476. Fl::unlock();
  477. }
  478. #endif
  479. carla_msleep(15);
  480. }
  481. else
  482. {
  483. carla_msleep(30);
  484. }
  485. }
  486. #ifdef WANT_ZYNADDSUBFX_UI
  487. if (fQuit && fUi != nullptr)
  488. {
  489. Fl::lock();
  490. delete fUi;
  491. fUi = nullptr;
  492. Fl::check();
  493. Fl::unlock();
  494. }
  495. #endif
  496. }
  497. private:
  498. Master* const fMaster;
  499. const HostDescriptor* const kHost;
  500. #ifdef WANT_ZYNADDSUBFX_UI
  501. MasterUI* fUi;
  502. int fUiClosed;
  503. int fNextUiAction;
  504. #endif
  505. bool fQuit;
  506. bool fChangeProgram;
  507. uint8_t fNextChannel;
  508. uint32_t fNextBank;
  509. uint32_t fNextProgram;
  510. };
  511. Master* const fMaster;
  512. unsigned fSampleRate;
  513. bool fIsActive;
  514. bool fIsOffline;
  515. ZynThread fThread;
  516. static int sInstanceCount;
  517. static NonRtList<ProgramInfo*> sPrograms;
  518. static void maybeInitPrograms(Master* const master)
  519. {
  520. static bool doSearch = true;
  521. if (! doSearch)
  522. return;
  523. doSearch = false;
  524. sPrograms.append(new ProgramInfo(0, 0, "default"));
  525. pthread_mutex_lock(&master->mutex);
  526. // refresh banks
  527. master->bank.rescanforbanks();
  528. for (uint32_t i=0, size = master->bank.banks.size(); i < size; ++i)
  529. {
  530. if (master->bank.banks[i].dir.empty())
  531. continue;
  532. master->bank.loadbank(master->bank.banks[i].dir);
  533. for (unsigned int instrument = 0; instrument < BANK_SIZE; ++instrument)
  534. {
  535. const std::string insName(master->bank.getname(instrument));
  536. if (insName.empty() || insName[0] == '\0' || insName[0] == ' ')
  537. continue;
  538. sPrograms.append(new ProgramInfo(i+1, instrument, insName.c_str()));
  539. }
  540. }
  541. pthread_mutex_unlock(&master->mutex);
  542. }
  543. static void loadProgram(Master* const master, const uint8_t channel, const uint32_t bank, const uint32_t program)
  544. {
  545. if (bank == 0)
  546. {
  547. pthread_mutex_lock(&master->mutex);
  548. master->part[channel]->defaults();
  549. master->part[channel]->applyparameters(false);
  550. master->partonoff(channel, 1);
  551. pthread_mutex_unlock(&master->mutex);
  552. return;
  553. }
  554. const std::string& bankdir(master->bank.banks[bank-1].dir);
  555. if (! bankdir.empty())
  556. {
  557. pthread_mutex_lock(&master->mutex);
  558. master->partonoff(channel, 1);
  559. master->bank.loadbank(bankdir);
  560. master->bank.loadfromslot(program, master->part[channel]);
  561. master->part[channel]->applyparameters(false);
  562. pthread_mutex_unlock(&master->mutex);
  563. }
  564. }
  565. public:
  566. static PluginHandle _instantiate(const PluginDescriptor*, HostDescriptor* host)
  567. {
  568. if (sInstanceCount++ == 0)
  569. {
  570. CARLA_ASSERT(synth == nullptr);
  571. CARLA_ASSERT(denormalkillbuf == nullptr);
  572. synth = new SYNTH_T();
  573. synth->buffersize = host->get_buffer_size(host->handle);
  574. synth->samplerate = host->get_sample_rate(host->handle);
  575. synth->alias();
  576. config.init();
  577. config.cfg.SoundBufferSize = synth->buffersize;
  578. config.cfg.SampleRate = synth->samplerate;
  579. config.cfg.GzipCompression = 0;
  580. sprng(std::time(nullptr));
  581. denormalkillbuf = new float[synth->buffersize];
  582. for (int i=0; i < synth->buffersize; ++i)
  583. denormalkillbuf[i] = (RND - 0.5f) * 1e-16;
  584. Master::getInstance();
  585. #ifdef WANT_ZYNADDSUBFX_UI
  586. if (gPixmapPath.isEmpty())
  587. {
  588. gPixmapPath = host->resource_dir;
  589. gPixmapPath += PIXMAP_PATH;
  590. gUiPixmapPath = gPixmapPath;
  591. }
  592. #endif
  593. }
  594. return new ZynAddSubFxPlugin(host);
  595. }
  596. static void _cleanup(PluginHandle handle)
  597. {
  598. delete (ZynAddSubFxPlugin*)handle;
  599. if (--sInstanceCount == 0)
  600. {
  601. CARLA_ASSERT(synth != nullptr);
  602. CARLA_ASSERT(denormalkillbuf != nullptr);
  603. Master::deleteInstance();
  604. delete[] denormalkillbuf;
  605. denormalkillbuf = nullptr;
  606. delete synth;
  607. synth = nullptr;
  608. }
  609. }
  610. static void _clearPrograms()
  611. {
  612. for (auto it = sPrograms.begin(); it.valid(); it.next())
  613. {
  614. ProgramInfo* const programInfo(*it);
  615. delete programInfo;
  616. }
  617. sPrograms.clear();
  618. }
  619. private:
  620. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ZynAddSubFxPlugin)
  621. };
  622. int ZynAddSubFxPlugin::sInstanceCount = 0;
  623. NonRtList<ZynAddSubFxPlugin::ProgramInfo*> ZynAddSubFxPlugin::sPrograms;
  624. static const struct ProgramsDestructor {
  625. ProgramsDestructor() {}
  626. ~ProgramsDestructor() {
  627. ZynAddSubFxPlugin::_clearPrograms();
  628. }
  629. } _programsDestructor;
  630. // -----------------------------------------------------------------------
  631. static const PluginDescriptor zynaddsubfxDesc = {
  632. /* category */ PLUGIN_CATEGORY_SYNTH,
  633. #ifdef WANT_ZYNADDSUBFX_UI
  634. /* hints */ static_cast<PluginHints>(PLUGIN_IS_SYNTH|PLUGIN_HAS_GUI|PLUGIN_USES_STATE),
  635. #else
  636. /* hints */ static_cast<PluginHints>(PLUGIN_IS_SYNTH|PLUGIN_USES_STATE),
  637. #endif
  638. /* audioIns */ 0,
  639. /* audioOuts */ 2,
  640. /* midiIns */ 1,
  641. /* midiOuts */ 0,
  642. /* paramIns */ ZynAddSubFxPlugin::PARAMETER_COUNT,
  643. /* paramOuts */ 0,
  644. /* name */ "ZynAddSubFX",
  645. /* label */ "zynaddsubfx",
  646. /* maker */ "falkTX",
  647. /* copyright */ "GNU GPL v2+",
  648. PluginDescriptorFILL(ZynAddSubFxPlugin)
  649. };
  650. // -----------------------------------------------------------------------
  651. void carla_register_native_plugin_zynaddsubfx()
  652. {
  653. carla_register_native_plugin(&zynaddsubfxDesc);
  654. }
  655. // -----------------------------------------------------------------------