RavEngine
Loading...
Searching...
No Matches
ogg_vorbis_recorder.h
1/*
2Copyright 2018 Google Inc. All Rights Reserved.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS-IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef RESONANCE_AUDIO_UTILS_OGG_VORBIS_RECORDER_H_
18#define RESONANCE_AUDIO_UTILS_OGG_VORBIS_RECORDER_H_
19
20#include <memory>
21#include <vector>
22
23#include "base/audio_buffer.h"
24#include "utils/buffer_crossfader.h"
25#include "utils/vorbis_stream_encoder.h"
26
27namespace vraudio {
28
29// Class that takes audio buffers as input and writes them into a compressed OGG
30// Vorbis file.
31
33 public:
34 // Constructs a new recorder with given sampling rate and number of channels.
35
36 OggVorbisRecorder(int sample_rate_hz, size_t num_channels, size_t num_frames,
37 size_t max_num_buffers);
38
39 // Adds next input buffer at the end of the record data.
40 //
41 // @param input_buffer Next audio buffer to be recorded.
42 void AddInput(std::unique_ptr<AudioBuffer> input_buffer);
43
44 // Flushes the record data.
45 void Reset();
46
47 // Writes the current record data into a file and resets the recorder.
48 //
49 // @param file_path Full path of the file to be recorded into.
50 // @param quality Compression quality of the record. The usable range is from
51 // -0.1 (lowest quality, smallest file) to 1.0 (highest quality, largest
52 // file).
53 // @param seamless True to record seamlessly for looping. Note that this
54 // option will truncate the record length by |num_frames_| samples.
55 // @return False if fails to successfully write data into |file_path|.
56 bool WriteToFile(const std::string& file_path, float quality, bool seamless);
57
58 private:
59 // Helper method to make record data seamless.
60 void MakeSeamless();
61
62 // Record sampling rate.
63 const int sample_rate_hz_;
64
65 // Record number of channels.
66 const size_t num_channels_;
67
68 // Record number of frames per buffer.
69 const size_t num_frames_;
70
71 // Maximum number of input buffers allowed to record.
72 const size_t max_num_buffers_;
73
74 // Record data that is stored as a list of planar audio buffers.
75 std::vector<std::unique_ptr<AudioBuffer>> data_;
76
77 // Temporary vector to extract pointers to planar channels in an
78 // |AudioBuffer|.
79 std::vector<const float*> temp_data_channel_ptrs_;
80
81 // Buffer crossfader that is used to create seamless loop.
82 BufferCrossfader crossfader_;
83
84 // Temporary buffer to store the crossfaded output.
85
86 AudioBuffer crossfade_buffer_;
87
88 // OGG Vorbis encoder to write record data into file in compressed format.
89 VorbisStreamEncoder encoder_;
90};
91
92} // namespace vraudio
93
94#endif // RESONANCE_AUDIO_UTILS_OGG_VORBIS_RECORDER_H_
Definition audio_buffer.h:78
Definition buffer_crossfader.h:26
Definition ogg_vorbis_recorder.h:32
Definition vorbis_stream_encoder.h:30