summaryrefslogtreecommitdiffstats
path: root/variablespeed/jni/ring_buffer.h
blob: 8afe43658e454dc14dc52a03c2337d56f71ec323 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef FRAMEWORKS_EX_VARIABLESPEED_JNI_RING_BUFFER_H_
#define FRAMEWORKS_EX_VARIABLESPEED_JNI_RING_BUFFER_H_

#include <vector>

#include "integral_types.h"
#include "macros.h"

// Circular buffer of multichannel audio, maintaining the position of the
// writing head and several reading head, and providing a method for
// accessing a contiguous block (through direct reference or the copy in a
// temporary read buffer in case the requested block crosses the buffer
// boundaries).
//
// This code is not thread-safe.

namespace video_editing {

class RingBuffer {
 public:
  RingBuffer();
  virtual ~RingBuffer();

  // Initializes a RingBuffer.
  // @param size: size of the buffer in frames.
  // @param num_channels: number of channels of the original audio.
  // @param num_readers: number of reading heads.
  void Init(int size, int num_channels, int num_readers);

  // Gets the position of a reading head.
  // @param reader reading head index.
  // @returns position pointed to by the #reader reading head.
  int64 Tell(int reader) const;

  // Moves a reading head.
  // @param reader reading head index.
  // @param position target position.
  void Seek(int reader, int64 position);

  // Reads samples for a reading head.
  // @param reader reading head index.
  // @param num_frames number of frames to read.
  // @param destination float buffer to which the samples will be written.
  void Copy(int reader, float* destination, int num_frames) const;

  // Writes samples.
  // @param samples float buffer containing the samples.
  // @param num_frames number of frames to write.
  void Write(const float* samples, int num_frames);

  // Flushes the content of the buffer and reset the position of the heads.
  void Reset();

  // Returns the number of frames we can still write.
  int overhead() const;

  // Returns the number of frames we can read for a given reader.
  // @param reader reading head index.
  int available(int reader) const;

  // Returns a pointer to num_frames x num_channels contiguous samples for
  // a given reader. In most cases this directly returns a pointer to the
  // data in the ring buffer. However, if the required block wraps around the
  // boundaries of the ring buffer, the data is copied to a temporary buffer
  // owned by the RingBuffer object.
  // @param reader reading head index.
  // @param num_frames number of frames to read.
  // @returns pointer to a continuous buffer containing num_frames.
  float* GetPointer(int reader, int num_frames);

  // Merges updated data back into the ring buffer, if it was updated in
  // the temporary buffer.  This operation follows a GetPointer() that
  // was used to obtain data to rewrite.  The buffer address supplied
  // here must match the one returned by GetPointer().
  // @param reader reading head index.
  // @param source pointer to a continuous buffer containing num_frames.
  // @param num_frames number of frames to copy back to the ring buffer.
  void MergeBack(int reader, const float* source, int num_frames);

 private:
  // Returns the position of the laziest reader.
  int64 GetTail() const;

  bool initialized_;
  float* samples_;
  std::vector<int64> readers_;
  int size_;
  int num_channels_;
  int num_readers_;
  int64 head_logical_;
  int head_;

  float* temp_read_buffer_;
  int temp_read_buffer_size_;

  DISALLOW_COPY_AND_ASSIGN(RingBuffer);
};

}  // namespace video_editing

#endif  // FRAMEWORKS_EX_VARIABLESPEED_JNI_RING_BUFFER_H_