PolySound.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #include "PolyCoreServices.h"
  24. #if defined(__APPLE__) && defined(__MACH__)
  25. #include <OpenAL/al.h>
  26. #include <OpenAL/alc.h>
  27. #else
  28. #include "al.h"
  29. #include "alc.h"
  30. #endif
  31. #define ALNoErrorStr "No AL error occurred"
  32. #define ALInvalidNameStr "AL error: a bad name (ID) was passed to an OpenAL function"
  33. #define ALInvalidEnumStr "AL error: an invalid enum value was passed to an OpenAL function"
  34. #define ALInvalidValueStr "AL error: an invalid value was passed to an OpenAL function"
  35. #define ALInvalidOpStr "AL error: the requested operation is not valid"
  36. #define ALOutOfMemoryStr "AL error: the requested operation resulted in OpenAL running out of memory"
  37. #define ALOtherErrorStr "AL error: unknown error"
  38. #define BUFFER_SIZE 32768
  39. #define STREAMING_BUFFER_COUNT 4
  40. #define STREAMING_BUFFER_SIZE 4096
  41. namespace Polycode {
  42. class String;
  43. class AudioStreamingSource {
  44. public:
  45. AudioStreamingSource(unsigned int channels, unsigned int bps, unsigned int freq);
  46. POLYIGNORE virtual unsigned int streamData(char *buffer, unsigned int size);
  47. unsigned int getNumChannels();
  48. unsigned int getBitsPerSample();
  49. unsigned int getFrequency();
  50. protected:
  51. unsigned int channels;
  52. unsigned int bps;
  53. unsigned int freq;
  54. };
  55. /**
  56. * Loads and plays a sound. This class can load and play an OGG or WAV sound file.
  57. */
  58. class _PolyExport Sound : public PolyBase {
  59. public:
  60. /**
  61. * Constructor.
  62. * @param fileName Path to an OGG or WAV file to load.
  63. */
  64. Sound(const String& fileName, bool generateFloatBuffer = false);
  65. Sound(int size, const char *data, int channels = 1, ALsizei freq = 44100, int bps = 16, bool generateFloatBuffer = false);
  66. Sound(AudioStreamingSource *streamingSource);
  67. virtual ~Sound();
  68. void loadFile(String fileName, bool generateFloatBuffer);
  69. void reloadProperties();
  70. /**
  71. * Play the sound once or in a loop.
  72. * @param once If this is true, play it once, otherwise, loop.
  73. */
  74. void Play(bool loop=false);
  75. /**
  76. * Stop the sound playback.
  77. */
  78. void Stop();
  79. /**
  80. * Sets the volume of this sound.
  81. * @param newVolume A Number 0-1, where 0 is no sound and 1 is the loudest.
  82. */
  83. void setVolume(Number newVolume);
  84. Number getVolume();
  85. /**
  86. * Sets the pitch of this sound.
  87. * @param newPitch A Number 0-1.
  88. */
  89. void setPitch(Number newPitch);
  90. Number getPitch();
  91. /**
  92. * Returns true if the sound is playing.
  93. * @return True if sound is playing, false if otherwise.
  94. */
  95. bool isPlaying();
  96. void setIsPositional(bool isPositional);
  97. void setSoundPosition(Vector3 position);
  98. void setSoundVelocity(Vector3 velocity);
  99. void setSoundDirection(Vector3 direction);
  100. /**
  101. * Sets the current sample offset of this sound.
  102. * @param off A number 0 <= off < sound sample length
  103. */
  104. void setOffset(int off);
  105. String getFileName();
  106. Number getPlaybackDuration();
  107. Number getPlaybackTime();
  108. void seekTo(Number time);
  109. /**
  110. * Returns the current sample offset (playback progress) of this sound.
  111. * @return The sample offset if it is known, -1 otherwise.
  112. */
  113. int getOffset();
  114. /**
  115. * Returns the number of samples in the sound.
  116. * @return The sample length if it is known, -1 otherwise.
  117. */
  118. int getSampleLength();
  119. void setPositionalProperties(Number referenceDistance, Number maxDistance);
  120. void setReferenceDistance(Number referenceDistance);
  121. void setMaxDistance(Number maxDistance);
  122. Number getReferenceDistance();
  123. Number getMaxDistance();
  124. ALuint loadBytes(const char *data, int size, int channels = 1, ALsizei freq = 44100, int bps = 16, bool generateFloatBuffer = false);
  125. ALuint loadWAV(const String& fileName, bool generateFloatBuffer);
  126. ALuint loadOGG(const String& fileName, bool generateFloatBuffer);
  127. ALuint GenSource(ALuint buffer);
  128. ALuint GenSource();
  129. ALenum checkALError(const String& operation);
  130. void soundError(const String& err);
  131. void soundCheck(bool result, const String& err);
  132. static unsigned long readByte32(const unsigned char buffer[4]);
  133. static unsigned short readByte16(const unsigned char buffer[2]);
  134. void updateStream();
  135. POLYIGNORE std::vector<float> *getFloatBuffer();
  136. protected:
  137. bool updateALBuffer(ALuint buffer);
  138. Number referenceDistance;
  139. Number maxDistance;
  140. bool streamingSound;
  141. AudioStreamingSource *streamingSource;
  142. Number pitch;
  143. Number volume;
  144. String fileName;
  145. bool soundLoaded;
  146. bool isPositional;
  147. ALuint buffer; // Kept around only for deletion purposes
  148. ALuint soundSource;
  149. int sampleLength;
  150. ALuint streamingSources;
  151. ALuint streamingBuffers[STREAMING_BUFFER_COUNT];
  152. std::vector<float> floatBuffer;
  153. };
  154. }