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
|
|
|
#include "common_audio/wav_file.h"
|
2014-10-31 21:51:03 +00:00
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <limits>
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "common_audio/include/audio_util.h"
|
|
|
|
|
#include "common_audio/wav_header.h"
|
|
|
|
|
#include "rtc_base/checks.h"
|
2018-03-19 15:40:00 +01:00
|
|
|
#include "rtc_base/logging.h"
|
2017-11-22 10:42:26 +01:00
|
|
|
#include "rtc_base/numerics/safe_conversions.h"
|
2018-07-25 16:05:48 +02:00
|
|
|
#include "rtc_base/system/arch.h"
|
2014-10-31 21:51:03 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
|
|
// We write 16-bit PCM WAV files.
|
|
|
|
|
static const WavFormat kWavFormat = kWavFormatPcm;
|
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
|
|
|
static const size_t kBytesPerSample = 2;
|
2014-10-31 21:51:03 +00:00
|
|
|
|
2014-12-16 20:17:21 +00:00
|
|
|
// Doesn't take ownership of the file handle and won't close it.
|
|
|
|
|
class ReadableWavFile : public ReadableWav {
|
|
|
|
|
public:
|
|
|
|
|
explicit ReadableWavFile(FILE* file) : file_(file) {}
|
2018-07-17 11:08:15 +02:00
|
|
|
size_t Read(void* buf, size_t num_bytes) override {
|
2014-12-16 20:17:21 +00:00
|
|
|
return fread(buf, 1, num_bytes, file_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
FILE* file_;
|
|
|
|
|
};
|
|
|
|
|
|
2014-10-31 21:51:03 +00:00
|
|
|
WavReader::WavReader(const std::string& filename)
|
2018-03-19 15:40:00 +01:00
|
|
|
: WavReader(rtc::OpenPlatformFileReadOnly(filename)) {}
|
|
|
|
|
|
|
|
|
|
WavReader::WavReader(rtc::PlatformFile file) {
|
|
|
|
|
RTC_CHECK_NE(file, rtc::kInvalidPlatformFileValue)
|
|
|
|
|
<< "Invalid file. Could not create file handle for wav file.";
|
|
|
|
|
file_handle_ = rtc::FdopenPlatformFile(file, "rb");
|
|
|
|
|
if (!file_handle_) {
|
|
|
|
|
RTC_LOG(LS_ERROR) << "Could not open wav file for reading: " << errno;
|
|
|
|
|
// Even though we failed to open a FILE*, the file is still open
|
|
|
|
|
// and needs to be closed.
|
|
|
|
|
if (!rtc::ClosePlatformFile(file)) {
|
|
|
|
|
RTC_LOG(LS_ERROR) << "Can't close file.";
|
|
|
|
|
}
|
|
|
|
|
FATAL() << "Could not open wav file for reading.";
|
|
|
|
|
}
|
2014-10-31 21:51:03 +00:00
|
|
|
|
2014-12-16 20:17:21 +00:00
|
|
|
ReadableWavFile readable(file_handle_);
|
2014-10-31 21:51:03 +00:00
|
|
|
WavFormat format;
|
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 bytes_per_sample;
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_CHECK(ReadWavHeader(&readable, &num_channels_, &sample_rate_, &format,
|
|
|
|
|
&bytes_per_sample, &num_samples_));
|
2014-12-16 20:17:21 +00:00
|
|
|
num_samples_remaining_ = num_samples_;
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_CHECK_EQ(kWavFormat, format);
|
|
|
|
|
RTC_CHECK_EQ(kBytesPerSample, bytes_per_sample);
|
2014-10-31 21:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WavReader::~WavReader() {
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-22 02:13:28 -08:00
|
|
|
int WavReader::sample_rate() const {
|
|
|
|
|
return sample_rate_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t WavReader::num_channels() const {
|
|
|
|
|
return num_channels_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t WavReader::num_samples() const {
|
|
|
|
|
return num_samples_;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-31 21:51:03 +00:00
|
|
|
size_t WavReader::ReadSamples(size_t num_samples, int16_t* samples) {
|
|
|
|
|
#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
|
|
|
|
|
#error "Need to convert samples to big-endian when reading from WAV file"
|
|
|
|
|
#endif
|
2014-12-16 20:17:21 +00:00
|
|
|
// There could be metadata after the audio; ensure we don't read it.
|
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
|
|
|
num_samples = std::min(num_samples, num_samples_remaining_);
|
2014-10-31 21:51:03 +00:00
|
|
|
const size_t read =
|
|
|
|
|
fread(samples, sizeof(*samples), num_samples, file_handle_);
|
|
|
|
|
// If we didn't read what was requested, ensure we've reached the EOF.
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_CHECK(read == num_samples || feof(file_handle_));
|
|
|
|
|
RTC_CHECK_LE(read, num_samples_remaining_);
|
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
|
|
|
num_samples_remaining_ -= read;
|
2014-10-31 21:51:03 +00:00
|
|
|
return read;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t WavReader::ReadSamples(size_t num_samples, float* samples) {
|
|
|
|
|
static const size_t kChunksize = 4096 / sizeof(uint16_t);
|
|
|
|
|
size_t read = 0;
|
|
|
|
|
for (size_t i = 0; i < num_samples; i += kChunksize) {
|
|
|
|
|
int16_t isamples[kChunksize];
|
|
|
|
|
size_t chunk = std::min(kChunksize, num_samples - i);
|
|
|
|
|
chunk = ReadSamples(chunk, isamples);
|
|
|
|
|
for (size_t j = 0; j < chunk; ++j)
|
|
|
|
|
samples[i + j] = isamples[j];
|
|
|
|
|
read += chunk;
|
|
|
|
|
}
|
|
|
|
|
return read;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WavReader::Close() {
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_CHECK_EQ(0, fclose(file_handle_));
|
2017-02-26 04:18:12 -08:00
|
|
|
file_handle_ = nullptr;
|
2014-10-31 21:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
2018-03-19 15:40:00 +01:00
|
|
|
WavWriter::WavWriter(const std::string& filename,
|
|
|
|
|
int sample_rate,
|
|
|
|
|
size_t num_channels)
|
|
|
|
|
// Unlike plain fopen, CreatePlatformFile takes care of filename utf8 ->
|
|
|
|
|
// wchar conversion on windows.
|
|
|
|
|
: WavWriter(rtc::CreatePlatformFile(filename), sample_rate, num_channels) {}
|
|
|
|
|
|
|
|
|
|
WavWriter::WavWriter(rtc::PlatformFile file,
|
|
|
|
|
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)
|
2018-03-19 15:40:00 +01:00
|
|
|
: sample_rate_(sample_rate), num_channels_(num_channels), num_samples_(0) {
|
|
|
|
|
// Handle errors from the CreatePlatformFile call in above constructor.
|
|
|
|
|
RTC_CHECK_NE(file, rtc::kInvalidPlatformFileValue)
|
|
|
|
|
<< "Invalid file. Could not create wav file.";
|
|
|
|
|
file_handle_ = rtc::FdopenPlatformFile(file, "wb");
|
|
|
|
|
if (!file_handle_) {
|
|
|
|
|
RTC_LOG(LS_ERROR) << "Could not open wav file for writing.";
|
|
|
|
|
// Even though we failed to open a FILE*, the file is still open
|
|
|
|
|
// and needs to be closed.
|
|
|
|
|
if (!rtc::ClosePlatformFile(file)) {
|
|
|
|
|
RTC_LOG(LS_ERROR) << "Can't close file.";
|
|
|
|
|
}
|
|
|
|
|
FATAL() << "Could not open wav file for writing.";
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_CHECK(CheckWavParameters(num_channels_, sample_rate_, kWavFormat,
|
|
|
|
|
kBytesPerSample, num_samples_));
|
2014-10-31 21:51:03 +00:00
|
|
|
|
|
|
|
|
// Write a blank placeholder header, since we need to know the total number
|
|
|
|
|
// of samples before we can fill in the real data.
|
|
|
|
|
static const uint8_t blank_header[kWavHeaderSize] = {0};
|
2016-11-28 15:21:39 -08:00
|
|
|
RTC_CHECK_EQ(1, fwrite(blank_header, kWavHeaderSize, 1, file_handle_));
|
2014-10-31 21:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WavWriter::~WavWriter() {
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-22 02:13:28 -08:00
|
|
|
int WavWriter::sample_rate() const {
|
|
|
|
|
return sample_rate_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t WavWriter::num_channels() const {
|
|
|
|
|
return num_channels_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t WavWriter::num_samples() const {
|
|
|
|
|
return num_samples_;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-31 21:51:03 +00:00
|
|
|
void WavWriter::WriteSamples(const int16_t* samples, size_t num_samples) {
|
|
|
|
|
#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
|
|
|
|
|
#error "Need to convert samples to little-endian when writing to WAV file"
|
|
|
|
|
#endif
|
|
|
|
|
const size_t written =
|
|
|
|
|
fwrite(samples, sizeof(*samples), num_samples, file_handle_);
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_CHECK_EQ(num_samples, written);
|
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
|
|
|
num_samples_ += written;
|
|
|
|
|
RTC_CHECK(num_samples_ >= written); // detect size_t overflow
|
2014-10-31 21:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WavWriter::WriteSamples(const float* samples, size_t num_samples) {
|
|
|
|
|
static const size_t kChunksize = 4096 / sizeof(uint16_t);
|
|
|
|
|
for (size_t i = 0; i < num_samples; i += kChunksize) {
|
|
|
|
|
int16_t isamples[kChunksize];
|
|
|
|
|
const size_t chunk = std::min(kChunksize, num_samples - i);
|
|
|
|
|
FloatS16ToS16(samples + i, chunk, isamples);
|
|
|
|
|
WriteSamples(isamples, chunk);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WavWriter::Close() {
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_CHECK_EQ(0, fseek(file_handle_, 0, SEEK_SET));
|
2014-10-31 21:51:03 +00:00
|
|
|
uint8_t header[kWavHeaderSize];
|
2014-11-03 18:20:06 +00:00
|
|
|
WriteWavHeader(header, num_channels_, sample_rate_, kWavFormat,
|
|
|
|
|
kBytesPerSample, num_samples_);
|
2016-11-28 15:21:39 -08:00
|
|
|
RTC_CHECK_EQ(1, fwrite(header, kWavHeaderSize, 1, file_handle_));
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_CHECK_EQ(0, fclose(file_handle_));
|
2017-02-26 04:18:12 -08:00
|
|
|
file_handle_ = nullptr;
|
2014-10-31 21:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|
|
|
|
|
|
|
|
|
|
rtc_WavWriter* rtc_WavOpen(const char* filename,
|
|
|
|
|
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) {
|
2014-10-31 21:51:03 +00:00
|
|
|
return reinterpret_cast<rtc_WavWriter*>(
|
|
|
|
|
new webrtc::WavWriter(filename, sample_rate, num_channels));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void rtc_WavClose(rtc_WavWriter* wf) {
|
|
|
|
|
delete reinterpret_cast<webrtc::WavWriter*>(wf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void rtc_WavWriteSamples(rtc_WavWriter* wf,
|
|
|
|
|
const float* samples,
|
|
|
|
|
size_t num_samples) {
|
|
|
|
|
reinterpret_cast<webrtc::WavWriter*>(wf)->WriteSamples(samples, num_samples);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int rtc_WavSampleRate(const rtc_WavWriter* wf) {
|
|
|
|
|
return reinterpret_cast<const webrtc::WavWriter*>(wf)->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 rtc_WavNumChannels(const rtc_WavWriter* wf) {
|
2014-10-31 21:51:03 +00:00
|
|
|
return reinterpret_cast<const webrtc::WavWriter*>(wf)->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 rtc_WavNumSamples(const rtc_WavWriter* wf) {
|
2014-10-31 21:51:03 +00:00
|
|
|
return reinterpret_cast<const webrtc::WavWriter*>(wf)->num_samples();
|
|
|
|
|
}
|