Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / content / browser / speech / audio_buffer.h
blob783ae6679f38825c206cc38946b684fc6c2482ad
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 AudioChunk(const uint8* data, size_t length, int bytes_per_sample);
24 bool IsEmpty() const;
25 int bytes_per_sample() const { return bytes_per_sample_; }
26 size_t NumSamples() const;
27 const std::string& AsString() const;
28 int16 GetSample16(size_t index) const;
29 const int16* SamplesData16() const;
30 friend class AudioBuffer;
32 private:
33 ~AudioChunk() {}
34 friend class base::RefCountedThreadSafe<AudioChunk>;
36 std::string data_string_;
37 int bytes_per_sample_;
39 DISALLOW_COPY_AND_ASSIGN(AudioChunk);
42 // Models an audio buffer. The current implementation relies on on-demand
43 // allocations of AudioChunk(s) (which uses a string as storage).
44 class AudioBuffer {
45 public:
46 explicit AudioBuffer(int bytes_per_sample);
47 ~AudioBuffer();
49 // Enqueues a copy of |length| bytes of |data| buffer.
50 void Enqueue(const uint8* data, size_t length);
52 // Dequeues, in FIFO order, a single chunk respecting the length of the
53 // corresponding Enqueue call (in a nutshell: multiple Enqueue calls followed
54 // by Dequeue calls will return the individual chunks without merging them).
55 scoped_refptr<AudioChunk> DequeueSingleChunk();
57 // Dequeues all previously enqueued chunks, merging them in a single chunk.
58 scoped_refptr<AudioChunk> DequeueAll();
60 // Removes and frees all the enqueued chunks.
61 void Clear();
63 // Checks whether the buffer is empty.
64 bool IsEmpty() const;
66 private:
67 typedef std::deque<scoped_refptr<AudioChunk> > ChunksContainer;
68 ChunksContainer chunks_;
69 int bytes_per_sample_;
71 DISALLOW_COPY_AND_ASSIGN(AudioBuffer);
74 } // namespace content
76 #endif // CONTENT_BROWSER_SPEECH_AUDIO_BUFFER_H_