2014-10-31 21:51:03 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#ifndef COMMON_AUDIO_WAV_FILE_H_
|
|
|
|
|
#define COMMON_AUDIO_WAV_FILE_H_
|
2014-10-31 21:51:03 +00:00
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2019-07-05 19:08:33 +02:00
|
|
|
|
2014-10-31 21:51:03 +00:00
|
|
|
#include <cstddef>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/constructor_magic.h"
|
2018-03-19 15:40:00 +01:00
|
|
|
#include "rtc_base/platform_file.h"
|
2019-06-13 14:08:24 +02:00
|
|
|
#include "rtc_base/system/file_wrapper.h"
|
2015-08-24 17:29:26 -07:00
|
|
|
|
2014-10-31 21:51:03 +00:00
|
|
|
namespace webrtc {
|
|
|
|
|
|
2015-08-24 17:29:26 -07:00
|
|
|
// Interface to provide access to WAV file parameters.
|
|
|
|
|
class WavFile {
|
|
|
|
|
public:
|
|
|
|
|
virtual ~WavFile() {}
|
|
|
|
|
|
|
|
|
|
virtual int sample_rate() const = 0;
|
Convert channel counts to size_t.
IIRC, this was originally requested by ajm during review of the other size_t conversions I did over the past year, and I agreed it made sense, but wanted to do it separately since those changes were already gargantuan.
BUG=chromium:81439
TEST=none
R=henrik.lundin@webrtc.org, henrika@webrtc.org, kjellander@webrtc.org, minyue@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1316523002 .
Cr-Commit-Position: refs/heads/master@{#11229}
2016-01-12 16:26:35 -08:00
|
|
|
virtual size_t num_channels() const = 0;
|
Misc. small cleanups.
* Better param names
* Avoid using negative values for (bogus) placeholder channel counts (mostly in tests). Since channels will be changing to size_t, negative values will be illegal; it's sufficient to use 0 in these cases.
* Use arraysize()
* Use size_t for counting frames, samples, blocks, buffers, and bytes -- most of these are already size_t in most places, this just fixes some stragglers
* reinterpret_cast<int64_t>(void*) is not necessarily safe; use uintptr_t instead
* Remove unnecessary code, e.g. dead code, needlessly long/repetitive code, or function overrides that exactly match the base definition
* Fix indenting
* Use uint32_t for timestamps (matching how it's already a uint32_t in most places)
* Spelling
* RTC_CHECK_EQ(expected, actual)
* Rewrap
* Use .empty()
* Be more pedantic about matching int/int32_t/
* Remove pointless consts on input parameters to functions
* Add missing sanity checks
All this was found in the course of constructing https://codereview.webrtc.org/1316523002/ , and is being landed separately first.
BUG=none
TEST=none
Review URL: https://codereview.webrtc.org/1534193008
Cr-Commit-Position: refs/heads/master@{#11191}
2016-01-08 13:50:27 -08:00
|
|
|
virtual size_t num_samples() const = 0;
|
2015-08-24 17:29:26 -07:00
|
|
|
};
|
|
|
|
|
|
2014-10-31 21:51:03 +00:00
|
|
|
// Simple C++ class for writing 16-bit PCM WAV files. All error handling is
|
2015-09-17 00:24:34 -07:00
|
|
|
// by calls to RTC_CHECK(), making it unsuitable for anything but debug code.
|
2015-08-24 17:29:26 -07:00
|
|
|
class WavWriter final : public WavFile {
|
2014-10-31 21:51:03 +00:00
|
|
|
public:
|
|
|
|
|
// Open a new WAV file for writing.
|
Convert channel counts to size_t.
IIRC, this was originally requested by ajm during review of the other size_t conversions I did over the past year, and I agreed it made sense, but wanted to do it separately since those changes were already gargantuan.
BUG=chromium:81439
TEST=none
R=henrik.lundin@webrtc.org, henrika@webrtc.org, kjellander@webrtc.org, minyue@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1316523002 .
Cr-Commit-Position: refs/heads/master@{#11229}
2016-01-12 16:26:35 -08:00
|
|
|
WavWriter(const std::string& filename, int sample_rate, size_t num_channels);
|
2014-10-31 21:51:03 +00:00
|
|
|
|
2018-03-19 15:40:00 +01:00
|
|
|
// Open a new WAV file for writing.
|
2019-06-13 14:08:24 +02:00
|
|
|
WavWriter(FileWrapper file, int sample_rate, size_t num_channels);
|
2018-03-19 15:40:00 +01:00
|
|
|
|
2014-10-31 21:51:03 +00:00
|
|
|
// Close the WAV file, after writing its header.
|
2016-02-22 02:13:28 -08:00
|
|
|
~WavWriter() override;
|
2014-10-31 21:51:03 +00:00
|
|
|
|
|
|
|
|
// Write additional samples to the file. Each sample is in the range
|
|
|
|
|
// [-32768,32767], and there must be the previously specified number of
|
|
|
|
|
// interleaved channels.
|
|
|
|
|
void WriteSamples(const float* samples, size_t num_samples);
|
|
|
|
|
void WriteSamples(const int16_t* samples, size_t num_samples);
|
|
|
|
|
|
2016-02-22 02:13:28 -08:00
|
|
|
int sample_rate() const override;
|
|
|
|
|
size_t num_channels() const override;
|
|
|
|
|
size_t num_samples() const override;
|
2014-10-31 21:51:03 +00:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void Close();
|
|
|
|
|
const int sample_rate_;
|
Convert channel counts to size_t.
IIRC, this was originally requested by ajm during review of the other size_t conversions I did over the past year, and I agreed it made sense, but wanted to do it separately since those changes were already gargantuan.
BUG=chromium:81439
TEST=none
R=henrik.lundin@webrtc.org, henrika@webrtc.org, kjellander@webrtc.org, minyue@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1316523002 .
Cr-Commit-Position: refs/heads/master@{#11229}
2016-01-12 16:26:35 -08:00
|
|
|
const size_t num_channels_;
|
Misc. small cleanups.
* Better param names
* Avoid using negative values for (bogus) placeholder channel counts (mostly in tests). Since channels will be changing to size_t, negative values will be illegal; it's sufficient to use 0 in these cases.
* Use arraysize()
* Use size_t for counting frames, samples, blocks, buffers, and bytes -- most of these are already size_t in most places, this just fixes some stragglers
* reinterpret_cast<int64_t>(void*) is not necessarily safe; use uintptr_t instead
* Remove unnecessary code, e.g. dead code, needlessly long/repetitive code, or function overrides that exactly match the base definition
* Fix indenting
* Use uint32_t for timestamps (matching how it's already a uint32_t in most places)
* Spelling
* RTC_CHECK_EQ(expected, actual)
* Rewrap
* Use .empty()
* Be more pedantic about matching int/int32_t/
* Remove pointless consts on input parameters to functions
* Add missing sanity checks
All this was found in the course of constructing https://codereview.webrtc.org/1316523002/ , and is being landed separately first.
BUG=none
TEST=none
Review URL: https://codereview.webrtc.org/1534193008
Cr-Commit-Position: refs/heads/master@{#11191}
2016-01-08 13:50:27 -08:00
|
|
|
size_t num_samples_; // Total number of samples written to file.
|
2019-06-13 14:08:24 +02:00
|
|
|
FileWrapper file_; // Output file, owned by this class
|
2015-08-24 17:29:26 -07:00
|
|
|
|
2015-09-16 05:37:44 -07:00
|
|
|
RTC_DISALLOW_COPY_AND_ASSIGN(WavWriter);
|
2014-10-31 21:51:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Follows the conventions of WavWriter.
|
2015-08-24 17:29:26 -07:00
|
|
|
class WavReader final : public WavFile {
|
2014-10-31 21:51:03 +00:00
|
|
|
public:
|
|
|
|
|
// Opens an existing WAV file for reading.
|
|
|
|
|
explicit WavReader(const std::string& filename);
|
|
|
|
|
|
|
|
|
|
// Close the WAV file.
|
2016-02-22 02:13:28 -08:00
|
|
|
~WavReader() override;
|
2014-10-31 21:51:03 +00:00
|
|
|
|
2019-04-16 16:49:32 +02:00
|
|
|
// Resets position to the beginning of the file.
|
|
|
|
|
void Reset();
|
|
|
|
|
|
2014-10-31 21:51:03 +00:00
|
|
|
// Returns the number of samples read. If this is less than requested,
|
|
|
|
|
// verifies that the end of the file was reached.
|
|
|
|
|
size_t ReadSamples(size_t num_samples, float* samples);
|
|
|
|
|
size_t ReadSamples(size_t num_samples, int16_t* samples);
|
|
|
|
|
|
2016-02-22 02:13:28 -08:00
|
|
|
int sample_rate() const override;
|
|
|
|
|
size_t num_channels() const override;
|
|
|
|
|
size_t num_samples() const override;
|
2014-10-31 21:51:03 +00:00
|
|
|
|
|
|
|
|
private:
|
2019-06-27 10:52:14 +02:00
|
|
|
// Opens an existing WAV file for reading.
|
|
|
|
|
explicit WavReader(rtc::PlatformFile file);
|
|
|
|
|
|
2014-10-31 21:51:03 +00:00
|
|
|
void Close();
|
|
|
|
|
int sample_rate_;
|
Convert channel counts to size_t.
IIRC, this was originally requested by ajm during review of the other size_t conversions I did over the past year, and I agreed it made sense, but wanted to do it separately since those changes were already gargantuan.
BUG=chromium:81439
TEST=none
R=henrik.lundin@webrtc.org, henrika@webrtc.org, kjellander@webrtc.org, minyue@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1316523002 .
Cr-Commit-Position: refs/heads/master@{#11229}
2016-01-12 16:26:35 -08:00
|
|
|
size_t num_channels_;
|
Misc. small cleanups.
* Better param names
* Avoid using negative values for (bogus) placeholder channel counts (mostly in tests). Since channels will be changing to size_t, negative values will be illegal; it's sufficient to use 0 in these cases.
* Use arraysize()
* Use size_t for counting frames, samples, blocks, buffers, and bytes -- most of these are already size_t in most places, this just fixes some stragglers
* reinterpret_cast<int64_t>(void*) is not necessarily safe; use uintptr_t instead
* Remove unnecessary code, e.g. dead code, needlessly long/repetitive code, or function overrides that exactly match the base definition
* Fix indenting
* Use uint32_t for timestamps (matching how it's already a uint32_t in most places)
* Spelling
* RTC_CHECK_EQ(expected, actual)
* Rewrap
* Use .empty()
* Be more pedantic about matching int/int32_t/
* Remove pointless consts on input parameters to functions
* Add missing sanity checks
All this was found in the course of constructing https://codereview.webrtc.org/1316523002/ , and is being landed separately first.
BUG=none
TEST=none
Review URL: https://codereview.webrtc.org/1534193008
Cr-Commit-Position: refs/heads/master@{#11191}
2016-01-08 13:50:27 -08:00
|
|
|
size_t num_samples_; // Total number of samples in the file.
|
|
|
|
|
size_t num_samples_remaining_;
|
2014-10-31 21:51:03 +00:00
|
|
|
FILE* file_handle_; // Input file, owned by this class.
|
2019-04-16 16:49:32 +02:00
|
|
|
fpos_t data_start_pos_; // Position in the file immediately after WAV header.
|
2015-08-24 17:29:26 -07:00
|
|
|
|
2015-09-16 05:37:44 -07:00
|
|
|
RTC_DISALLOW_COPY_AND_ASSIGN(WavReader);
|
2014-10-31 21:51:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#endif // COMMON_AUDIO_WAV_FILE_H_
|