Introduced SpeechRecognitionDispatcher(Host) classes, handling dispatch of IPC messag...
[chromium-blink-merge.git] / content / browser / speech / audio_buffer.h
blobfffba8c3fc90f846cfb8e6cf81b1681bd9964170
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_
7 #pragma once
9 #include <deque>
10 #include <string>
12 #include "base/basictypes.h"
13 #include "base/memory/ref_counted.h"
14 #include "content/common/content_export.h"
16 namespace speech {
18 // Models a chunk derived from an AudioBuffer.
19 class CONTENT_EXPORT AudioChunk :
20 public base::RefCountedThreadSafe<AudioChunk> {
21 public:
22 explicit AudioChunk(int bytes_per_sample);
23 AudioChunk(const uint8* data, size_t length, int bytes_per_sample);
25 bool IsEmpty() const;
26 int bytes_per_sample() const { return bytes_per_sample_; }
27 size_t NumSamples() const;
28 const std::string& AsString() const;
29 int16 GetSample16(size_t index) const;
30 const int16* SamplesData16() const;
31 friend class AudioBuffer;
33 private:
34 ~AudioChunk() {}
35 friend class base::RefCountedThreadSafe<AudioChunk>;
37 std::string data_string_;
38 int bytes_per_sample_;
40 DISALLOW_COPY_AND_ASSIGN(AudioChunk);
43 // Models an audio buffer. The current implementation relies on on-demand
44 // allocations of AudioChunk(s) (which uses a string as storage).
45 class AudioBuffer {
46 public:
47 explicit AudioBuffer(int bytes_per_sample);
48 ~AudioBuffer();
50 // Enqueues a copy of |length| bytes of |data| buffer.
51 void Enqueue(const uint8* data, size_t length);
53 // Dequeues, in FIFO order, a single chunk respecting the length of the
54 // corresponding Enqueue call (in a nutshell: multiple Enqueue calls followed
55 // by Dequeue calls will return the individual chunks without merging them).
56 scoped_refptr<AudioChunk> DequeueSingleChunk();
58 // Dequeues all previously enqueued chunks, merging them in a single chunk.
59 scoped_refptr<AudioChunk> DequeueAll();
61 // Removes and frees all the enqueued chunks.
62 void Clear();
64 // Checks whether the buffer is empty.
65 bool IsEmpty() const;
67 private:
68 typedef std::deque<scoped_refptr<AudioChunk> > ChunksContainer;
69 ChunksContainer chunks_;
70 int bytes_per_sample_;
72 DISALLOW_COPY_AND_ASSIGN(AudioBuffer);
75 } // namespace speech
77 #endif // CONTENT_BROWSER_SPEECH_AUDIO_BUFFER_H_