153 lines
4.8 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2015 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.
*/
#include "webrtc/base/checks.h"
#include "webrtc/modules/audio_processing/test/test_utils.h"
namespace webrtc {
RawFile::RawFile(const std::string& filename)
: file_handle_(fopen(filename.c_str(), "wb")) {}
RawFile::~RawFile() {
fclose(file_handle_);
}
void RawFile::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 PCM file"
#endif
fwrite(samples, sizeof(*samples), num_samples, file_handle_);
}
void RawFile::WriteSamples(const float* samples, size_t num_samples) {
fwrite(samples, sizeof(*samples), num_samples, file_handle_);
}
Reland of Add aecdump support to audioproc_f. (patchset #2 id:250001 of https://codereview.webrtc.org/1423693008/ ) Reason for revert: Oh dear, this broke compilation. I guess more was built on top of this CL before I reverted it. Reverting now for futher investigation (and re-land using CQ) Original issue's description: > Revert of Add aecdump support to audioproc_f. (patchset #8 id:200001 of https://codereview.webrtc.org/1409943002/ ) > > Reason for revert: > This breaks iOS GYP generation as described on http://www.webrtc.org/native-code/ios > I'm going to drive getting the build_with_libjingle=1 setting removed from the bots to match the official instructions. > > See https://code.google.com/p/webrtc/issues/detail?id=4653 for more context, as this is exactly what that issue tries to solve. > > Original issue's description: > > Add aecdump support to audioproc_f. > > > > Add a new interface to abstract away file operations. This CL temporarily > > removes support for dumping the output of reverse streams. It will be easy to > > restore in the new framework, although we may decide to only allow it with > > the aecdump format. > > > > We also now require the user to specify the output format, rather than > > defaulting to the input format. > > > > TEST=Bit-exact output to the previous audioproc_f version using an input wav > > file, and to the legacy audioproc using an aecdump file. > > > > Committed: https://crrev.com/bdafe31b86e9819b0adb9041f87e6194b7422b08 > > Cr-Commit-Position: refs/heads/master@{#10460} > > TBR=aluebs@webrtc.org,peah@webrtc.org,andrew@webrtc.org > NOPRESUBMIT=true > NOTREECHECKS=true > NOTRY=true > > Committed: https://crrev.com/d279941bb54bfdc6e7324bf36cac76581474b96d > Cr-Commit-Position: refs/heads/master@{#10523} TBR=aluebs@webrtc.org,peah@webrtc.org,andrew@webrtc.org NOPRESUBMIT=true NOTREECHECKS=true NOTRY=true Review URL: https://codereview.webrtc.org/1419953010 Cr-Commit-Position: refs/heads/master@{#10524}
2015-11-05 06:23:02 -08:00
ChannelBufferWavReader::ChannelBufferWavReader(rtc::scoped_ptr<WavReader> file)
: file_(file.Pass()) {}
bool ChannelBufferWavReader::Read(ChannelBuffer<float>* buffer) {
RTC_CHECK_EQ(file_->num_channels(), buffer->num_channels());
interleaved_.resize(buffer->size());
if (file_->ReadSamples(interleaved_.size(), &interleaved_[0]) !=
interleaved_.size()) {
return false;
}
FloatS16ToFloat(&interleaved_[0], interleaved_.size(), &interleaved_[0]);
Deinterleave(&interleaved_[0], buffer->num_frames(), buffer->num_channels(),
buffer->channels());
return true;
}
ChannelBufferWavWriter::ChannelBufferWavWriter(rtc::scoped_ptr<WavWriter> file)
: file_(file.Pass()) {}
void ChannelBufferWavWriter::Write(const ChannelBuffer<float>& buffer) {
RTC_CHECK_EQ(file_->num_channels(), buffer.num_channels());
interleaved_.resize(buffer.size());
Interleave(buffer.channels(), buffer.num_frames(), buffer.num_channels(),
&interleaved_[0]);
FloatToFloatS16(&interleaved_[0], interleaved_.size(), &interleaved_[0]);
file_->WriteSamples(&interleaved_[0], interleaved_.size());
}
void WriteIntData(const int16_t* data,
size_t length,
WavWriter* wav_file,
RawFile* raw_file) {
if (wav_file) {
wav_file->WriteSamples(data, length);
}
if (raw_file) {
raw_file->WriteSamples(data, length);
}
}
void WriteFloatData(const float* const* data,
int samples_per_channel,
int num_channels,
WavWriter* wav_file,
RawFile* raw_file) {
size_t length = num_channels * samples_per_channel;
rtc::scoped_ptr<float[]> buffer(new float[length]);
Interleave(data, samples_per_channel, num_channels, buffer.get());
if (raw_file) {
raw_file->WriteSamples(buffer.get(), length);
}
// TODO(aluebs): Use ScaleToInt16Range() from audio_util
for (size_t i = 0; i < length; ++i) {
buffer[i] = buffer[i] > 0 ?
buffer[i] * std::numeric_limits<int16_t>::max() :
-buffer[i] * std::numeric_limits<int16_t>::min();
}
if (wav_file) {
wav_file->WriteSamples(buffer.get(), length);
}
}
FILE* OpenFile(const std::string& filename, const char* mode) {
FILE* file = fopen(filename.c_str(), mode);
if (!file) {
printf("Unable to open file %s\n", filename.c_str());
exit(1);
}
return file;
}
int SamplesFromRate(int rate) {
return AudioProcessing::kChunkSizeMs * rate / 1000;
}
void SetFrameSampleRate(AudioFrame* frame,
int sample_rate_hz) {
frame->sample_rate_hz_ = sample_rate_hz;
frame->samples_per_channel_ = AudioProcessing::kChunkSizeMs *
sample_rate_hz / 1000;
}
AudioProcessing::ChannelLayout LayoutFromChannels(int num_channels) {
switch (num_channels) {
case 1:
return AudioProcessing::kMono;
case 2:
return AudioProcessing::kStereo;
default:
Reland of Add aecdump support to audioproc_f. (patchset #2 id:250001 of https://codereview.webrtc.org/1423693008/ ) Reason for revert: Oh dear, this broke compilation. I guess more was built on top of this CL before I reverted it. Reverting now for futher investigation (and re-land using CQ) Original issue's description: > Revert of Add aecdump support to audioproc_f. (patchset #8 id:200001 of https://codereview.webrtc.org/1409943002/ ) > > Reason for revert: > This breaks iOS GYP generation as described on http://www.webrtc.org/native-code/ios > I'm going to drive getting the build_with_libjingle=1 setting removed from the bots to match the official instructions. > > See https://code.google.com/p/webrtc/issues/detail?id=4653 for more context, as this is exactly what that issue tries to solve. > > Original issue's description: > > Add aecdump support to audioproc_f. > > > > Add a new interface to abstract away file operations. This CL temporarily > > removes support for dumping the output of reverse streams. It will be easy to > > restore in the new framework, although we may decide to only allow it with > > the aecdump format. > > > > We also now require the user to specify the output format, rather than > > defaulting to the input format. > > > > TEST=Bit-exact output to the previous audioproc_f version using an input wav > > file, and to the legacy audioproc using an aecdump file. > > > > Committed: https://crrev.com/bdafe31b86e9819b0adb9041f87e6194b7422b08 > > Cr-Commit-Position: refs/heads/master@{#10460} > > TBR=aluebs@webrtc.org,peah@webrtc.org,andrew@webrtc.org > NOPRESUBMIT=true > NOTREECHECKS=true > NOTRY=true > > Committed: https://crrev.com/d279941bb54bfdc6e7324bf36cac76581474b96d > Cr-Commit-Position: refs/heads/master@{#10523} TBR=aluebs@webrtc.org,peah@webrtc.org,andrew@webrtc.org NOPRESUBMIT=true NOTREECHECKS=true NOTRY=true Review URL: https://codereview.webrtc.org/1419953010 Cr-Commit-Position: refs/heads/master@{#10524}
2015-11-05 06:23:02 -08:00
RTC_CHECK(false);
return AudioProcessing::kMono;
}
}
Reland of Add aecdump support to audioproc_f. (patchset #2 id:250001 of https://codereview.webrtc.org/1423693008/ ) Reason for revert: Oh dear, this broke compilation. I guess more was built on top of this CL before I reverted it. Reverting now for futher investigation (and re-land using CQ) Original issue's description: > Revert of Add aecdump support to audioproc_f. (patchset #8 id:200001 of https://codereview.webrtc.org/1409943002/ ) > > Reason for revert: > This breaks iOS GYP generation as described on http://www.webrtc.org/native-code/ios > I'm going to drive getting the build_with_libjingle=1 setting removed from the bots to match the official instructions. > > See https://code.google.com/p/webrtc/issues/detail?id=4653 for more context, as this is exactly what that issue tries to solve. > > Original issue's description: > > Add aecdump support to audioproc_f. > > > > Add a new interface to abstract away file operations. This CL temporarily > > removes support for dumping the output of reverse streams. It will be easy to > > restore in the new framework, although we may decide to only allow it with > > the aecdump format. > > > > We also now require the user to specify the output format, rather than > > defaulting to the input format. > > > > TEST=Bit-exact output to the previous audioproc_f version using an input wav > > file, and to the legacy audioproc using an aecdump file. > > > > Committed: https://crrev.com/bdafe31b86e9819b0adb9041f87e6194b7422b08 > > Cr-Commit-Position: refs/heads/master@{#10460} > > TBR=aluebs@webrtc.org,peah@webrtc.org,andrew@webrtc.org > NOPRESUBMIT=true > NOTREECHECKS=true > NOTRY=true > > Committed: https://crrev.com/d279941bb54bfdc6e7324bf36cac76581474b96d > Cr-Commit-Position: refs/heads/master@{#10523} TBR=aluebs@webrtc.org,peah@webrtc.org,andrew@webrtc.org NOPRESUBMIT=true NOTREECHECKS=true NOTRY=true Review URL: https://codereview.webrtc.org/1419953010 Cr-Commit-Position: refs/heads/master@{#10524}
2015-11-05 06:23:02 -08:00
std::vector<Point> ParseArrayGeometry(const std::string& mic_positions) {
const std::vector<float> values = ParseList<float>(mic_positions);
Reland of Add aecdump support to audioproc_f. (patchset #2 id:250001 of https://codereview.webrtc.org/1423693008/ ) Reason for revert: Oh dear, this broke compilation. I guess more was built on top of this CL before I reverted it. Reverting now for futher investigation (and re-land using CQ) Original issue's description: > Revert of Add aecdump support to audioproc_f. (patchset #8 id:200001 of https://codereview.webrtc.org/1409943002/ ) > > Reason for revert: > This breaks iOS GYP generation as described on http://www.webrtc.org/native-code/ios > I'm going to drive getting the build_with_libjingle=1 setting removed from the bots to match the official instructions. > > See https://code.google.com/p/webrtc/issues/detail?id=4653 for more context, as this is exactly what that issue tries to solve. > > Original issue's description: > > Add aecdump support to audioproc_f. > > > > Add a new interface to abstract away file operations. This CL temporarily > > removes support for dumping the output of reverse streams. It will be easy to > > restore in the new framework, although we may decide to only allow it with > > the aecdump format. > > > > We also now require the user to specify the output format, rather than > > defaulting to the input format. > > > > TEST=Bit-exact output to the previous audioproc_f version using an input wav > > file, and to the legacy audioproc using an aecdump file. > > > > Committed: https://crrev.com/bdafe31b86e9819b0adb9041f87e6194b7422b08 > > Cr-Commit-Position: refs/heads/master@{#10460} > > TBR=aluebs@webrtc.org,peah@webrtc.org,andrew@webrtc.org > NOPRESUBMIT=true > NOTREECHECKS=true > NOTRY=true > > Committed: https://crrev.com/d279941bb54bfdc6e7324bf36cac76581474b96d > Cr-Commit-Position: refs/heads/master@{#10523} TBR=aluebs@webrtc.org,peah@webrtc.org,andrew@webrtc.org NOPRESUBMIT=true NOTREECHECKS=true NOTRY=true Review URL: https://codereview.webrtc.org/1419953010 Cr-Commit-Position: refs/heads/master@{#10524}
2015-11-05 06:23:02 -08:00
const size_t num_mics =
rtc::CheckedDivExact(values.size(), static_cast<size_t>(3));
RTC_CHECK_GT(num_mics, 0u) << "mic_positions is not large enough.";
std::vector<Point> result;
result.reserve(num_mics);
for (size_t i = 0; i < values.size(); i += 3) {
Reland of Add aecdump support to audioproc_f. (patchset #2 id:250001 of https://codereview.webrtc.org/1423693008/ ) Reason for revert: Oh dear, this broke compilation. I guess more was built on top of this CL before I reverted it. Reverting now for futher investigation (and re-land using CQ) Original issue's description: > Revert of Add aecdump support to audioproc_f. (patchset #8 id:200001 of https://codereview.webrtc.org/1409943002/ ) > > Reason for revert: > This breaks iOS GYP generation as described on http://www.webrtc.org/native-code/ios > I'm going to drive getting the build_with_libjingle=1 setting removed from the bots to match the official instructions. > > See https://code.google.com/p/webrtc/issues/detail?id=4653 for more context, as this is exactly what that issue tries to solve. > > Original issue's description: > > Add aecdump support to audioproc_f. > > > > Add a new interface to abstract away file operations. This CL temporarily > > removes support for dumping the output of reverse streams. It will be easy to > > restore in the new framework, although we may decide to only allow it with > > the aecdump format. > > > > We also now require the user to specify the output format, rather than > > defaulting to the input format. > > > > TEST=Bit-exact output to the previous audioproc_f version using an input wav > > file, and to the legacy audioproc using an aecdump file. > > > > Committed: https://crrev.com/bdafe31b86e9819b0adb9041f87e6194b7422b08 > > Cr-Commit-Position: refs/heads/master@{#10460} > > TBR=aluebs@webrtc.org,peah@webrtc.org,andrew@webrtc.org > NOPRESUBMIT=true > NOTREECHECKS=true > NOTRY=true > > Committed: https://crrev.com/d279941bb54bfdc6e7324bf36cac76581474b96d > Cr-Commit-Position: refs/heads/master@{#10523} TBR=aluebs@webrtc.org,peah@webrtc.org,andrew@webrtc.org NOPRESUBMIT=true NOTREECHECKS=true NOTRY=true Review URL: https://codereview.webrtc.org/1419953010 Cr-Commit-Position: refs/heads/master@{#10524}
2015-11-05 06:23:02 -08:00
result.push_back(Point(values[i + 0], values[i + 1], values[i + 2]));
}
return result;
}
Reland of Add aecdump support to audioproc_f. (patchset #2 id:250001 of https://codereview.webrtc.org/1423693008/ ) Reason for revert: Oh dear, this broke compilation. I guess more was built on top of this CL before I reverted it. Reverting now for futher investigation (and re-land using CQ) Original issue's description: > Revert of Add aecdump support to audioproc_f. (patchset #8 id:200001 of https://codereview.webrtc.org/1409943002/ ) > > Reason for revert: > This breaks iOS GYP generation as described on http://www.webrtc.org/native-code/ios > I'm going to drive getting the build_with_libjingle=1 setting removed from the bots to match the official instructions. > > See https://code.google.com/p/webrtc/issues/detail?id=4653 for more context, as this is exactly what that issue tries to solve. > > Original issue's description: > > Add aecdump support to audioproc_f. > > > > Add a new interface to abstract away file operations. This CL temporarily > > removes support for dumping the output of reverse streams. It will be easy to > > restore in the new framework, although we may decide to only allow it with > > the aecdump format. > > > > We also now require the user to specify the output format, rather than > > defaulting to the input format. > > > > TEST=Bit-exact output to the previous audioproc_f version using an input wav > > file, and to the legacy audioproc using an aecdump file. > > > > Committed: https://crrev.com/bdafe31b86e9819b0adb9041f87e6194b7422b08 > > Cr-Commit-Position: refs/heads/master@{#10460} > > TBR=aluebs@webrtc.org,peah@webrtc.org,andrew@webrtc.org > NOPRESUBMIT=true > NOTREECHECKS=true > NOTRY=true > > Committed: https://crrev.com/d279941bb54bfdc6e7324bf36cac76581474b96d > Cr-Commit-Position: refs/heads/master@{#10523} TBR=aluebs@webrtc.org,peah@webrtc.org,andrew@webrtc.org NOPRESUBMIT=true NOTREECHECKS=true NOTRY=true Review URL: https://codereview.webrtc.org/1419953010 Cr-Commit-Position: refs/heads/master@{#10524}
2015-11-05 06:23:02 -08:00
std::vector<Point> ParseArrayGeometry(const std::string& mic_positions,
size_t num_mics) {
std::vector<Point> result = ParseArrayGeometry(mic_positions);
RTC_CHECK_EQ(result.size(), num_mics)
<< "Could not parse mic_positions or incorrect number of points.";
return result;
}
} // namespace webrtc