File: Sample.h

package info (click to toggle)
lsp-plugins 1.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 91,856 kB
  • sloc: cpp: 427,831; xml: 57,779; makefile: 9,961; php: 1,005; sh: 18
file content (379 lines) | stat: -rw-r--r-- 17,563 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
 * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
 *           (C) 2020 Vladimir Sadovnikov <sadko4u@gmail.com>
 *
 * This file is part of lsp-dsp-units
 * Created on: 12 мая 2017 г.
 *
 * lsp-dsp-units is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * lsp-dsp-units is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with lsp-dsp-units. If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef LSP_PLUG_IN_DSP_UNITS_SAMPLING_SAMPLE_H_
#define LSP_PLUG_IN_DSP_UNITS_SAMPLING_SAMPLE_H_

#include <lsp-plug.in/dsp-units/version.h>
#include <lsp-plug.in/dsp-units/iface/IStateDumper.h>
#include <lsp-plug.in/dsp-units/sampling/types.h>
#include <lsp-plug.in/io/Path.h>
#include <lsp-plug.in/runtime/LSPString.h>
#include <lsp-plug.in/mm/IOutAudioStream.h>
#include <lsp-plug.in/mm/IInAudioStream.h>

#define AUDIO_SAMPLE_CONTENT_TYPE       "application/x-lsp-audio-sample"

namespace lsp
{
    namespace dspu
    {
        /**
         * An audio sample: audio data in PCM format that stores each audio channel in
         * a separate sequential part of the memory as 32-bit float values.
         */
        class LSP_DSP_UNITS_PUBLIC Sample
        {
            private:
                Sample & operator = (const Sample &);
                Sample(const Sample &);

            private:
                float              *vBuffer;        // Sample data
                size_t              nSampleRate;    // Sample rate
                size_t              nLength;        // Current length
                size_t              nMaxLength;     // Maximum possible length
                size_t              nChannels;      // Number of channels
                size_t              nGcRefs;        // GC stuff: Number of references
                Sample             *pGcNext;        // GC stuff: Pointer to the next
                mutable void       *pUserData;      // Some user data attached to sample

            protected:
                static void         put_chunk_linear(float *dst, const float *src, size_t len, size_t fade_in, size_t fade_out);
                static void         put_chunk_const_power(float *dst, const float *src, size_t len, size_t fade_in, size_t fade_out);

                typedef void        (*put_chunk_t)(float *dst, const float *src, size_t len, size_t fade_in, size_t fade_out);

            protected:
                status_t            fast_downsample(Sample *s, size_t new_sample_rate);
                status_t            fast_upsample(Sample *s, size_t new_sample_rate);
                status_t            complex_downsample(Sample *s, size_t new_sample_rate);
                status_t            complex_upsample(Sample *s, size_t new_sample_rate);
                status_t            do_simple_stretch(size_t new_length, size_t start, size_t end, put_chunk_t put_chunk);
                status_t            do_single_crossfade_stretch(size_t new_length, size_t fade_len, size_t start, size_t end, put_chunk_t put_chunk);
                status_t            open_stream_ext(mm::IInAudioStream **is, const io::Path *path);
                status_t            try_open_regular_file(mm::IInAudioStream **is, const io::Path *path);
                status_t            try_open_lspc(mm::IInAudioStream **is, const io::Path *lspc, const io::Path *item);

            public:
                explicit Sample();
                ~Sample();

                /**
                 * Create uninitialied sample
                 */
                void        construct();

                /** Drop sample contents
                 *
                 */
                void        destroy();

            public: // Garbage-collected stuff
                /**
                 * Get number of references
                 * @return number of references
                 */
                inline size_t       gc_references() const   { return nGcRefs;       }

                /**
                 * Incremente reference counter
                 * @return new number of references
                 */
                inline size_t       gc_acquire()            { return ++nGcRefs;     }

                /**
                 * Decrement reference counter
                 * @return new number of references
                 */
                inline size_t       gc_release()            { return --nGcRefs;     }

                /**
                 * Get pointer to the next sample in the single-directional garbage list
                 * @return next sample reference in the garbage list
                 */
                inline Sample      *gc_next()               { return pGcNext;       }

                /**
                 * Link sample to the next in the garbage list
                 * @param next pointer to next sample
                 * @return previously stored pointer to next sample
                 */
                Sample             *gc_link(Sample *next);

