[Session restore] Rename group name Enabled to Restore.
[chromium-blink-merge.git] / content / browser / speech / audio_buffer.h
blob7b0fd7e19df77a4fffed5b276c018e1ebe69d7af
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.
5 #ifndef CONTENT_BROWSER_SPEECH_AUDIO_BUFFER_H_
6 #define CONTENT_BROWSER_SPEECH_AUDIO_BUFFER_H_
8 #include <deque>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h"
13 #include "content/common/content_export.h"
15 namespace content {
17 // Models a chunk derived from an AudioBuffer.
18 class CONTENT_EXPORT AudioChunk :
19 public base::RefCountedThreadSafe<AudioChunk> {
20 public:
21 explicit AudioChunk(int bytes_per_sample);
22 // Creates a chunk of |length| bytes, initialized to zeros.
23 AudioChunk(size_t length, int bytes_per_sample);
24 AudioChunk(const uint8* data, size_t length, int bytes_per_sample);
26 bool IsEmpty() const;
27 int bytes_per_sample() const { return bytes_per_sample_; }
28 size_t NumSamples() const;
29 const std::string& AsString() const;
30 int16 GetSample16(size_t index) const;
31 const int16* SamplesData16() const;
32 uint8* writable_data() { return reinterpret_cast<uint8*>(&data_string_[0]); }
34 private:
35 friend class base::RefCountedThreadSafe<AudioChunk>;
36 ~AudioChunk() {}
38 std::string data_string_;
39 const int bytes_per_sample_;
41 DISALLOW_COPY_AND_ASSIGN(AudioChunk);
44 // Models an audio buffer. The current implementation relies on on-demand
45 // allocations of AudioChunk(s) (which uses a string as storage).
46 class AudioBuffer {
47 public:
48 explicit AudioBuffer(int bytes_per_sample);
49 ~AudioBuffer();
51 // Enqueues a copy of |length| bytes of |data| buffer.
52 void Enqueue(const uint8* data, size_t length);
54 // Dequeues, in FIFO order, a single chunk respecting the length of the
55 // corresponding Enqueue call (in a nutshell: multiple Enqueue calls followed
56 // by DequeueSingleChunk calls will return the individual chunks without
57 // merging them).
58 scoped_refptr<AudioChunk> DequeueSingleChunk();
60 // Dequeues all previously enqueued chunks, merging them in a single chunk.
61 scoped_refptr<AudioChunk> DequeueAll();
63 // Removes and frees all the enqueued chunks.
64 void Clear();
66 // Checks whether the buffer is empty.
67 bool IsEmpty() const;
69 private:
70 typedef std::deque<scoped_refptr<AudioChunk> > ChunksContainer;
71 ChunksContainer chunks_;
72 const int bytes_per_sample_;
74 DISALLOW_COPY_AND_ASSIGN(AudioBuffer);
77 } // namespace content
79 #endif // CONTENT_BROWSER_SPEECH_AUDIO_BUFFER_H_