LiveKit C++ SDK
Real-time audio/video SDK for C++
Loading...
Searching...
No Matches
audio_frame.h
1/*
2 * Copyright 2025 LiveKit
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an “AS IS” BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <cstdint>
20#include <string>
21#include <vector>
22
23namespace livekit {
24
25namespace proto {
26class AudioFrameBufferInfo;
27class OwnedAudioFrameBuffer;
28} // namespace proto
29
38public:
50 AudioFrame(std::vector<std::int16_t> data, int sample_rate, int num_channels,
52 AudioFrame(); // Default constructor
53 virtual ~AudioFrame() = default;
54
60
64 static AudioFrame fromOwnedInfo(const proto::OwnedAudioFrameBuffer &owned);
65
66 // ---- Accessors ----
67
68 const std::vector<std::int16_t> &data() const noexcept { return data_; }
69 std::vector<std::int16_t> &data() noexcept { return data_; }
70
72 std::size_t total_samples() const noexcept { return data_.size(); }
73
75 int sample_rate() const noexcept { return sample_rate_; }
76
78 int num_channels() const noexcept { return num_channels_; }
79
81 int samples_per_channel() const noexcept { return samples_per_channel_; }
82
84 double duration() const noexcept;
85
87 std::string to_string() const;
88
89protected:
90 // Build a proto AudioFrameBufferInfo pointing at this frame’s data.
91 // Used internally by AudioSource.
92 proto::AudioFrameBufferInfo toProto() const;
93 friend class AudioSource;
94
95private:
96 std::vector<std::int16_t> data_;
97 int sample_rate_;
98 int num_channels_;
99 int samples_per_channel_;
100};
101
102} // namespace livekit
Represents a raw PCM audio frame with interleaved int16 samples.
Definition audio_frame.h:37
AudioFrame(std::vector< std::int16_t > data, int sample_rate, int num_channels, int samples_per_channel)
int num_channels() const noexcept
Number of channels.
Definition audio_frame.h:78
static AudioFrame create(int sample_rate, int num_channels, int samples_per_channel)
std::size_t total_samples() const noexcept
Number of samples in the buffer (per all channels).
Definition audio_frame.h:72
static AudioFrame fromOwnedInfo(const proto::OwnedAudioFrameBuffer &owned)
double duration() const noexcept
Duration in seconds (samples_per_channel / sample_rate).
int samples_per_channel() const noexcept
Samples per channel.
Definition audio_frame.h:81
int sample_rate() const noexcept
Sample rate in Hz.
Definition audio_frame.h:75
std::string to_string() const
A human-readable description.
Definition audio_source.h:36