blob: 1991c3b59b54ec594222763957af4e69957e3777 [file] [log] [blame]
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +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
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
17#include "testing/gtest/include/gtest/gtest.h"
18#include "webrtc/base/compile_assert.h"
19#include "webrtc/common_audio/wav_header.h"
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000020#include "webrtc/common_audio/wav_file.h"
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000021#include "webrtc/test/testsupport/fileutils.h"
22
andrew@webrtc.org048c5022014-12-16 20:17:21 +000023namespace webrtc {
24
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000025static const float kSamples[] = {0.0, 10.0, 4e4, -1e9};
26
27// Write a tiny WAV file with the C++ interface and verify the result.
28TEST(WavWriterTest, CPP) {
andrew@webrtc.org048c5022014-12-16 20:17:21 +000029 const std::string outfile = test::OutputPath() + "wavtest1.wav";
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +000030 static const uint32_t kNumSamples = 3;
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000031 {
andrew@webrtc.org048c5022014-12-16 20:17:21 +000032 WavWriter w(outfile, 14099, 1);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +000033 EXPECT_EQ(14099, w.sample_rate());
34 EXPECT_EQ(1, w.num_channels());
35 EXPECT_EQ(0u, w.num_samples());
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000036 w.WriteSamples(kSamples, kNumSamples);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +000037 EXPECT_EQ(kNumSamples, w.num_samples());
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000038 }
andrew@webrtc.org048c5022014-12-16 20:17:21 +000039 // Write some extra "metadata" to the file that should be silently ignored
40 // by WavReader. We don't use WavWriter directly for this because it doesn't
41 // support metadata.
42 static const uint8_t kMetadata[] = {101, 202};
43 {
44 FILE* f = fopen(outfile.c_str(), "ab");
45 ASSERT_TRUE(f);
46 ASSERT_EQ(1u, fwrite(kMetadata, sizeof(kMetadata), 1, f));
47 fclose(f);
48 }
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000049 static const uint8_t kExpectedContents[] = {
50 'R', 'I', 'F', 'F',
51 42, 0, 0, 0, // size of whole file - 8: 6 + 44 - 8
52 'W', 'A', 'V', 'E',
53 'f', 'm', 't', ' ',
54 16, 0, 0, 0, // size of fmt block - 8: 24 - 8
55 1, 0, // format: PCM (1)
56 1, 0, // channels: 1
57 0x13, 0x37, 0, 0, // sample rate: 14099
58 0x26, 0x6e, 0, 0, // byte rate: 2 * 14099
59 2, 0, // block align: NumChannels * BytesPerSample
60 16, 0, // bits per sample: 2 * 8
61 'd', 'a', 't', 'a',
62 6, 0, 0, 0, // size of payload: 6
63 0, 0, // first sample: 0.0
64 10, 0, // second sample: 10.0
65 0xff, 0x7f, // third sample: 4e4 (saturated)
andrew@webrtc.org048c5022014-12-16 20:17:21 +000066 kMetadata[0], kMetadata[1],
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000067 };
68 static const int kContentSize =
andrew@webrtc.org048c5022014-12-16 20:17:21 +000069 kWavHeaderSize + kNumSamples * sizeof(int16_t) + sizeof(kMetadata);
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000070 COMPILE_ASSERT(sizeof(kExpectedContents) == kContentSize, content_size);
andrew@webrtc.org048c5022014-12-16 20:17:21 +000071 EXPECT_EQ(size_t(kContentSize), test::GetFileSize(outfile));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000072 FILE* f = fopen(outfile.c_str(), "rb");
73 ASSERT_TRUE(f);
74 uint8_t contents[kContentSize];
75 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f));
76 EXPECT_EQ(0, fclose(f));
77 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000078
79 {
andrew@webrtc.org048c5022014-12-16 20:17:21 +000080 WavReader r(outfile);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000081 EXPECT_EQ(14099, r.sample_rate());
82 EXPECT_EQ(1, r.num_channels());
83 EXPECT_EQ(kNumSamples, r.num_samples());
84 static const float kTruncatedSamples[] = {0.0, 10.0, 32767.0};
85 float samples[kNumSamples];
86 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, samples));
87 EXPECT_EQ(0, memcmp(kTruncatedSamples, samples, sizeof(samples)));
88 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, samples));
89 }
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000090}
91
92// Write a tiny WAV file with the C interface and verify the result.
93TEST(WavWriterTest, C) {
andrew@webrtc.org048c5022014-12-16 20:17:21 +000094 const std::string outfile = test::OutputPath() + "wavtest2.wav";
95 rtc_WavWriter* w = rtc_WavOpen(outfile.c_str(), 11904, 2);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +000096 EXPECT_EQ(11904, rtc_WavSampleRate(w));
97 EXPECT_EQ(2, rtc_WavNumChannels(w));
98 EXPECT_EQ(0u, rtc_WavNumSamples(w));
99 static const uint32_t kNumSamples = 4;
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000100 rtc_WavWriteSamples(w, &kSamples[0], 2);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000101 EXPECT_EQ(2u, rtc_WavNumSamples(w));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000102 rtc_WavWriteSamples(w, &kSamples[2], kNumSamples - 2);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000103 EXPECT_EQ(kNumSamples, rtc_WavNumSamples(w));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000104 rtc_WavClose(w);
105 static const uint8_t kExpectedContents[] = {
106 'R', 'I', 'F', 'F',
107 44, 0, 0, 0, // size of whole file - 8: 8 + 44 - 8
108 'W', 'A', 'V', 'E',
109 'f', 'm', 't', ' ',
110 16, 0, 0, 0, // size of fmt block - 8: 24 - 8
111 1, 0, // format: PCM (1)
112 2, 0, // channels: 2
113 0x80, 0x2e, 0, 0, // sample rate: 11904
114 0, 0xba, 0, 0, // byte rate: 2 * 2 * 11904
115 4, 0, // block align: NumChannels * BytesPerSample
116 16, 0, // bits per sample: 2 * 8
117 'd', 'a', 't', 'a',
118 8, 0, 0, 0, // size of payload: 8
119 0, 0, // first sample: 0.0
120 10, 0, // second sample: 10.0
121 0xff, 0x7f, // third sample: 4e4 (saturated)
122 0, 0x80, // fourth sample: -1e9 (saturated)
123 };
124 static const int kContentSize =
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000125 kWavHeaderSize + kNumSamples * sizeof(int16_t);
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000126 COMPILE_ASSERT(sizeof(kExpectedContents) == kContentSize, content_size);
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000127 EXPECT_EQ(size_t(kContentSize), test::GetFileSize(outfile));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000128 FILE* f = fopen(outfile.c_str(), "rb");
129 ASSERT_TRUE(f);
130 uint8_t contents[kContentSize];
131 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f));
132 EXPECT_EQ(0, fclose(f));
133 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize));
134}
135
136// Write a larger WAV file. You can listen to this file to sanity-check it.
137TEST(WavWriterTest, LargeFile) {
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000138 std::string outfile = test::OutputPath() + "wavtest3.wav";
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000139 static const int kSampleRate = 8000;
140 static const int kNumChannels = 2;
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000141 static const uint32_t kNumSamples = 3 * kSampleRate * kNumChannels;
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000142 float samples[kNumSamples];
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000143 for (uint32_t i = 0; i < kNumSamples; i += kNumChannels) {
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000144 // A nice periodic beeping sound.
145 static const double kToneHz = 440;
146 const double t = static_cast<double>(i) / (kNumChannels * kSampleRate);
147 const double x =
148 std::numeric_limits<int16_t>::max() * std::sin(t * kToneHz * 2 * M_PI);
149 samples[i] = std::pow(std::sin(t * 2 * 2 * M_PI), 10) * x;
150 samples[i + 1] = std::pow(std::cos(t * 2 * 2 * M_PI), 10) * x;
151 }
152 {
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000153 WavWriter w(outfile, kSampleRate, kNumChannels);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000154 EXPECT_EQ(kSampleRate, w.sample_rate());
155 EXPECT_EQ(kNumChannels, w.num_channels());
156 EXPECT_EQ(0u, w.num_samples());
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000157 w.WriteSamples(samples, kNumSamples);
kwiberg@webrtc.org584cd8d2014-08-25 06:26:04 +0000158 EXPECT_EQ(kNumSamples, w.num_samples());
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000159 }
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000160 EXPECT_EQ(sizeof(int16_t) * kNumSamples + kWavHeaderSize,
161 test::GetFileSize(outfile));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000162
163 {
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000164 WavReader r(outfile);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000165 EXPECT_EQ(kSampleRate, r.sample_rate());
166 EXPECT_EQ(kNumChannels, r.num_channels());
167 EXPECT_EQ(kNumSamples, r.num_samples());
168
169 float read_samples[kNumSamples];
170 EXPECT_EQ(kNumSamples, r.ReadSamples(kNumSamples, read_samples));
171 for (size_t i = 0; i < kNumSamples; ++i)
172 EXPECT_NEAR(samples[i], read_samples[i], 1);
173
174 EXPECT_EQ(0u, r.ReadSamples(kNumSamples, read_samples));
175 }
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000176}
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000177
178} // namespace webrtc