            public: // Regular suff
                inline bool         valid() const                   { return (vBuffer != NULL) && (nChannels > 0) && (nLength > 0) && (nMaxLength > 0); }
                inline size_t       max_length() const              { return nMaxLength; }

                inline float       *getBuffer(size_t channel)       { return &vBuffer[nMaxLength * channel]; }
                inline const float *getBuffer(size_t channel) const { return &vBuffer[nMaxLength * channel]; }

                inline float       *channel(size_t channel)         { return &vBuffer[nMaxLength * channel]; }
                inline const float *channel(size_t channel) const   { return &vBuffer[nMaxLength * channel]; }

                inline float       *getBuffer(size_t channel, size_t offset) { return &vBuffer[nMaxLength * channel + offset]; }
                inline const float *getBuffer(size_t channel, size_t offset) const { return &vBuffer[nMaxLength * channel + offset]; }

                /**
                 * Return number of audio channels
                 * @return number of audio channels
                 */
                inline size_t       channels() const                { return nChannels;     }

                /**
                 * Get sample length in samples
                 * @return sample length in samples
                 */
                inline size_t       samples() const                 { return nLength;       }
                inline size_t       length() const                  { return nLength;       }

                /**
                 * Return the sample duration in seconds, available only if sample rate is specified
                 * @return sample duration in seconds
                 */
                inline double       duration() const                { return (nSampleRate > 0) ? double(nLength) / double(nSampleRate) : 0.0; }

                /**
                 * Get sample rate
                 * @return actual sample rate
                 */
                inline size_t       sample_rate() const             { return nSampleRate;   }

                /**
                 * Set sample rate
                 * @param srate sample rate of the sample
                 */
                inline void         set_sample_rate(size_t srate)   { nSampleRate = srate;  }

                /**
                 * Copy sample contents
                 * @param s source sample to perform copy
                 * @return status of operation
                 */
                status_t            copy(const Sample *s);
                inline status_t     copy(const Sample &s)           { return copy(&s);      }

                /** Set length of sample
                 *
                 * @param length length to set
                 * @return actual length of the sample
                 */
                inline size_t set_length(size_t length)
                {
                    if (length > nMaxLength)
                        length = nMaxLength;
                    return nLength = length;
                }

                /** Extend length of sample
                 *
                 * @param length length to extend
                 * @return actual length of the sample
                 */
                inline size_t extend(size_t length)
                {
                    if (length > nMaxLength)
                        length = nMaxLength;
                    if (nLength < length)
                        nLength = length;
                    return nLength;
                }

                /** Clear sample (make length equal to zero
                 *
                 */
                inline void clear()
                {
                    nLength     = 0;
                }

                /** Initialize sample, all previously allocated data will be lost
                 *
                 * @param channels number of channels
                 * @param max_length maximum possible sample length
                 * @param length initial sample length
                 * @return true if data was successful allocated
                 */
                bool init(size_t channels, size_t max_length, size_t length = 0);

                /** Resize sample, all previously allocated data will be kept
                 *
                 * @param channels number of channels
                 * @param max_length maximum possible sample length
                 * @param length initial sample length
                 * @return true if data was successful resized
                 */
                bool resize(size_t channels, size_t max_length, size_t length = 0);

                /** Stretch part of the sample
                 *
                 * @param new_length the new length of the stretched region in samples
                 * @param chunk_size chunk size in samples, 0 means automatic chunk size selection
                 * @param fade_type the crossfade type between chunks
                 * @param fade_size the relative size of the crossfade region between two chunks in range of 0 to 1
                 * @param start the number of the sample associated with the start of the range to be stretched
                 * @param end the number of the first sample after the end of the range to be stretched
                 * @return status of operation
                 */
                status_t stretch(
                    size_t new_length, size_t chunk_size,
                    sample_crossfade_t fade_type, float fade_size,
                    size_t start, size_t end);

                /** Stretch the whole sample
                 *
                 * @param new_length the new length of the sample in samples
                 * @param chunk_size chunk size in samples, 0 means automatic chunk size selection
                 * @param fade_type the crossfade type between chunks
                 * @param fade_size the relative size of the crossfade region between two chunks in range of 0 to 1
                 * @return true if data was successfuly stretched
                 */
                status_t stretch(size_t new_length, size_t chunk_size, sample_crossfade_t fade_type, float fade_size);

