PolySound.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyGlobals.h"
  21. #include "PolyVector3.h"
  22. #include "PolyString.h"
  23. #if defined(__APPLE__) && defined(__MACH__)
  24. #include <OpenAL/al.h>
  25. #include <OpenAL/alc.h>
  26. #else
  27. #include "al.h"
  28. #include "alc.h"
  29. #endif
  30. #define ALNoErrorStr "No AL error occurred"
  31. #define ALInvalidNameStr "AL error: a bad name (ID) was passed to an OpenAL function"
  32. #define ALInvalidEnumStr "AL error: an invalid enum value was passed to an OpenAL function"
  33. #define ALInvalidValueStr "AL error: an invalid value was passed to an OpenAL function"
  34. #define ALInvalidOpStr "AL error: the requested operation is not valid"
  35. #define ALOutOfMemoryStr "AL error: the requested operation resulted in OpenAL running out of memory"
  36. #define ALOtherErrorStr "AL error: unknown error"
  37. #define BUFFER_SIZE 32768
  38. namespace Polycode {
  39. class String;
  40. /**
  41. * Loads and plays a sound. This class can load and play an OGG or WAV sound file.
  42. */
  43. class _PolyExport Sound : public PolyBase {
  44. public:
  45. /**
  46. * Constructor.
  47. * @param fileName Path to an OGG or WAV file to load.
  48. */
  49. Sound(const String& fileName);
  50. Sound(const char *data, int size, int channels = 1, ALsizei freq = 44100, int bps = 16);
  51. virtual ~Sound();
  52. void loadFile(String fileName);
  53. void reloadProperties();
  54. /**
  55. * Play the sound once or in a loop.
  56. * @param once If this is true, play it once, otherwise, loop.
  57. */
  58. void Play(bool loop=false);
  59. /**
  60. * Stop the sound playback.
  61. */
  62. void Stop();
  63. /**
  64. * Sets the volume of this sound.
  65. * @param newVolume A Number 0-1, where 0 is no sound and 1 is the loudest.
  66. */
  67. void setVolume(Number newVolume);
  68. Number getVolume();
  69. /**
  70. * Sets the pitch of this sound.
  71. * @param newPitch A Number 0-1.
  72. */
  73. void setPitch(Number newPitch);
  74. Number getPitch();
  75. /**
  76. * Returns true if the sound is playing.
  77. * @return True if sound is playing, false if otherwise.
  78. */
  79. bool isPlaying();
  80. void setIsPositional(bool isPositional);
  81. void setSoundPosition(Vector3 position);
  82. void setSoundVelocity(Vector3 velocity);
  83. void setSoundDirection(Vector3 direction);
  84. /**
  85. * Sets the current sample offset of this sound.
  86. * @param off A number 0 <= off < sound sample length
  87. */
  88. void setOffset(int off);
  89. String getFileName();
  90. Number getPlaybackDuration();
  91. Number getPlaybackTime();
  92. void seekTo(Number time);
  93. /**
  94. * Returns the current sample offset (playback progress) of this sound.
  95. * @return The sample offset if it is known, -1 otherwise.
  96. */
  97. int getOffset();
  98. /**
  99. * Returns the number of samples in the sound.
  100. * @return The sample length if it is known, -1 otherwise.
  101. */
  102. int getSampleLength();
  103. void setPositionalProperties(Number referenceDistance, Number maxDistance);
  104. void setReferenceDistance(Number referenceDistance);
  105. void setMaxDistance(Number maxDistance);
  106. Number getReferenceDistance();
  107. Number getMaxDistance();
  108. ALuint loadBytes(const char *data, int size, int channels = 1, ALsizei freq = 44100, int bps = 16);
  109. ALuint loadWAV(const String& fileName);
  110. ALuint loadOGG(const String& fileName);
  111. ALuint GenSource(ALuint buffer);
  112. ALuint GenSource();
  113. ALenum checkALError(const String& operation);
  114. void soundError(const String& err);
  115. void soundCheck(bool result, const String& err);
  116. static unsigned long readByte32(const unsigned char buffer[4]);
  117. static unsigned short readByte16(const unsigned char buffer[2]);
  118. protected:
  119. Number referenceDistance;
  120. Number maxDistance;
  121. Number pitch;
  122. Number volume;
  123. String fileName;
  124. bool soundLoaded;
  125. bool isPositional;
  126. ALuint buffer; // Kept around only for deletion purposes
  127. ALuint soundSource;
  128. int sampleLength;
  129. };
  130. }