BsOggVorbisEncoder.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "vorbis\vorbisenc.h"
  6. namespace bs
  7. {
  8. /** @addtogroup OpenAudio
  9. * @{
  10. */
  11. /** Used for encoding PCM to Ogg Vorbis audio data. */
  12. class OggVorbisEncoder
  13. {
  14. public:
  15. OggVorbisEncoder();
  16. ~OggVorbisEncoder();
  17. /**
  18. * Sets up the writer. Should be called before calling write().
  19. *
  20. * @param[in] writeCallback Callback that will be triggered when the writer is ready to output some data.
  21. * The callback should copy the provided data into its own buffer.
  22. * @param[in] sampleRate Determines how many samples per second the written data will have.
  23. * @param[in] bitDepth Determines the size of a single sample, in bits.
  24. * @param[in] numChannels Determines the number of audio channels. Channel data will be output interleaved
  25. * in the output buffer.
  26. */
  27. bool open(std::function<void(UINT8*, UINT32)> writeCallback, UINT32 sampleRate, UINT32 bitDepth, UINT32 numChannels);
  28. /**
  29. * Writes a new set of samples and converts them to Ogg Vorbis.
  30. *
  31. * @param[in] samples Samples in PCM format. 8-bit samples should be unsigned, but higher bit depths signed.
  32. * Each sample is assumed to be the bit depth that was provided to the open() method.
  33. * @param[in] numSamples Number of samples to encode.
  34. */
  35. void write(UINT8* samples, UINT32 numSamples);
  36. /**
  37. * Flushes the last of the data into the write buffer (triggers the write callback). This is called automatically
  38. * when the writer is closed or goes out of scope.
  39. */
  40. void flush();
  41. /**
  42. * Closes the encoder and flushes the last of the data into the write buffer (triggers the write callback). This is
  43. * called automatically when the writer goes out of scope.
  44. */
  45. void close();
  46. /**
  47. * Helper method that allows you to quickly convert PCM to Ogg Vorbis data.
  48. *
  49. * @param[in] samples Buffer containing samples in PCM format. All samples should be in signed integer format.
  50. * @param[in] info Meta-data describing the provided samples.
  51. * @param[out] size Number of bytes written to the output buffer.
  52. * @return Buffer containing the encoded samples, allocated using the general allocator.
  53. */
  54. static UINT8* PCMToOggVorbis(UINT8* samples, const AudioDataInfo& info, UINT32& size);
  55. private:
  56. /** Writes Vorbis blocks into Ogg packets. */
  57. void writeBlocks();
  58. static const UINT32 BUFFER_SIZE = 4096;
  59. std::function<void(UINT8*, UINT32)> mWriteCallback;
  60. UINT8 mBuffer[BUFFER_SIZE];
  61. UINT32 mBufferOffset;
  62. UINT32 mNumChannels;
  63. UINT32 mBitDepth;
  64. bool mClosed;
  65. ogg_stream_state mOggState;
  66. vorbis_info mVorbisInfo;
  67. vorbis_dsp_state mVorbisState;
  68. vorbis_block mVorbisBlock;
  69. };
  70. /** @} */
  71. }