EchoMap 2026-07-25 6d3977c
An experimental cross-platform digital signal processing application for sound-source localisation.
Loading...
Searching...
No Matches
Signal.hpp
Go to the documentation of this file.
1
7
8#ifndef ECHOMAP_SIGNAL_HPP
9#define ECHOMAP_SIGNAL_HPP
10
11#include <cstdint>
12#include <filesystem>
13#include <ranges>
14#include <vector>
15
16#include "Object.hpp"
17
18namespace echomap
19{
20
39class Signal : public Object<Signal>
40{
41public:
45 struct Sample
46 {
47 using TimeT = float;
48 using AmplitudeT = float;
49
52 };
53
54private:
56
57public:
61 struct Source
62 {
65 bool dirty = false;
66 bool is_loaded = false;
67
68 [[nodiscard]] bool operator<(const Source& other) const;
69 };
70
75
81 [[nodiscard]] std::uint64_t get_sample_count() const noexcept;
82
99 [[nodiscard]] const std::optional<Source>& observe_source() const noexcept;
100
101 [[nodiscard]] Sample::TimeT get_time_offset() const noexcept;
102 [[nodiscard]] std::size_t get_sample_rate() const noexcept;
103
104 [[nodiscard]] decltype(samples)::const_iterator begin() const;
105 [[nodiscard]] decltype(samples)::const_iterator end() const;
106 [[nodiscard]] decltype(samples)::const_iterator cbegin() const noexcept;
107 [[nodiscard]] decltype(samples)::const_iterator cend() const noexcept;
108
109 [[nodiscard]] auto timed_samples() const
110 {
111 return std::views::iota(std::size_t{0}, samples.size()) |
112 std::views::transform([this](const std::size_t index) -> Sample {
113 return {.time = get_time_at_index(index), .amplitude = samples[index]};
114 });
115 }
116
117 [[nodiscard]] Sample::AmplitudeT operator[](std::size_t index) const noexcept;
118 [[nodiscard]] std::span<const Sample::AmplitudeT> amplitudes() const noexcept;
119
120 [[nodiscard]] bool is_uniformly_sampled() const noexcept;
121
130 [[nodiscard]] Sample::TimeT get_time_at_index(std::size_t index) const noexcept;
131
132 Signal(const Signal& old_signal);
133 Signal(const Signal& old_signal,
134 std::string_view new_name);
135
136private:
146
147 friend class SignalFactory;
148
155 explicit Signal(
157 const std::optional<Source>& source = {}
158 );
159
170 void emplace_sample(Sample::AmplitudeT amplitude);
171
181 void emplace_sample(
182 Sample::TimeT time,
183 Sample::AmplitudeT amplitude
184 );
185
194 void emplace_sample(const Sample& sample);
195
207
218 Sample::TimeT time,
219 Sample::AmplitudeT amplitude
220 );
221
230 void emplace_sample_from_source(const Sample& sample);
231
237 void reserve_samples(std::size_t count);
238
239 void set_source(
240 const std::filesystem::path& path,
241 std::size_t channel
242 );
243
244 void set_time_offset(Sample::TimeT new_time_offset) noexcept;
245 void set_sample_rate(std::size_t new_sample_rate) noexcept;
246
254 void emplace_time(Sample::TimeT given_time);
255
258
282};
283
284template <> constexpr std::string_view Object<Signal>::class_name = "Signal";
285
286} // namespace echomap
287
288#endif // ECHOMAP_SIGNAL_HPP
Object specification.
static constexpr std::string_view class_name
Display name for objects of the type determined by the templated class.
Definition Object.hpp:235
void reserve_samples(std::size_t count)
Reserves memory to store the given number of total samples in the channel.
Definition Signal.cpp:191
std::uint64_t get_sample_count() const noexcept
Retrieves the total number of samples in the Signal stream.
Definition Signal.cpp:38
static constexpr std::pair< Sample::AmplitudeT, Sample::AmplitudeT > normalised_range
The range within which the amplitude values are normalised.
Definition Signal.hpp:74
void emplace_sample_from_source(Sample::AmplitudeT amplitude)
Emplace an externally sourced sample to the back of the channel sample data.
Definition Signal.cpp:164
void emplace_sample(Sample::AmplitudeT amplitude)
Emplace a sample to the back of the channel sample data.
Definition Signal.cpp:131
std::vector< Sample::AmplitudeT > samples
Amplitude sample stream.
Definition Signal.hpp:55
std::optional< Source > fs_source
External source, if any, of the Signal Sample stream.
Definition Signal.hpp:257
Baseline timing_baseline
A baseline of timing parameters.
Definition Signal.hpp:256
std::optional< std::vector< Sample::TimeT > > time_offsets
An optional vector of explicit timestamps for each sample.
Definition Signal.hpp:281
const std::optional< Source > & observe_source() const noexcept
Retrieves the optional Source of the Signal.
Definition Signal.cpp:43
Sample::TimeT get_time_at_index(std::size_t index) const noexcept
Determines the corresponding time of the amplitude appearing at the given index in the sample array.
Definition Signal.cpp:95
void emplace_time(Sample::TimeT given_time)
Implementation helper to associate the latest amplitude sample with the given time.
Definition Signal.cpp:224
The main EchoMap outermost namespace for all non-exported symbols.
STL namespace.
T size(T... args)
Provides basic timing information relating to the Signal samples.
Definition Signal.hpp:141
Sample::TimeT time_offset
Timestamp, in seconds, of the first sample.
Definition Signal.hpp:142
std::size_t sample_rate
Constant sample rate, in Hz, of the signal.
Definition Signal.hpp:143
float sample_rate_r
Reciprocal of the sample rate; zero if sample rate is zero.
Definition Signal.hpp:144
A PCM float-32 sampled audio point at an explicit time offset.
Definition Signal.hpp:46
float AmplitudeT
Type for sample amplitudes.
Definition Signal.hpp:48
AmplitudeT amplitude
Normalised amplitude at the time.
Definition Signal.hpp:51
TimeT time
Time, in ms.
Definition Signal.hpp:50
float TimeT
Type for sample times.
Definition Signal.hpp:47
Indicates an external source of a Signal on the filesystem.
Definition Signal.hpp:62
bool dirty
Does the Signal contain additional samples?
Definition Signal.hpp:65
std::size_t channel
The channel number of the Signal within the given file.
Definition Signal.hpp:64
bool is_loaded
Has the Signal loaded all samples from the filesystem?
Definition Signal.hpp:66
std::filesystem::path path
The path (absolute or relative to CWD) of the source wave file.
Definition Signal.hpp:63