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
9#include "utils/format.h"
10#include "utils/logger.h"
11#include "utils/types.h"
12
13namespace zrythm::utils::audio
14{
15
19enum class BitDepth
20{
21 BIT_DEPTH_8,
22 BIT_DEPTH_16,
23 BIT_DEPTH_24,
24 BIT_DEPTH_32
25};
26
31constexpr std::array<std::pair<int, BitDepth>, 4> bit_depth_map = {
32 std::pair{ 8, BitDepth::BIT_DEPTH_8 },
33 std::pair{ 16, BitDepth::BIT_DEPTH_16 },
34 std::pair{ 24, BitDepth::BIT_DEPTH_24 },
35 std::pair{ 32, BitDepth::BIT_DEPTH_32 }
36};
37
38constexpr int
39bit_depth_enum_to_int (BitDepth depth)
40{
41 auto it =
42 std::ranges::find (bit_depth_map, depth, &std::pair<int, BitDepth>::second);
43 z_return_val_if_fail (it != bit_depth_map.end (), -1);
44 return it->first;
45}
46
47constexpr BitDepth
48bit_depth_int_to_enum (int depth)
49{
50 auto it =
51 std::ranges::find (bit_depth_map, depth, &std::pair<int, BitDepth>::first);
52 z_return_val_if_fail (it != bit_depth_map.end (), BitDepth::BIT_DEPTH_16);
53 return it->second;
54}
55
61get_num_frames (const fs::path &filepath);
62
66bool
67frames_equal (
68 std::span<const float> src1,
69 std::span<const float> src2,
70 float epsilon);
71
78bool
79audio_files_equal (
80 const char * f1,
81 const char * f2,
82 size_t num_frames,
83 float epsilon);
84
88bool
89frames_silent (std::span<const float> src);
90
96float
97detect_bpm (
98 const float * src,
99 size_t num_frames,
100 unsigned int samplerate,
101 std::vector<float> &candidates);
102
103bool
104audio_file_is_silent (const fs::path &filepath);
105
109int
110get_num_cores ();
111
112class AudioBuffer : public juce::AudioBuffer<audio_sample_type_t>
113{
114public:
115 AudioBuffer () = default;
116 AudioBuffer (int num_channels, int num_frames_per_channel)
117 : juce::AudioBuffer<audio_sample_type_t> (num_channels, num_frames_per_channel)
118 {
119 }
120
130 static std::unique_ptr<AudioBuffer>
131 from_interleaved (const float * src, size_t num_frames, size_t num_channels);
132
138
145 void deinterleave_samples (size_t num_channels);
146
147 // algorithms (each channel is treated as a separate signal)
148
149 void invert_phase ();
150 void normalize_peak ();
151};
152
153}; // namespace zrythm::utils::audio
154
155DEFINE_ENUM_FORMATTER (
156 zrythm::utils::audio::BitDepth,
157 BitDepth,
158 QT_TR_NOOP_UTF8 ("8 bit"),
159 QT_TR_NOOP_UTF8 ("16 bit"),
160 QT_TR_NOOP_UTF8 ("24 bit"),
161 QT_TR_NOOP_UTF8 ("32 bit"));
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.
uint_fast64_t unsigned_frame_t
Unsigned type for frame index.
Definition types.h:78