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