Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CONTENT_BROWSER_SPEECH_AUDIO_BUFFER_H_ |
| 6 | #define CONTENT_BROWSER_SPEECH_AUDIO_BUFFER_H_ |
| 7 | |
| 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
| 11 | #include <string> |
| 12 | |
| 13 | #include "base/containers/circular_deque.h" |
| 14 | #include "base/macros.h" |
| 15 | #include "base/memory/ref_counted.h" |
| 16 | #include "content/common/content_export.h" |
| 17 | |
| 18 | namespace content { |
| 19 | |
| 20 | // Models a chunk derived from an AudioBuffer. |
| 21 | class CONTENT_EXPORT AudioChunk : |
| 22 | public base::RefCountedThreadSafe<AudioChunk> { |
| 23 | public: |
| 24 | explicit AudioChunk(int bytes_per_sample); |
| 25 | // Creates a chunk of |length| bytes, initialized to zeros. |
| 26 | AudioChunk(size_t length, int bytes_per_sample); |
| 27 | AudioChunk(const uint8_t* data, size_t length, int bytes_per_sample); |
| 28 | |
| 29 | bool IsEmpty() const; |
| 30 | int bytes_per_sample() const { return bytes_per_sample_; } |
| 31 | size_t NumSamples() const; |
| 32 | const std::string& AsString() const; |
| 33 | int16_t GetSample16(size_t index) const; |
| 34 | const int16_t* SamplesData16() const; |
| 35 | uint8_t* writable_data() { |
| 36 | return reinterpret_cast<uint8_t*>(&data_string_[0]); |
| 37 | } |
| 38 | |
| 39 | private: |
| 40 | friend class base::RefCountedThreadSafe<AudioChunk>; |
| 41 | ~AudioChunk() {} |
| 42 | |
| 43 | std::string data_string_; |
| 44 | const int bytes_per_sample_; |
| 45 | |
| 46 | DISALLOW_COPY_AND_ASSIGN(AudioChunk); |
| 47 | }; |
| 48 | |
| 49 | // Models an audio buffer. The current implementation relies on on-demand |
| 50 | // allocations of AudioChunk(s) (which uses a string as storage). |
| 51 | class AudioBuffer { |
| 52 | public: |
| 53 | explicit AudioBuffer(int bytes_per_sample); |
| 54 | ~AudioBuffer(); |
| 55 | |
| 56 | // Enqueues a copy of |length| bytes of |data| buffer. |
| 57 | void Enqueue(const uint8_t* data, size_t length); |
| 58 | |
| 59 | // Dequeues, in FIFO order, a single chunk respecting the length of the |
| 60 | // corresponding Enqueue call (in a nutshell: multiple Enqueue calls followed |
| 61 | // by DequeueSingleChunk calls will return the individual chunks without |
| 62 | // merging them). |
| 63 | scoped_refptr<AudioChunk> DequeueSingleChunk(); |
| 64 | |
| 65 | // Dequeues all previously enqueued chunks, merging them in a single chunk. |
| 66 | scoped_refptr<AudioChunk> DequeueAll(); |
| 67 | |
| 68 | // Removes and frees all the enqueued chunks. |
| 69 | void Clear(); |
| 70 | |
| 71 | // Checks whether the buffer is empty. |
| 72 | bool IsEmpty() const; |
| 73 | |
| 74 | private: |
| 75 | using ChunksContainer = base::circular_deque<scoped_refptr<AudioChunk>>; |
| 76 | ChunksContainer chunks_; |
| 77 | const int bytes_per_sample_; |
| 78 | |
| 79 | DISALLOW_COPY_AND_ASSIGN(AudioBuffer); |
| 80 | }; |
| 81 | |
| 82 | } // namespace content |
| 83 | |
| 84 | #endif // CONTENT_BROWSER_SPEECH_AUDIO_BUFFER_H_ |