1/* 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" 20#include "webrtc/common_audio/wav_writer.h" 21#include "webrtc/test/testsupport/fileutils.h" 22 23static const float kSamples[] = {0.0, 10.0, 4e4, -1e9}; 24 25// Write a tiny WAV file with the C++ interface and verify the result. 26TEST(WavWriterTest, CPP) { 27 const std::string outfile = webrtc::test::OutputPath() + "wavtest1.wav"; 28 static const uint32_t kNumSamples = 3; 29 { 30 webrtc::WavFile w(outfile, 14099, 1); 31 EXPECT_EQ(14099, w.sample_rate()); 32 EXPECT_EQ(1, w.num_channels()); 33 EXPECT_EQ(0u, w.num_samples()); 34 w.WriteSamples(kSamples, kNumSamples); 35 EXPECT_EQ(kNumSamples, w.num_samples()); 36 } 37 static const uint8_t kExpectedContents[] = { 38 'R', 'I', 'F', 'F', 39 42, 0, 0, 0, // size of whole file - 8: 6 + 44 - 8 40 'W', 'A', 'V', 'E', 41 'f', 'm', 't', ' ', 42 16, 0, 0, 0, // size of fmt block - 8: 24 - 8 43 1, 0, // format: PCM (1) 44 1, 0, // channels: 1 45 0x13, 0x37, 0, 0, // sample rate: 14099 46 0x26, 0x6e, 0, 0, // byte rate: 2 * 14099 47 2, 0, // block align: NumChannels * BytesPerSample 48 16, 0, // bits per sample: 2 * 8 49 'd', 'a', 't', 'a', 50 6, 0, 0, 0, // size of payload: 6 51 0, 0, // first sample: 0.0 52 10, 0, // second sample: 10.0 53 0xff, 0x7f, // third sample: 4e4 (saturated) 54 }; 55 static const int kContentSize = 56 webrtc::kWavHeaderSize + kNumSamples * sizeof(int16_t); 57 COMPILE_ASSERT(sizeof(kExpectedContents) == kContentSize, content_size); 58 EXPECT_EQ(size_t(kContentSize), webrtc::test::GetFileSize(outfile)); 59 FILE* f = fopen(outfile.c_str(), "rb"); 60 ASSERT_TRUE(f); 61 uint8_t contents[kContentSize]; 62 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f)); 63 EXPECT_EQ(0, fclose(f)); 64 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize)); 65} 66 67// Write a tiny WAV file with the C interface and verify the result. 68TEST(WavWriterTest, C) { 69 const std::string outfile = webrtc::test::OutputPath() + "wavtest2.wav"; 70 rtc_WavFile *w = rtc_WavOpen(outfile.c_str(), 11904, 2); 71 EXPECT_EQ(11904, rtc_WavSampleRate(w)); 72 EXPECT_EQ(2, rtc_WavNumChannels(w)); 73 EXPECT_EQ(0u, rtc_WavNumSamples(w)); 74 static const uint32_t kNumSamples = 4; 75 rtc_WavWriteSamples(w, &kSamples[0], 2); 76 EXPECT_EQ(2u, rtc_WavNumSamples(w)); 77 rtc_WavWriteSamples(w, &kSamples[2], kNumSamples - 2); 78 EXPECT_EQ(kNumSamples, rtc_WavNumSamples(w)); 79 rtc_WavClose(w); 80 static const uint8_t kExpectedContents[] = { 81 'R', 'I', 'F', 'F', 82 44, 0, 0, 0, // size of whole file - 8: 8 + 44 - 8 83 'W', 'A', 'V', 'E', 84 'f', 'm', 't', ' ', 85 16, 0, 0, 0, // size of fmt block - 8: 24 - 8 86 1, 0, // format: PCM (1) 87 2, 0, // channels: 2 88 0x80, 0x2e, 0, 0, // sample rate: 11904 89 0, 0xba, 0, 0, // byte rate: 2 * 2 * 11904 90 4, 0, // block align: NumChannels * BytesPerSample 91 16, 0, // bits per sample: 2 * 8 92 'd', 'a', 't', 'a', 93 8, 0, 0, 0, // size of payload: 8 94 0, 0, // first sample: 0.0 95 10, 0, // second sample: 10.0 96 0xff, 0x7f, // third sample: 4e4 (saturated) 97 0, 0x80, // fourth sample: -1e9 (saturated) 98 }; 99 static const int kContentSize = 100 webrtc::kWavHeaderSize + kNumSamples * sizeof(int16_t); 101 COMPILE_ASSERT(sizeof(kExpectedContents) == kContentSize, content_size); 102 EXPECT_EQ(size_t(kContentSize), webrtc::test::GetFileSize(outfile)); 103 FILE* f = fopen(outfile.c_str(), "rb"); 104 ASSERT_TRUE(f); 105 uint8_t contents[kContentSize]; 106 ASSERT_EQ(1u, fread(contents, kContentSize, 1, f)); 107 EXPECT_EQ(0, fclose(f)); 108 EXPECT_EQ(0, memcmp(kExpectedContents, contents, kContentSize)); 109} 110 111// Write a larger WAV file. You can listen to this file to sanity-check it. 112TEST(WavWriterTest, LargeFile) { 113 std::string outfile = webrtc::test::OutputPath() + "wavtest3.wav"; 114 static const int kSampleRate = 8000; 115 static const int kNumChannels = 2; 116 static const uint32_t kNumSamples = 3 * kSampleRate * kNumChannels; 117 float samples[kNumSamples]; 118 for (uint32_t i = 0; i < kNumSamples; i += kNumChannels) { 119 // A nice periodic beeping sound. 120 static const double kToneHz = 440; 121 const double t = static_cast<double>(i) / (kNumChannels * kSampleRate); 122 const double x = 123 std::numeric_limits<int16_t>::max() * std::sin(t * kToneHz * 2 * M_PI); 124 samples[i] = std::pow(std::sin(t * 2 * 2 * M_PI), 10) * x; 125 samples[i + 1] = std::pow(std::cos(t * 2 * 2 * M_PI), 10) * x; 126 } 127 { 128 webrtc::WavFile w(outfile, kSampleRate, kNumChannels); 129 EXPECT_EQ(kSampleRate, w.sample_rate()); 130 EXPECT_EQ(kNumChannels, w.num_channels()); 131 EXPECT_EQ(0u, w.num_samples()); 132 w.WriteSamples(samples, kNumSamples); 133 EXPECT_EQ(kNumSamples, w.num_samples()); 134 } 135 EXPECT_EQ(sizeof(int16_t) * kNumSamples + webrtc::kWavHeaderSize, 136 webrtc::test::GetFileSize(outfile)); 137} 138