blob: 7bad87a7a80e6c209904b49af54cff2333390e26 [file] [log] [blame]
kwiberg@webrtc.org877083c2014-08-20 07:42:461/*
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
11// MSVC++ requires this to be set before any other includes to get M_PI.
12#define _USE_MATH_DEFINES
13
14#include <cmath>
15#include <limits>
16
kwiberg77eab702016-09-29 00:42:0117#include "webrtc/test/gtest.h"
kwiberg@webrtc.org877083c2014-08-20 07:42:4618#include "webrtc/common_audio/wav_header.h"
andrew@webrtc.orga3ed7132014-10-31 21:51:0319#include "webrtc/common_audio/wav_file.h"
kwiberg@webrtc.org877083c2014-08-20 07:42:4620#include "webrtc/test/testsupport/fileutils.h"
21
andrew@webrtc.org048c5022014-12-16 20:17:2122namespace webrtc {
23
kwiberg@webrtc.org877083c2014-08-20 07:42:4624static const float kSamples[] = {0.0, 10.0, 4e4, -1e9};
25
26// Write a tiny WAV file with the C++ interface and verify the result.
27TEST(WavWriterTest, CPP) {
andrew@webrtc.org048c5022014-12-16 20:17:2128 const std::string outfile = test::OutputPath() + "wavtest1.wav";
pkasting25702cb2016-01-08 21:50:2729 static const size_t kNumSamples = 3;
kwiberg@webrtc.org877083c2014-08-20 07:42:4630 {
andrew@webrtc.org048c5022014-12-16 20:17:2131 WavWriter w(outfile, 14099, 1);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:0432 EXPECT_EQ(14099, w.sample_rate());
Peter Kasting69558702016-01-13 00:26:3533 EXPECT_EQ(1u, w.num_channels());
kwiberg@webrtc.org584cd8d2014-08-25 06:26:0434 EXPECT_EQ(0u, w.num_samples());
kwiberg@webrtc.org877083c2014-08-20 07:42:4635 w.WriteSamples(kSamples, kNumSamples);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:0436 EXPECT_EQ(kNumSamples, w.num_samples());
kwiberg@webrtc.org877083c2014-08-20 07:42:4637 }
andrew@webrtc.org048c5022014-12-16 20:17:2138 // Write some extra "metadata" to the file that should be silently ignored
39 // by WavReader. We don't use WavWriter directly for this because it doesn't
40 // support metadata.
41 static const uint8_t kMetadata[] = {101, 202};
42 {
43 FILE* f = fopen(outfile.c_str(), "ab");
44 ASSERT_TRUE(f);
45 ASSERT_EQ(1u, fwrite(kMetadata, sizeof(kMetadata), 1, f));
46 fclose(f);
47 }
kwiberg@webrtc.org877083c2014-08-20 07:42:4648 static const uint8_t kExpectedContents[] = {
49 'R', 'I', 'F', 'F',
50 42, 0, 0, 0, // size of whole file - 8: 6 + 44 - 8
51 'W', 'A', 'V', 'E',
52 'f', 'm', 't', ' ',
53 16, 0, 0, 0, // size of fmt block - 8: 24 - 8
54 1, 0, // format: PCM (1)
55 1, 0, // channels: 1
56 0x13, 0x37, 0, 0, // sample rate: 14099
57 0x26, 0x6e, 0, 0, // byte rate: 2 * 14099
58 2, 0, // block align: NumChannels * BytesPerSample
59 16, 0, // bits per sample: 2 * 8
60 'd', 'a', 't', 'a',
61 6, 0, 0, 0, // size of payload: 6
62 0, 0, // first sample: 0.0
63 10, 0, // second sample: 10.0
64 0xff, 0x7f, // third sample: 4e4 (saturated)
andrew@webrtc.org048c5022014-12-16 20:17:2165 kMetadata[0], kMetadata[1],
kwiberg@webrtc.org877083c2014-08-20 07:42:4666 };
pkasting25702cb2016-01-08 21:50:2767 static const size_t kContentSize =
andrew@webrtc.org048c5022014-12-16 20:17:2168 kWavHeaderSize + kNumSamples * sizeof(int16_t) + sizeof(kMetadata);
kwiberg@webrtc.org2ebfac52015-01-14 10:51:5469 static_assert(sizeof(kExpectedContents) == kContentSize, "content size");
pkasting25702cb2016-01-08 21:50:2770 EXPECT_EQ(kContentSize, test::GetFileSize(outfile));
kwiberg@webrtc.org877083c2014-08-20 07:42:4671 FILE* f = fopen(outfile.c_str(), "rb");
72 ASSERT_TRUE(f);
73 uint8_t contents[kContentSize];
74 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f));
75 EXPECT_EQ(0, fclose(f));
76 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize));
andrew@webrtc.orga3ed7132014-10-31 21:51:0377
78 {
andrew@webrtc.org048c5022014-12-16 20:17:2179 WavReader r(outfile);
andrew@webrtc.orga3ed7132014-10-31 21:51:0380 EXPECT_EQ(14099, r.sample_rate());
Peter Kasting69558702016-01-13 00:26:3581 EXPECT_EQ(1u, r.num_channels());
andrew@webrtc.orga3ed7132014-10-31 21:51:0382 EXPECT_EQ(kNumSamples, r.num_samples());
83 static const float kTruncatedSamples[] = {0.0, 10.0, 32767.0};
84 float samples[kNumSamples];
85 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, samples));
86 EXPECT_EQ(0, memcmp(kTruncatedSamples, samples, sizeof(samples)));
87 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, samples));
88 }
kwiberg@webrtc.org877083c2014-08-20 07:42:4689}
90
91// Write a tiny WAV file with the C interface and verify the result.
92TEST(WavWriterTest, C) {
andrew@webrtc.org048c5022014-12-16 20:17:2193 const std::string outfile = test::OutputPath() + "wavtest2.wav";
94 rtc_WavWriter* w = rtc_WavOpen(outfile.c_str(), 11904, 2);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:0495 EXPECT_EQ(11904, rtc_WavSampleRate(w));
Peter Kasting69558702016-01-13 00:26:3596 EXPECT_EQ(2u, rtc_WavNumChannels(w));
kwiberg@webrtc.org584cd8d2014-08-25 06:26:0497 EXPECT_EQ(0u, rtc_WavNumSamples(w));
pkasting25702cb2016-01-08 21:50:2798 static const size_t kNumSamples = 4;
kwiberg@webrtc.org877083c2014-08-20 07:42:4699 rtc_WavWriteSamples(w, &kSamples[0], 2);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04100 EXPECT_EQ(2u, rtc_WavNumSamples(w));
kwiberg@webrtc.org877083c2014-08-20 07:42:46101 rtc_WavWriteSamples(w, &kSamples[2], kNumSamples - 2);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04102 EXPECT_EQ(kNumSamples, rtc_WavNumSamples(w));
kwiberg@webrtc.org877083c2014-08-20 07:42:46103 rtc_WavClose(w);
104 static const uint8_t kExpectedContents[] = {
105 'R', 'I', 'F', 'F',
106 44, 0, 0, 0, // size of whole file - 8: 8 + 44 - 8
107 'W', 'A', 'V', 'E',
108 'f', 'm', 't', ' ',
109 16, 0, 0, 0, // size of fmt block - 8: 24 - 8
110 1, 0, // format: PCM (1)
111 2, 0, // channels: 2
112 0x80, 0x2e, 0, 0, // sample rate: 11904
113 0, 0xba, 0, 0, // byte rate: 2 * 2 * 11904
114 4, 0, // block align: NumChannels * BytesPerSample
115 16, 0, // bits per sample: 2 * 8
116 'd', 'a', 't', 'a',
117 8, 0, 0, 0, // size of payload: 8
118 0, 0, // first sample: 0.0
119 10, 0, // second sample: 10.0
120 0xff, 0x7f, // third sample: 4e4 (saturated)
121 0, 0x80, // fourth sample: -1e9 (saturated)
122 };
pkasting25702cb2016-01-08 21:50:27123 static const size_t kContentSize =
andrew@webrtc.org048c5022014-12-16 20:17:21124 kWavHeaderSize + kNumSamples * sizeof(int16_t);
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54125 static_assert(sizeof(kExpectedContents) == kContentSize, "content size");
pkasting25702cb2016-01-08 21:50:27126 EXPECT_EQ(kContentSize, test::GetFileSize(outfile));
kwiberg@webrtc.org877083c2014-08-20 07:42:46127 FILE* f = fopen(outfile.c_str(), "rb");
128 ASSERT_TRUE(f);
129 uint8_t contents[kContentSize];
130 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f));
131 EXPECT_EQ(0, fclose(f));
132 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize));
133}
134
135// Write a larger WAV file. You can listen to this file to sanity-check it.
136TEST(WavWriterTest, LargeFile) {
andrew@webrtc.org048c5022014-12-16 20:17:21137 std::string outfile = test::OutputPath() + "wavtest3.wav";
kwiberg@webrtc.org877083c2014-08-20 07:42:46138 static const int kSampleRate = 8000;
Peter Kasting69558702016-01-13 00:26:35139 static const size_t kNumChannels = 2;
pkasting25702cb2016-01-08 21:50:27140 static const size_t kNumSamples = 3 * kSampleRate * kNumChannels;
kwiberg@webrtc.org877083c2014-08-20 07:42:46141 float samples[kNumSamples];
pkasting25702cb2016-01-08 21:50:27142 for (size_t i = 0; i < kNumSamples; i += kNumChannels) {
kwiberg@webrtc.org877083c2014-08-20 07:42:46143 // A nice periodic beeping sound.
144 static const double kToneHz = 440;
145 const double t = static_cast<double>(i) / (kNumChannels * kSampleRate);
146 const double x =
147 std::numeric_limits<int16_t>::max() * std::sin(t * kToneHz * 2 * M_PI);
148 samples[i] = std::pow(std::sin(t * 2 * 2 * M_PI), 10) * x;
149 samples[i + 1] = std::pow(std::cos(t * 2 * 2 * M_PI), 10) * x;
150 }
151 {
andrew@webrtc.org048c5022014-12-16 20:17:21152 WavWriter w(outfile, kSampleRate, kNumChannels);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04153 EXPECT_EQ(kSampleRate, w.sample_rate());
154 EXPECT_EQ(kNumChannels, w.num_channels());
155 EXPECT_EQ(0u, w.num_samples());
kwiberg@webrtc.org877083c2014-08-20 07:42:46156 w.WriteSamples(samples, kNumSamples);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04157 EXPECT_EQ(kNumSamples, w.num_samples());
kwiberg@webrtc.org877083c2014-08-20 07:42:46158 }
andrew@webrtc.org048c5022014-12-16 20:17:21159 EXPECT_EQ(sizeof(int16_t) * kNumSamples + kWavHeaderSize,
160 test::GetFileSize(outfile));
andrew@webrtc.orga3ed7132014-10-31 21:51:03161
162 {
andrew@webrtc.org048c5022014-12-16 20:17:21163 WavReader r(outfile);
andrew@webrtc.orga3ed7132014-10-31 21:51:03164 EXPECT_EQ(kSampleRate, r.sample_rate());
165 EXPECT_EQ(kNumChannels, r.num_channels());
166 EXPECT_EQ(kNumSamples, r.num_samples());
167
168 float read_samples[kNumSamples];
169 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, read_samples));
170 for (size_t i = 0; i < kNumSamples; ++i)
171 EXPECT_NEAR(samples[i], read_samples[i], 1);
172
173 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, read_samples));
174 }
kwiberg@webrtc.org877083c2014-08-20 07:42:46175}
andrew@webrtc.org048c5022014-12-16 20:17:21176
177} // namespace webrtc