BufferedSoundStream.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Audio/SoundStream.h"
  5. #include "../Container/ArrayPtr.h"
  6. #include "../Container/List.h"
  7. #include "../Core/Mutex.h"
  8. #include "../Container/Pair.h"
  9. namespace Urho3D
  10. {
  11. /// %Sound stream that supports manual buffering of data from the main thread.
  12. class URHO3D_API BufferedSoundStream : public SoundStream
  13. {
  14. public:
  15. /// Construct.
  16. BufferedSoundStream();
  17. /// Destruct.
  18. ~BufferedSoundStream() override;
  19. /// Produce sound data into destination. Return number of bytes produced. Called by SoundSource from the mixing thread.
  20. unsigned GetData(signed char* dest, unsigned numBytes) override;
  21. /// Buffer sound data. Makes a copy of it.
  22. void AddData(void* data, unsigned numBytes);
  23. /// Buffer sound data by taking ownership of it.
  24. void AddData(const SharedArrayPtr<signed char>& data, unsigned numBytes);
  25. /// Buffer sound data by taking ownership of it.
  26. void AddData(const SharedArrayPtr<signed short>& data, unsigned numBytes);
  27. /// Remove all buffered audio data.
  28. void Clear();
  29. /// Return amount of buffered (unplayed) sound data in bytes.
  30. unsigned GetBufferNumBytes() const;
  31. /// Return length of buffered (unplayed) sound data in seconds.
  32. float GetBufferLength() const;
  33. private:
  34. /// Buffers and their sizes.
  35. List<Pair<SharedArrayPtr<signed char>, unsigned>> buffers_;
  36. /// Byte position in the front most buffer.
  37. unsigned position_;
  38. /// Mutex for buffer data.
  39. mutable Mutex bufferMutex_;
  40. };
  41. }