💿🐜 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.

119 lines
3.4 KiB

5 years ago
  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. // Load wav file
  50. {
  51. const char* filename = "shutter.wav";
  52. unsigned int channels;
  53. unsigned int sampleRate;
  54. drwav_uint64 frameCount;
  55. std::int16_t* sampleData = drwav_open_file_and_read_pcm_frames_s16(filename, &channels, &sampleRate, &frameCount);
  56. if (sampleData == nullptr)
  57. {
  58. throw std::runtime_error("Couldn't load wav file");
  59. drwav_free(sampleData);
  60. }
  61. bool stereo = (channels > 1);
  62. ALenum format = (stereo) ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16;
  63. std::size_t sampleCount = frameCount * channels;
  64. std::size_t sampleDataSize = sampleCount * sizeof(drwav_int16);
  65. alBufferData(buffer, format, sampleData, sampleDataSize, sampleRate);
  66. }
  67. alSourcei(source, AL_BUFFER, buffer);
  68. //alSourcePlay(source);
  69. }
  70. SoundSystem::~SoundSystem()
  71. {
  72. alDeleteSources(1, &source);
  73. alDeleteBuffers(1, &buffer);
  74. alcMakeContextCurrent(nullptr);
  75. alcDestroyContext(context);
  76. alcCloseDevice(device);
  77. }
  78. void SoundSystem::scrot()
  79. {
  80. alSourcePlay(source);
  81. }
  82. void SoundSystem::update(float t, float dt)
  83. {
  84. auto members = entityGroup.getMembers();
  85. for (const SoundSourceEntityGroup::Member* member: *members)
  86. {
  87. TransformComponent* transform = std::get<0>(member->components);
  88. SoundSourceComponent* sound = std::get<1>(member->components);
  89. }
  90. }
  91. void SoundSystem::memberRegistered(const SoundSourceEntityGroup::Member* member)
  92. {
  93. TransformComponent* transform = std::get<0>(member->components);
  94. SoundSourceComponent* sound = std::get<1>(member->components);
  95. }
  96. void SoundSystem::memberUnregistered(const SoundSourceEntityGroup::Member* member)
  97. {
  98. TransformComponent* transform = std::get<0>(member->components);
  99. SoundSourceComponent* sound = std::get<1>(member->components);
  100. }