💿🐜 Antkeeper source code https://antkeeper.com
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.

121 lines
3.5 KiB

  1. /*
  2. * Copyright (C) 2017-2019 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper Source Code.
  5. *
  6. * Antkeeper Source Code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper Source Code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "sound-system.hpp"
  20. #include <stdexcept>
  21. #include "dr_libs/dr_wav.h"
  22. SoundSystem::SoundSystem(ComponentManager* componentManager):
  23. System(componentManager),
  24. entityGroup(componentManager),
  25. device(nullptr),
  26. context(nullptr)
  27. {
  28. device = alcOpenDevice(nullptr);
  29. if (!device)
  30. {
  31. throw std::runtime_error("SoundSystem::SoundSystem(): Failed to open audio device.");
  32. }
  33. context = alcCreateContext(device, nullptr);
  34. if (!alcMakeContextCurrent(context))
  35. {
  36. throw std::runtime_error("SoundSystem::SoundSystem(): Failed to create audio context.");
  37. }
  38. ALfloat listenerOrientation[] = { 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f };
  39. alListener3f(AL_POSITION, 0.0f, 0.0f, 1.0f);
  40. alListener3f(AL_VELOCITY, 0.0f, 0.0f, 0.0f);
  41. alListenerfv(AL_ORIENTATION, listenerOrientation);
  42. alGenSources((ALuint)1, &source);
  43. alSourcef(source, AL_PITCH, 1);
  44. alSourcef(source, AL_GAIN, 1);
  45. alSource3f(source, AL_POSITION, 0, 0, 0);
  46. alSource3f(source, AL_VELOCITY, 0, 0, 0);
  47. alSourcei(source, AL_LOOPING, AL_FALSE);
  48. alGenBuffers((ALuint)1, &buffer);
  49. /*
  50. // Load wav file
  51. {
  52. const char* filename = "data/shutter.wav";
  53. unsigned int channels;
  54. unsigned int sampleRate;
  55. drwav_uint64 frameCount;
  56. std::int16_t* sampleData = drwav_open_file_and_read_pcm_frames_s16(filename, &channels, &sampleRate, &frameCount);
  57. if (sampleData == nullptr)
  58. {
  59. throw std::runtime_error("Couldn't load wav file");
  60. drwav_free(sampleData);
  61. }
  62. bool stereo = (channels > 1);
  63. ALenum format = (stereo) ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16;
  64. std::size_t sampleCount = frameCount * channels;
  65. std::size_t sampleDataSize = sampleCount * sizeof(drwav_int16);
  66. alBufferData(buffer, format, sampleData, sampleDataSize, sampleRate);
  67. }
  68. */
  69. alSourcei(source, AL_BUFFER, buffer);
  70. //alSourcePlay(source);
  71. }
  72. SoundSystem::~SoundSystem()
  73. {
  74. alDeleteSources(1, &source);
  75. alDeleteBuffers(1, &buffer);
  76. alcMakeContextCurrent(nullptr);
  77. alcDestroyContext(context);
  78. alcCloseDevice(device);
  79. }
  80. void SoundSystem::scrot()
  81. {
  82. alSourcePlay(source);
  83. }
  84. void SoundSystem::update(float t, float dt)
  85. {
  86. auto members = entityGroup.getMembers();
  87. for (const SoundSourceEntityGroup::Member* member: *members)
  88. {
  89. TransformComponent* transform = std::get<0>(member->components);
  90. SoundSourceComponent* sound = std::get<1>(member->components);
  91. }
  92. }
  93. void SoundSystem::memberRegistered(const SoundSourceEntityGroup::Member* member)
  94. {
  95. TransformComponent* transform = std::get<0>(member->components);
  96. SoundSourceComponent* sound = std::get<1>(member->components);
  97. }
  98. void SoundSystem::memberUnregistered(const SoundSourceEntityGroup::Member* member)
  99. {
  100. TransformComponent* transform = std::get<0>(member->components);
  101. SoundSourceComponent* sound = std::get<1>(member->components);
  102. }