RavEngine
Loading...
Searching...
No Matches
vorbis_stream_encoder.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_VORBIS_STREAM_ENCODER_H_
18#define RESONANCE_AUDIO_UTILS_VORBIS_STREAM_ENCODER_H_
19
20#include <fstream>
21#include <iostream>
22
23#include "base/integral_types.h"
24
25#include "ogg/ogg.h"
26#include "vorbis/codec.h"
27
28namespace vraudio {
29
31 public:
32 // Supported encoding modes.
33 enum class EncodingMode {
34 kUndefined,
35 // Variable bit rate mode (VBR).
36 kVariableBitRate,
37 // Average bit rate mode (ABR).
38 kAverageBitRate,
39 };
40
42
43 // Initializes Vorbis encoding.
44 //
45 // @param filename Ogg vorbis output file. If it doesn't exist, it will be
46 // created. Existing files will be overwritten.
47 // @param num_channels Number of input channels.
48 // @param sample_rate Sample rate of input audio buffers.
49 // @param encoding_mode Selects variable (VBR) or average encoding (ABR).
50 // @param bitrate Target bitrate (only used when selecting ABR encoding).
51 // @param quality Target quality (only used when selecting VBR encoding). The
52 // usable range is -.1 (lowest quality, smallest file) to 1. (highest
53 // quality, largest file).
54 // @return False in case of file I/O errors or libvorbis initialization
55 // failures like non-supported channel/sample rate configuration.
56 bool InitializeForFile(const std::string& filename, size_t num_channels,
57 int sample_rate, EncodingMode encoding_mode,
58 int bitrate, float quality);
59
60 // Feeds input audio data into libvorbis encoder and triggers encoding.
61 //
62 // @param Array of pointers to planar channel data.
63 // @param num_channels Number of input channels.
64 // @param num_frames Number of input frames.
65 // @return False in case of file I/O errors or missing encoder initialization.
66 bool AddPlanarBuffer(const float* const* input_ptrs, size_t num_channels,
67 size_t num_frames);
68
69 // Flushes the remaining audio buffers and closes the output file.
70 //
71 // @return False in case of file I/O errors or missing encoder initialization.
72 bool FlushAndClose();
73
74 private:
75 // Copies input audio data into libvorbis encoder buffer.
76 //
77 // @param Array of pointers to planar channel data.
78 // @param num_channels Number of input channels.
79 // @param num_frames Number of input frames.
80 void PrepareVorbisBuffer(const float* const* input_ptrs, size_t num_channels,
81 size_t num_frames);
82
83 // Performs encoding of audio data prepared via |PrepareVorbisBuffer| or when
84 // the end of stream has been signaled.
85 //
86 // @return False in case of file I/O errors or missing encoder initialization.
87 bool PerformEncoding();
88
89 // Dumps data from |ogg_page_| struct to |output_file_stream_|.
90 //
91 // @return False in case of file I/O errors or missing encoder initialization.
92 bool WriteOggPage();
93
94 // Flag indicating if encoder has been successfully initialized.
95 bool init_;
96
97 // Output file stream.
98 std::ofstream output_file_stream_;
99
100 // libogg structs.
101 ogg_stream_state stream_state_;
102 ogg_page ogg_page_;
103 ogg_packet ogg_packet_;
104
105 // libvorbis structs.
106 vorbis_info vorbis_info_;
107 vorbis_comment vorbis_comment_;
108 vorbis_dsp_state vorbis_state_;
109 vorbis_block vorbis_block_;
110};
111
112} // namespace vraudio
113
114#endif // RESONANCE_AUDIO_UTILS_VORBIS_STREAM_ENCODER_H_
Definition vorbis_stream_encoder.h:30