audio_buffer_unittest.cc revision effb81e5f8246d0db0270817048dc992db66e9fb
1926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 2926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// found in the LICENSE file. 4926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 5926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#include "base/strings/string_util.h" 6926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#include "base/strings/stringprintf.h" 7926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#include "media/base/audio_buffer.h" 8926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#include "media/base/audio_bus.h" 9926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#include "media/base/test_helpers.h" 10926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h" 11926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 12926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)namespace media { 13926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 14926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)const static int kSampleRate = 44100; 15926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 16926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)static void VerifyResult(float* channel_data, 17926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) int frames, 18926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) float start, 19926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) float increment) { 20926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) for (int i = 0; i < frames; ++i) { 21926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) SCOPED_TRACE(base::StringPrintf( 22926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) "i=%d/%d start=%f, increment=%f", i, frames, start, increment)); 23926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) ASSERT_EQ(channel_data[i], start); 24926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) start += increment; 25926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) } 26926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)} 2753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 28926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)TEST(AudioBufferTest, CopyFrom) { 295d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) const ChannelLayout channel_layout = CHANNEL_LAYOUT_MONO; 305d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) const int frames = 8; 315d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles) const base::TimeDelta start_time; 3253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) const base::TimeDelta duration = base::TimeDelta::FromSeconds(frames); 3353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) scoped_refptr<AudioBuffer> buffer = 3453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) MakeAudioBuffer<uint8>(kSampleFormatU8, 35926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) channel_layout, 36c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles) ChannelLayoutToChannelCount(channel_layout), 37926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) kSampleRate, 38926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 1, 39926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 1, 40926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) frames, 41926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) start_time, 42926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) duration); 4309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) EXPECT_EQ(frames, buffer->frame_count()); 4409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) EXPECT_EQ(buffer->timestamp(), start_time); 4509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) EXPECT_EQ(buffer->duration().InSeconds(), frames); 4609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) EXPECT_FALSE(buffer->end_of_stream()); 4709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)} 4809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) 4909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)TEST(AudioBufferTest, CreateEOSBuffer) { 5009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) scoped_refptr<AudioBuffer> buffer = AudioBuffer::CreateEOSBuffer(); 5109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) EXPECT_TRUE(buffer->end_of_stream()); 5209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)} 5309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) 5409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)TEST(AudioBufferTest, FrameSize) { 5509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) const uint8 kTestData[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 5609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 5709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) 27, 28, 29, 30, 31 }; 5809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) const base::TimeDelta kTimestampA = base::TimeDelta::FromMicroseconds(1337); 5909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) const base::TimeDelta kTimestampB = base::TimeDelta::FromMicroseconds(1234); 6009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) 6109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) const uint8* const data[] = { kTestData }; 6209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) scoped_refptr<AudioBuffer> buffer = 6309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) AudioBuffer::CopyFrom(kSampleFormatU8, 6409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) CHANNEL_LAYOUT_STEREO, 6509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) 2, 6609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) kSampleRate, 6709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) 16, 6809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) data, 6909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) kTimestampA, 7009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) kTimestampB); 7109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) EXPECT_EQ(16, buffer->frame_count()); // 2 channels of 8-bit data 7209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) 7309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) buffer = AudioBuffer::CopyFrom(kSampleFormatF32, 7409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) CHANNEL_LAYOUT_4_0, 7509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) 4, 7609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) kSampleRate, 7709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) 2, 7809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) data, 7909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) kTimestampA, 8009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) kTimestampB); 8109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) EXPECT_EQ(2, buffer->frame_count()); // now 4 channels of 32-bit data 8209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)} 8309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) 8409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)TEST(AudioBufferTest, ReadU8) { 8509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) const ChannelLayout channel_layout = CHANNEL_LAYOUT_4_0; 8609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) const int channels = ChannelLayoutToChannelCount(channel_layout); 8709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) const int frames = 4; 8809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) const base::TimeDelta start_time; 89926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) const base::TimeDelta duration = base::TimeDelta::FromSeconds(frames); 90926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) scoped_refptr<AudioBuffer> buffer = MakeAudioBuffer<uint8>(kSampleFormatU8, 91926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) channel_layout, 92926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) channels, 93926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) kSampleRate, 9409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) 128, 95926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 1, 96926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) frames, 97926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) start_time, 98926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) duration); 99926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 10009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) // Read all 4 frames from the buffer. Data is interleaved, so ch[0] should be 101926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) // 128, 132, 136, 140, other channels similar. However, values are converted 102926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) // from [0, 255] to [-1.0, 1.0] with a bias of 128. Thus the first buffer 103926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) // value should be 0.0, then 1/127, 2/127, etc. 104926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100); 105926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) buffer->ReadFrames(frames, 0, 0, bus.get()); 106926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) VerifyResult(bus->channel(0), frames, 0.0f, 4.0f / 127.0f); 107926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) VerifyResult(bus->channel(1), frames, 1.0f / 127.0f, 4.0f / 127.0f); 108926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) VerifyResult(bus->channel(2), frames, 2.0f / 127.0f, 4.0f / 127.0f); 109926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) VerifyResult(bus->channel(3), frames, 3.0f / 127.0f, 4.0f / 127.0f); 110926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)} 111926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 112926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)TEST(AudioBufferTest, ReadS16) { 113926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) const ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO; 114926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) const int channels = ChannelLayoutToChannelCount(channel_layout); 115926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) const int frames = 10; 116926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) const base::TimeDelta start_time; 117926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) const base::TimeDelta duration = base::TimeDelta::FromSeconds(frames); 118926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) scoped_refptr<AudioBuffer> buffer = MakeAudioBuffer<int16>(kSampleFormatS16, 119926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) channel_layout, 120926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) channels, 121926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) kSampleRate, 122926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 1, 123926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 1, 124926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) frames, 125926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) start_time, 126926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) duration); 127926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 128926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) // Read 6 frames from the buffer. Data is interleaved, so ch[0] should be 1, 129926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) // 3, 5, 7, 9, 11, and ch[1] should be 2, 4, 6, 8, 10, 12. Data is converted 130926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) // to float from -1.0 to 1.0 based on int16 range. 131926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100); 132926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) buffer->ReadFrames(6, 0, 0, bus.get()); 133926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) VerifyResult(bus->channel(0), 6, 1.0f / kint16max, 2.0f / kint16max); 134926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) VerifyResult(bus->channel(1), 6, 2.0f / kint16max, 2.0f / kint16max); 13509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) 136926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) // Now read the same data one frame at a time. 137926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) bus = AudioBus::Create(channels, 100); 138926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) for (int i = 0; i < frames; ++i) { 139926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) buffer->ReadFrames(1, i, i, bus.get()); 140926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) } 141926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) VerifyResult(bus->channel(0), frames, 1.0f / kint16max, 2.0f / kint16max); 142926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) VerifyResult(bus->channel(1), frames, 2.0f / kint16max, 2.0f / kint16max); 143926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)} 144926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 145926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)TEST(AudioBufferTest, ReadS32) { 146926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) const ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO; 14709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) const int channels = ChannelLayoutToChannelCount(channel_layout); 148926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) const int frames = 6; 14909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) const base::TimeDelta start_time; 150926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) const base::TimeDelta duration = base::TimeDelta::FromSeconds(frames); 15109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) scoped_refptr<AudioBuffer> buffer = MakeAudioBuffer<int32>(kSampleFormatS32, 152926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) channel_layout, 15309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) channels, 15409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) kSampleRate, 15509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) 1, 15609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) 1, 15709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) frames, 15809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) start_time, 159926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) duration); 160926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 161926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) // Read 6 frames from the buffer. Data is interleaved, so ch[0] should be 1, 162926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) // 3, 5, 7, 9, 11, and ch[1] should be 2, 4, 6, 8, 10, 12. Data is converted 163926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) // to float from -1.0 to 1.0 based on int32 range. 16409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100); 165926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) buffer->ReadFrames(frames, 0, 0, bus.get()); 166926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) VerifyResult(bus->channel(0), frames, 1.0f / kint32max, 2.0f / kint32max); 167926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) VerifyResult(bus->channel(1), frames, 2.0f / kint32max, 2.0f / kint32max); 168926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 169926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) // Now read 2 frames starting at frame offset 3. ch[0] should be 7, 9, and 17009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) // ch[1] should be 8, 10. 171926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) buffer->ReadFrames(2, 3, 0, bus.get()); 172926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) VerifyResult(bus->channel(0), 2, 7.0f / kint32max, 2.0f / kint32max); 173926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) VerifyResult(bus->channel(1), 2, 8.0f / kint32max, 2.0f / kint32max); 174926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)} 175926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 176926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)TEST(AudioBufferTest, ReadF32) { 177926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) const ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO; 178926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) const int channels = ChannelLayoutToChannelCount(channel_layout); 179926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) const int frames = 20; 180926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) const base::TimeDelta start_time; 181926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) const base::TimeDelta duration = base::TimeDelta::FromSeconds(frames); 182926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) scoped_refptr<AudioBuffer> buffer = MakeAudioBuffer<float>(kSampleFormatF32, 183926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) channel_layout, 184 channels, 185 kSampleRate, 186 1.0f, 187 1.0f, 188 frames, 189 start_time, 190 duration); 191 192 // Read first 10 frames from the buffer. F32 is interleaved, so ch[0] should 193 // be 1, 3, 5, ... and ch[1] should be 2, 4, 6, ... 194 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100); 195 buffer->ReadFrames(10, 0, 0, bus.get()); 196 VerifyResult(bus->channel(0), 10, 1.0f, 2.0f); 197 VerifyResult(bus->channel(1), 10, 2.0f, 2.0f); 198 199 // Read second 10 frames. 200 bus = AudioBus::Create(channels, 100); 201 buffer->ReadFrames(10, 10, 0, bus.get()); 202 VerifyResult(bus->channel(0), 10, 21.0f, 2.0f); 203 VerifyResult(bus->channel(1), 10, 22.0f, 2.0f); 204} 205 206TEST(AudioBufferTest, ReadS16Planar) { 207 const ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO; 208 const int channels = ChannelLayoutToChannelCount(channel_layout); 209 const int frames = 20; 210 const base::TimeDelta start_time; 211 const base::TimeDelta duration = base::TimeDelta::FromSeconds(frames); 212 scoped_refptr<AudioBuffer> buffer = 213 MakeAudioBuffer<int16>(kSampleFormatPlanarS16, 214 channel_layout, 215 channels, 216 kSampleRate, 217 1, 218 1, 219 frames, 220 start_time, 221 duration); 222 223 // Read 6 frames from the buffer. Data is planar, so ch[0] should be 1, 2, 3, 224 // 4, 5, 6, and ch[1] should be 21, 22, 23, 24, 25, 26. Data is converted to 225 // float from -1.0 to 1.0 based on int16 range. 226 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100); 227 buffer->ReadFrames(6, 0, 0, bus.get()); 228 VerifyResult(bus->channel(0), 6, 1.0f / kint16max, 1.0f / kint16max); 229 VerifyResult(bus->channel(1), 6, 21.0f / kint16max, 1.0f / kint16max); 230 231 // Read all the frames backwards, one by one. ch[0] should be 20, 19, ... 232 bus = AudioBus::Create(channels, 100); 233 for (int i = 0; i < frames; ++i) { 234 buffer->ReadFrames(1, frames - i - 1, i, bus.get()); 235 } 236 VerifyResult(bus->channel(0), frames, 20.0f / kint16max, -1.0f / kint16max); 237 VerifyResult(bus->channel(1), frames, 40.0f / kint16max, -1.0f / kint16max); 238 239 // Read 0 frames with different offsets. Existing data in AudioBus should be 240 // unchanged. 241 buffer->ReadFrames(0, 0, 0, bus.get()); 242 buffer->ReadFrames(0, 0, 10, bus.get()); 243 buffer->ReadFrames(0, 10, 0, bus.get()); 244 VerifyResult(bus->channel(0), frames, 20.0f / kint16max, -1.0f / kint16max); 245 VerifyResult(bus->channel(1), frames, 40.0f / kint16max, -1.0f / kint16max); 246} 247 248TEST(AudioBufferTest, ReadF32Planar) { 249 const ChannelLayout channel_layout = CHANNEL_LAYOUT_4_0; 250 const int channels = ChannelLayoutToChannelCount(channel_layout); 251 const int frames = 100; 252 const base::TimeDelta start_time; 253 const base::TimeDelta duration = base::TimeDelta::FromSeconds(frames); 254 scoped_refptr<AudioBuffer> buffer = 255 MakeAudioBuffer<float>(kSampleFormatPlanarF32, 256 channel_layout, 257 channels, 258 kSampleRate, 259 1.0f, 260 1.0f, 261 frames, 262 start_time, 263 duration); 264 265 // Read all 100 frames from the buffer. F32 is planar, so ch[0] should be 1, 266 // 2, 3, 4, ..., ch[1] should be 101, 102, 103, ..., and so on for all 4 267 // channels. 268 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100); 269 buffer->ReadFrames(frames, 0, 0, bus.get()); 270 VerifyResult(bus->channel(0), frames, 1.0f, 1.0f); 271 VerifyResult(bus->channel(1), frames, 101.0f, 1.0f); 272 VerifyResult(bus->channel(2), frames, 201.0f, 1.0f); 273 VerifyResult(bus->channel(3), frames, 301.0f, 1.0f); 274 275 // Now read 20 frames from the middle of the buffer. 276 bus = AudioBus::Create(channels, 100); 277 buffer->ReadFrames(20, 50, 0, bus.get()); 278 VerifyResult(bus->channel(0), 20, 51.0f, 1.0f); 279 VerifyResult(bus->channel(1), 20, 151.0f, 1.0f); 280 VerifyResult(bus->channel(2), 20, 251.0f, 1.0f); 281 VerifyResult(bus->channel(3), 20, 351.0f, 1.0f); 282} 283 284TEST(AudioBufferTest, EmptyBuffer) { 285 const ChannelLayout channel_layout = CHANNEL_LAYOUT_4_0; 286 const int channels = ChannelLayoutToChannelCount(channel_layout); 287 const int frames = 100; 288 const base::TimeDelta start_time; 289 const base::TimeDelta duration = base::TimeDelta::FromSeconds(frames); 290 scoped_refptr<AudioBuffer> buffer = AudioBuffer::CreateEmptyBuffer( 291 channel_layout, channels, kSampleRate, frames, start_time, duration); 292 EXPECT_EQ(frames, buffer->frame_count()); 293 EXPECT_EQ(start_time, buffer->timestamp()); 294 EXPECT_EQ(frames, buffer->duration().InSeconds()); 295 EXPECT_FALSE(buffer->end_of_stream()); 296 297 // Read all 100 frames from the buffer. All data should be 0. 298 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100); 299 buffer->ReadFrames(frames, 0, 0, bus.get()); 300 VerifyResult(bus->channel(0), frames, 0.0f, 0.0f); 301 VerifyResult(bus->channel(1), frames, 0.0f, 0.0f); 302 VerifyResult(bus->channel(2), frames, 0.0f, 0.0f); 303 VerifyResult(bus->channel(3), frames, 0.0f, 0.0f); 304} 305 306TEST(AudioBufferTest, Trim) { 307 const ChannelLayout channel_layout = CHANNEL_LAYOUT_4_0; 308 const int channels = ChannelLayoutToChannelCount(channel_layout); 309 const int frames = 100; 310 const base::TimeDelta start_time; 311 const base::TimeDelta duration = base::TimeDelta::FromSeconds(frames); 312 scoped_refptr<AudioBuffer> buffer = 313 MakeAudioBuffer<float>(kSampleFormatPlanarF32, 314 channel_layout, 315 channels, 316 kSampleRate, 317 1.0f, 318 1.0f, 319 frames, 320 start_time, 321 duration); 322 EXPECT_EQ(frames, buffer->frame_count()); 323 EXPECT_EQ(start_time, buffer->timestamp()); 324 EXPECT_EQ(frames, buffer->duration().InSeconds()); 325 326 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100); 327 buffer->ReadFrames(20, 0, 0, bus.get()); 328 VerifyResult(bus->channel(0), 20, 1.0f, 1.0f); 329 330 // Trim off 10 frames from the start. 331 buffer->TrimStart(10); 332 EXPECT_EQ(buffer->frame_count(), frames - 10); 333 EXPECT_EQ(buffer->timestamp(), start_time + base::TimeDelta::FromSeconds(10)); 334 EXPECT_EQ(buffer->duration(), base::TimeDelta::FromSeconds(90)); 335 buffer->ReadFrames(20, 0, 0, bus.get()); 336 VerifyResult(bus->channel(0), 20, 11.0f, 1.0f); 337 338 // Trim off 10 frames from the end. 339 buffer->TrimEnd(10); 340 EXPECT_EQ(buffer->frame_count(), frames - 20); 341 EXPECT_EQ(buffer->timestamp(), start_time + base::TimeDelta::FromSeconds(10)); 342 EXPECT_EQ(buffer->duration(), base::TimeDelta::FromSeconds(80)); 343 buffer->ReadFrames(20, 0, 0, bus.get()); 344 VerifyResult(bus->channel(0), 20, 11.0f, 1.0f); 345 346 // Trim off 50 more from the start. 347 buffer->TrimStart(50); 348 EXPECT_EQ(buffer->frame_count(), frames - 70); 349 EXPECT_EQ(buffer->timestamp(), start_time + base::TimeDelta::FromSeconds(60)); 350 EXPECT_EQ(buffer->duration(), base::TimeDelta::FromSeconds(30)); 351 buffer->ReadFrames(10, 0, 0, bus.get()); 352 VerifyResult(bus->channel(0), 10, 61.0f, 1.0f); 353 354 // Trim off the last 30 frames. 355 buffer->TrimEnd(30); 356 EXPECT_EQ(buffer->frame_count(), 0); 357 EXPECT_EQ(buffer->timestamp(), start_time + base::TimeDelta::FromSeconds(60)); 358 EXPECT_EQ(buffer->duration(), base::TimeDelta::FromSeconds(0)); 359} 360 361} // namespace media 362