                /** Resize sample to match the specified number of audio channels,
                 * all previously allocated data will be kept
                 *
                 * @param channels number of channels
                 */
                bool set_channels(size_t channels);

                /** Resample sample
                 *
                 * @param new_sample_rate new sample rate
                 * @return status of operation
                 */
                status_t resample(size_t new_sample_rate);

                /** Reverse track
                 *
                 * @param channel channel to reverse
                 * @return true on success
                 */
                bool reverse(size_t channel);

                /** Reverse sample
                 *
                 * @return true on success
                 */
                void reverse();

                /**
                 * Normalize the sample
                 * @param gain the maximum peak gain
                 * @param mode the normalization mode
                 */
                void normalize(float gain, sample_normalize_t mode);

                /**
                 * Swap contents with another sample
                 * @param dst sample to perform swap
                 */
                void swap(Sample *dst);
                inline void swap(Sample &dst)                   { swap(&dst);                           }

                /**
                 * Save sample contents to file
                 * @param path path to the file
                 * @param offset first sample to save
                 * @param count maximum number of samples to save, all available if negative
                 * @return actual number of samples written or negative error code
                 */
                ssize_t save_range(const char *path, size_t offset, ssize_t count = -1);
                ssize_t save_range(const LSPString *path, size_t offset, ssize_t count = -1);
                ssize_t save_range(const io::Path *path, size_t offset, ssize_t count = -1);
                ssize_t save_range(mm::IOutAudioStream *out, size_t offset, ssize_t count = -1);

                /**
                 * Save sample contents to file
                 * @param path path to the file
                 * @return actual number of samples written or negative error code
                 */
                inline ssize_t save(const char *path)           { return save_range(path, 0, nLength);  }
                inline ssize_t save(const LSPString *path)      { return save_range(path, 0, nLength);  }
                inline ssize_t save(const io::Path *path)       { return save_range(path, 0, nLength);  }
                ssize_t save(mm::IOutAudioStream *out)          { return save_range(out, 0, nLength);   }

                /**
                 * Load file
                 * @param path location of the file
                 * @param max_duration maximum duration in seconds
                 * @return status of operation
                 */
                status_t load(const char *path, float max_duration = -1);
                status_t load(const LSPString *path, float max_duration = -1);
                status_t load(const io::Path *path, float max_duration = -1);
                status_t load(mm::IInAudioStream *in, float max_duration = -1);

                /**
                 * Load file
                 * @param path location of the file
                 * @param max_samples maximum number of samples
                 * @return status of operation
                 */
                status_t loads(const char *path, ssize_t max_samples = -1);
                status_t loads(const LSPString *path, ssize_t max_samples = -1);
                status_t loads(const io::Path *path, ssize_t max_samples = -1);
                status_t loads(mm::IInAudioStream *in, ssize_t max_samples = -1);

                /**
                 * Extended load file: if no success, try to locate file in the archive like LSPC, etc.
                 * @param path path to the file
                 * @param max_duration maximum duration in seconds
                 * @return status of operation
                 */
                status_t load_ext(const char *path, float max_duration = -1);
                status_t load_ext(const LSPString *path, float max_duration = -1);
                status_t load_ext(const io::Path *path, float max_duration = -1);

                /**
                 * Extended load file: if no success, try to locate file in the archive like LSPC, etc.
                 * @param path location of the file
                 * @param max_samples maximum number of samples
                 * @return status of operation
                 */
                status_t loads_ext(const char *path, ssize_t max_samples = -1);
                status_t loads_ext(const LSPString *path, ssize_t max_samples = -1);
                status_t loads_ext(const io::Path *path, ssize_t max_samples = -1);

                /**
                 * Get some user data linked to the sample
                 * @return user data linked to the sample
                 */
                inline void        *user_data() const       { return pUserData;     }

                /**
                 * Set user data. The user is fully responsible for managing the data associated with the
                 * pointer, i.e. allocation, deallocation and access.
                 * @param user user data to set
                 * @return previously used user data
                 */
                void               *set_user_data(void *user);

                /**
                 * Dump the state
                 * @param dumper dumper
                 */
                void dump(IStateDumper *v) const;
        };
    }
} /* namespace lsp */

#endif /* LSP_PLUG_IN_DSP_UNITS_SAMPLING_SAMPLE_H_ */