Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
audio.h
1// SPDX-FileCopyrightText: © 2019-2024 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include <algorithm>
7#include <array>
8#include <cassert>
9#include <filesystem>
10#include <span>
11
12#include <juce_audio_basics/juce_audio_basics.h>
13
14namespace zrythm::utils::audio
15{
16
20enum class BitDepth
21{
22 BIT_DEPTH_8,
23 BIT_DEPTH_16,
24 BIT_DEPTH_24,
25 BIT_DEPTH_32
26};
27
32constexpr std::array<std::pair<int, BitDepth>, 4> bit_depth_map = {
33 std::pair{ 8, BitDepth::BIT_DEPTH_8 },
34 std::pair{ 16, BitDepth::BIT_DEPTH_16 },
35 std::pair{ 24, BitDepth::BIT_DEPTH_24 },
36 std::pair{ 32, BitDepth::BIT_DEPTH_32 }
37};
38
39constexpr int
40bit_depth_enum_to_int (BitDepth depth)
41{
42 auto it =
43 std::ranges::find (bit_depth_map, depth, &std::pair<int, BitDepth>::second);
44 assert (it != bit_depth_map.end ());
45 return it->first;
46}
47
48constexpr BitDepth
49bit_depth_int_to_enum (int depth)
50{
51 auto it =
52 std::ranges::find (bit_depth_map, depth, &std::pair<int, BitDepth>::first);
53 assert (it != bit_depth_map.end ());
54 return it->second;
55}
56
61uint32_t
62get_num_frames (const std::filesystem::path &filepath);
63
67bool
68frames_equal (
69 std::span<const float> src1,
70 std::span<const float> src2,
71 float epsilon);
72
79bool
80audio_files_equal (
81 const char * f1,
82 const char * f2,
83 size_t num_frames,
84 float epsilon);
85
89bool
90frames_silent (std::span<const float> src);
91
97float
98detect_bpm (
99 const float * src,
100 size_t num_frames,
101 unsigned int samplerate,
102 std::vector<float> &candidates);
103
104bool
105audio_file_is_silent (const std::filesystem::path &filepath);
106
110int
111get_num_cores ();
112
113class AudioBuffer : public juce::AudioSampleBuffer
114{
115public:
116 AudioBuffer () = default;
117 AudioBuffer (int num_channels, int num_frames_per_channel)
118 : juce::AudioSampleBuffer (num_channels, num_frames_per_channel)
119 {
120 }
121
131 static std::unique_ptr<AudioBuffer>
132 from_interleaved (const float * src, size_t num_frames, size_t num_channels);
133
139
146 void deinterleave_samples (size_t num_channels);
147
148 // algorithms (each channel is treated as a separate signal)
149
150 void invert_phase ();
151 void normalize_peak ();
152};
153
157bool
158buffer_has_audio (
159 const juce::AudioSampleBuffer &buffer,
160 size_t offset,
161 size_t num_frames,
162 float threshold = 1e-6f);
163
164}; // namespace zrythm::utils::audio
165
166// TODO
167#if 0
168DEFINE_ENUM_FORMATTER (
169 zrythm::utils::audio::BitDepth,
170 BitDepth,
171 QT_TR_NOOP_UTF8 ("8 bit"),
172 QT_TR_NOOP_UTF8 ("16 bit"),
173 QT_TR_NOOP_UTF8 ("24 bit"),
174 QT_TR_NOOP_UTF8 ("32 bit"));
175#endif
void deinterleave_samples(size_t num_channels)
De-interleaves the samples in this buffer from interleaved format to non-interleaved (planar) format.
static std::unique_ptr< AudioBuffer > from_interleaved(const float *src, size_t num_frames, size_t num_channels)
Creates an AudioBuffer from interleaved audio data.
void interleave_samples()
Interleaves the samples in this buffer from non-interleaved (planar) format to interleaved format.