- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / speech / audio_buffer.h
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.
4
5 #ifndef CONTENT_BROWSER_SPEECH_AUDIO_BUFFER_H_
6 #define CONTENT_BROWSER_SPEECH_AUDIO_BUFFER_H_
7
8 #include <deque>
9 #include <string>
10
11 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h"
13 #include "content/common/content_export.h"
14
15 namespace content {
16
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);
23
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;
31
32  private:
33   ~AudioChunk() {}
34   friend class base::RefCountedThreadSafe<AudioChunk>;
35
36   std::string data_string_;
37   int bytes_per_sample_;
38
39   DISALLOW_COPY_AND_ASSIGN(AudioChunk);
40 };
41
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();
48
49   // Enqueues a copy of |length| bytes of |data| buffer.
50   void Enqueue(const uint8* data, size_t length);
51
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();
56
57   // Dequeues all previously enqueued chunks, merging them in a single chunk.
58   scoped_refptr<AudioChunk> DequeueAll();
59
60   // Removes and frees all the enqueued chunks.
61   void Clear();
62
63   // Checks whether the buffer is empty.
64   bool IsEmpty() const;
65
66  private:
67   typedef std::deque<scoped_refptr<AudioChunk> > ChunksContainer;
68   ChunksContainer chunks_;
69   int bytes_per_sample_;
70
71   DISALLOW_COPY_AND_ASSIGN(AudioBuffer);
72 };
73
74 }  // namespace content
75
76 #endif  // CONTENT_BROWSER_SPEECH_AUDIO_BUFFER_H_