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.
 
 
 

117 lines
2.9 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, audio::format::sint16le, 22050, 1);
  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)
  48. {
  49. switch (mode)
  50. {
  51. case 2: // triangle signal
  52. stream[i] = (i % 128 - 64) * 8;
  53. break;
  54. case 1: // white noise
  55. stream[i] = lol::rand(-2048, 2048);
  56. break;
  57. case 0: // inactive
  58. stream[i] = 0;
  59. break;
  60. }
  61. }
  62. }
  63. virtual void tick_game(float seconds) override
  64. {
  65. WorldEntity::tick_game(seconds);
  66. auto mouse = input::mouse();
  67. auto keyboard = input::keyboard();
  68. if (keyboard->key_pressed(input::key::SC_Return))
  69. m_sample->play();
  70. if (keyboard->key_pressed(input::key::SC_Space))
  71. m_mask ^= 2;
  72. if (mouse->button_pressed(input::button::BTN_Left))
  73. m_mask ^= 1;
  74. }
  75. virtual bool release_game() override
  76. {
  77. for (int i = 0; i < 2; ++i)
  78. audio::stop_streaming(m_streams[i]);
  79. return true;
  80. }
  81. private:
  82. int m_streams[2];
  83. int m_mask = 0;
  84. std::array<int16_t, 20000> m_instrument;
  85. sample* m_sample;
  86. Text *m_text;
  87. };
  88. int main(int argc, char **argv)
  89. {
  90. sys::init(argc, argv);
  91. Application app("Tutorial 9: Sound", ivec2(640, 480), 60.0f);
  92. new sound_demo();
  93. app.Run();
  94. return EXIT_SUCCESS;
  95. }