blob: d7aa2c1c03a89c9c01233e069a4f454cfb8e39c1 [file] [log] [blame]
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +00001/*
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef COMMON_AUDIO_WAV_FILE_H_
12#define COMMON_AUDIO_WAV_FILE_H_
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000013
14#ifdef __cplusplus
15
16#include <stdint.h>
17#include <cstddef>
18#include <string>
19
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/constructor_magic.h"
Artem Titove62f6002018-03-19 15:40:00 +010021#include "rtc_base/platform_file.h"
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070022
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000023namespace webrtc {
24
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070025// Interface to provide access to WAV file parameters.
26class WavFile {
27 public:
28 virtual ~WavFile() {}
29
30 virtual int sample_rate() const = 0;
Peter Kasting69558702016-01-12 16:26:35 -080031 virtual size_t num_channels() const = 0;
pkasting25702cb2016-01-08 13:50:27 -080032 virtual size_t num_samples() const = 0;
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070033};
34
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000035// Simple C++ class for writing 16-bit PCM WAV files. All error handling is
henrikg91d6ede2015-09-17 00:24:34 -070036// by calls to RTC_CHECK(), making it unsuitable for anything but debug code.
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070037class WavWriter final : public WavFile {
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000038 public:
39 // Open a new WAV file for writing.
Peter Kasting69558702016-01-12 16:26:35 -080040 WavWriter(const std::string& filename, int sample_rate, size_t num_channels);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000041
Artem Titove62f6002018-03-19 15:40:00 +010042 // Open a new WAV file for writing.
43 WavWriter(rtc::PlatformFile file, int sample_rate, size_t num_channels);
44
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000045 // Close the WAV file, after writing its header.
peahd1f718b2016-02-22 02:13:28 -080046 ~WavWriter() override;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000047
48 // Write additional samples to the file. Each sample is in the range
49 // [-32768,32767], and there must be the previously specified number of
50 // interleaved channels.
51 void WriteSamples(const float* samples, size_t num_samples);
52 void WriteSamples(const int16_t* samples, size_t num_samples);
53
peahd1f718b2016-02-22 02:13:28 -080054 int sample_rate() const override;
55 size_t num_channels() const override;
56 size_t num_samples() const override;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000057
58 private:
59 void Close();
60 const int sample_rate_;
Peter Kasting69558702016-01-12 16:26:35 -080061 const size_t num_channels_;
pkasting25702cb2016-01-08 13:50:27 -080062 size_t num_samples_; // Total number of samples written to file.
Yves Gerey665174f2018-06-19 15:03:05 +020063 FILE* file_handle_; // Output file, owned by this class
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070064
henrikg3c089d72015-09-16 05:37:44 -070065 RTC_DISALLOW_COPY_AND_ASSIGN(WavWriter);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000066};
67
68// Follows the conventions of WavWriter.
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070069class WavReader final : public WavFile {
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000070 public:
71 // Opens an existing WAV file for reading.
72 explicit WavReader(const std::string& filename);
73
Artem Titove62f6002018-03-19 15:40:00 +010074 // Opens an existing WAV file for reading.
75 explicit WavReader(rtc::PlatformFile file);
76
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000077 // Close the WAV file.
peahd1f718b2016-02-22 02:13:28 -080078 ~WavReader() override;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000079
Artem Titov153056b2019-04-16 16:49:32 +020080 // Resets position to the beginning of the file.
81 void Reset();
82
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000083 // Returns the number of samples read. If this is less than requested,
84 // verifies that the end of the file was reached.
85 size_t ReadSamples(size_t num_samples, float* samples);
86 size_t ReadSamples(size_t num_samples, int16_t* samples);
87
peahd1f718b2016-02-22 02:13:28 -080088 int sample_rate() const override;
89 size_t num_channels() const override;
90 size_t num_samples() const override;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000091
92 private:
93 void Close();
94 int sample_rate_;
Peter Kasting69558702016-01-12 16:26:35 -080095 size_t num_channels_;
pkasting25702cb2016-01-08 13:50:27 -080096 size_t num_samples_; // Total number of samples in the file.
97 size_t num_samples_remaining_;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000098 FILE* file_handle_; // Input file, owned by this class.
Artem Titov153056b2019-04-16 16:49:32 +020099 fpos_t data_start_pos_; // Position in the file immediately after WAV header.
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -0700100
henrikg3c089d72015-09-16 05:37:44 -0700101 RTC_DISALLOW_COPY_AND_ASSIGN(WavReader);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000102};
103
104} // namespace webrtc
105
106extern "C" {
107#endif // __cplusplus
108
109// C wrappers for the WavWriter class.
110typedef struct rtc_WavWriter rtc_WavWriter;
111rtc_WavWriter* rtc_WavOpen(const char* filename,
112 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800113 size_t num_channels);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000114void rtc_WavClose(rtc_WavWriter* wf);
115void rtc_WavWriteSamples(rtc_WavWriter* wf,
116 const float* samples,
117 size_t num_samples);
118int rtc_WavSampleRate(const rtc_WavWriter* wf);
Peter Kasting69558702016-01-12 16:26:35 -0800119size_t rtc_WavNumChannels(const rtc_WavWriter* wf);
pkasting25702cb2016-01-08 13:50:27 -0800120size_t rtc_WavNumSamples(const rtc_WavWriter* wf);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000121
122#ifdef __cplusplus
123} // extern "C"
124#endif
125
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200126#endif // COMMON_AUDIO_WAV_FILE_H_