PolySound.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "polycode/core/PolyGlobals.h"
  21. #include "polycode/core/PolyVector3.h"
  22. #include "polycode/core/PolyString.h"
  23. #include "polycode/core/PolyCoreServices.h"
  24. #define BUFFER_SIZE 32768
  25. #define STREAMING_BUFFER_SIZE
  26. namespace Polycode {
  27. enum SoundFormat {SoundFormatUnsupported, SoundFormat8, SoundFormat16, SoundFormat32};
  28. class String;
  29. class AudioStreamingSource {
  30. public:
  31. AudioStreamingSource(unsigned int channels, unsigned int freq);
  32. POLYIGNORE virtual unsigned int streamData(int16_t *buffer, unsigned int size);
  33. unsigned int getNumChannels();
  34. unsigned int getFrequency();
  35. protected:
  36. unsigned int channels;
  37. unsigned int freq;
  38. };
  39. /**
  40. * Loads and plays a sound. This class can load and play an OGG or WAV sound file.
  41. */
  42. class _PolyExport Sound : public PolyBase {
  43. public:
  44. /**
  45. * Constructor.
  46. * @param fileName Path to an OGG or WAV file to load.
  47. */
  48. Sound(const String& fileName);
  49. Sound(int size, const char *data, int channels, unsigned int freq, SoundFormat format);
  50. Sound(AudioStreamingSource *streamingSource);
  51. Number getSampleAsNumber(unsigned int offset, unsigned int channel);
  52. virtual ~Sound();
  53. void loadFile(String fileName);
  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, bool restartSound=true);
  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. bool isLooped();
  81. void setIsPositional(bool isPositional);
  82. void setSoundPosition(Vector3 position);
  83. void setSoundVelocity(Vector3 velocity);
  84. void setSoundDirection(Vector3 direction);
  85. /**
  86. * Sets the current sample offset of this sound.
  87. * @param off A number 0 <= off < sound sample length
  88. */
  89. void setOffset(unsigned int offset);
  90. String getFileName();
  91. Number getPlaybackDuration();
  92. Number getPlaybackTime();
  93. void seekTo(Number time);
  94. /**
  95. * Returns the current sample offset (playback progress) of this sound.
  96. * @return The sample offset if it is known, -1 otherwise.
  97. */
  98. int getOffset();
  99. /**
  100. * Returns the number of samples in the sound.
  101. * @return The sample length if it is known, -1 otherwise.
  102. */
  103. int getSampleLength();
  104. unsigned int getFrequency();
  105. void setPositionalProperties(Number referenceDistance, Number maxDistance);
  106. void setReferenceDistance(Number referenceDistance);
  107. void setMaxDistance(Number maxDistance);
  108. Number getReferenceDistance();
  109. Number getMaxDistance();
  110. bool loadBytes(const char *data, int size, int channels, unsigned int freq, SoundFormat format);
  111. bool loadWAV(const String& fileName);
  112. bool loadOGG(const String& fileName);
  113. void soundCheck(bool result, const String& err);
  114. static unsigned long readByte32(const unsigned char buffer[4]);
  115. static unsigned short readByte16(const unsigned char buffer[2]);
  116. void updateStream(unsigned int streamCount);
  117. protected:
  118. Number referenceDistance;
  119. Number maxDistance;
  120. bool streamingSound;
  121. AudioStreamingSource *streamingSource;
  122. Number pitch;
  123. Number frequencyAdjust;
  124. Number volume;
  125. String fileName;
  126. bool soundLoaded;
  127. bool playing;
  128. bool looped;
  129. bool isPositional;
  130. unsigned int numSamples;
  131. unsigned int numChannels;
  132. unsigned int playbackOffset;
  133. unsigned int frequency;
  134. int16_t *soundBuffer;
  135. };
  136. }