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.
 
 
 

119 lines
3.0 KiB

  1. //
  2. // Lol Engine — Sound tutorial
  3. //
  4. // Copyright © 2011—2019 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // Lol Engine is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #if HAVE_CONFIG_H
  13. # include "config.h"
  14. #endif
  15. #include <lol/engine.h>
  16. #include <functional>
  17. #include <array>
  18. using namespace lol;
  19. class sound_demo : public WorldEntity
  20. {
  21. public:
  22. sound_demo()
  23. {
  24. for (int i = 0; i < 2; ++i)
  25. {
  26. auto f = std::bind(&sound_demo::synth, this, i,
  27. std::placeholders::_1,
  28. std::placeholders::_2);
  29. m_streams[i] = audio::start_streaming(f);
  30. }
  31. for (size_t i = 0; i < m_instrument.size(); ++i)
  32. m_instrument[i] = (int16_t)(i % 80 * (10000 - lol::abs(i - 10000)) * 40 / 10000);
  33. m_sample = sample::create(m_instrument.data(), 40000);
  34. m_text = new Text("SPACE for sine wave, Left Click for white noise",
  35. "data/font/ascii.png");
  36. m_text->SetPos(vec3(5, 5, 1));
  37. Ticker::Ref(m_text);
  38. }
  39. ~sound_demo()
  40. {
  41. Ticker::Unref(m_text);
  42. }
  43. void synth(int channel, void *buf, int bytes)
  44. {
  45. int mode = (1 << channel) & m_mask;
  46. int16_t *stream = (int16_t *)buf;
  47. for (int i = 0; i < bytes / 2; i += 2)
  48. {
  49. switch (mode)
  50. {
  51. case 2: // square / triangle signals
  52. stream[i] = 800 * (i % 128 > 64 ? -1 : 1);
  53. stream[i + 1] = (i % 128 - 64) * 15;
  54. break;
  55. case 1: // white noise
  56. stream[i] = lol::rand(-2000, 2000);
  57. stream[i + 1] = lol::rand(-1000, 1000);
  58. break;
  59. case 0: // inactive
  60. stream[i] = stream[i + 1] = 0;
  61. break;
  62. }
  63. }
  64. }
  65. virtual void tick_game(float seconds) override
  66. {
  67. WorldEntity::tick_game(seconds);
  68. auto mouse = input::mouse();
  69. auto keyboard = input::keyboard();
  70. if (keyboard->key_pressed(input::key::SC_Return))
  71. m_sample->play();
  72. if (keyboard->key_pressed(input::key::SC_Space))
  73. m_mask ^= 2;
  74. if (mouse->button_pressed(input::button::BTN_Left))
  75. m_mask ^= 1;
  76. }
  77. virtual bool release_game() override
  78. {
  79. for (int i = 0; i < 2; ++i)
  80. audio::stop_streaming(m_streams[i]);
  81. return true;
  82. }
  83. private:
  84. int m_streams[2];
  85. int m_mask = 0;
  86. std::array<int16_t, 20000> m_instrument;
  87. sample* m_sample;
  88. Text *m_text;
  89. };
  90. int main(int argc, char **argv)
  91. {
  92. sys::init(argc, argv);
  93. Application app("Tutorial 9: Sound", ivec2(640, 480), 60.0f);
  94. new sound_demo();
  95. app.Run();
  96. return EXIT_SUCCESS;
  97. }