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_
12 #include "base/basictypes.h"
13 #include "base/memory/ref_counted.h"
14 #include "content/common/content_export.h"
18 // Models a chunk derived from an AudioBuffer.
19 class CONTENT_EXPORT AudioChunk
:
20 public base::RefCountedThreadSafe
<AudioChunk
> {
22 explicit AudioChunk(int bytes_per_sample
);
23 AudioChunk(const uint8
* data
, size_t length
, int bytes_per_sample
);
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
;
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).
47 explicit AudioBuffer(int bytes_per_sample
);
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.
64 // Checks whether the buffer is empty.
68 typedef std::deque
<scoped_refptr
<AudioChunk
> > ChunksContainer
;
69 ChunksContainer chunks_
;
70 int bytes_per_sample_
;
72 DISALLOW_COPY_AND_ASSIGN(AudioBuffer
);
77 #endif // CONTENT_BROWSER_SPEECH_AUDIO_BUFFER_H_