LiveKit C++ Client SDK v1.1.0
Real-time audio/video/data 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
23#include "livekit/visibility.h"
24
25namespace livekit {
26
27namespace proto {
28class AudioFrameBufferInfo;
29class OwnedAudioFrameBuffer;
30} // namespace proto
31
37class LIVEKIT_API AudioFrame {
38public:
48 AudioFrame(std::vector<std::int16_t> data, int sample_rate, int num_channels, int samples_per_channel);
49 AudioFrame(); // Default constructor
50 virtual ~AudioFrame() = default;
51
53 static AudioFrame create(int sample_rate, int num_channels, int samples_per_channel);
54
56 static AudioFrame fromOwnedInfo(const proto::OwnedAudioFrameBuffer& owned);
57
58 // ---- Accessors ----
59
60 const std::vector<std::int16_t>& data() const noexcept { return data_; }
61 std::vector<std::int16_t>& data() noexcept { return data_; }
62
64 std::size_t totalSamples() const noexcept { return data_.size(); }
65
67 int sampleRate() const noexcept { return sample_rate_; }
68
70 int numChannels() const noexcept { return num_channels_; }
71
73 int samplesPerChannel() const noexcept { return samples_per_channel_; }
74
76 double duration() const noexcept;
77
79 std::string toString() const;
80
81protected:
82 // Build a proto AudioFrameBufferInfo pointing at this frame’s data.
83 // Used internally by AudioSource.
84 proto::AudioFrameBufferInfo toProto() const;
85 friend class AudioSource;
86
87private:
88 std::vector<std::int16_t> data_;
89 int sample_rate_;
90 int num_channels_;
91 int samples_per_channel_;
92};
93
94} // 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)
Construct an AudioFrame from raw PCM samples.
std::size_t totalSamples() const noexcept
Number of samples in the buffer (per all channels).
Definition audio_frame.h:64
static AudioFrame create(int sample_rate, int num_channels, int samples_per_channel)
Create a new zero-initialized AudioFrame instance.
int numChannels() const noexcept
Number of channels.
Definition audio_frame.h:70
static AudioFrame fromOwnedInfo(const proto::OwnedAudioFrameBuffer &owned)
Construct an AudioFrame by copying data out of an OwnedAudioFrameBuffer.
int samplesPerChannel() const noexcept
Samples per channel.
Definition audio_frame.h:73
double duration() const noexcept
Duration in seconds (samplesPerChannel / sampleRate).
int sampleRate() const noexcept
Sample rate in Hz.
Definition audio_frame.h:67
Represents a real-time audio source with an internal audio queue.
Definition audio_source.h:35
Public API for the LiveKit C++ Client SDK.
Definition audio_frame.h:25