Path: blob/master/tests/scene/test_audio_stream_wav.h
10277 views
/**************************************************************************/1/* test_audio_stream_wav.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/math/math_defs.h"33#include "core/math/math_funcs.h"34#include "scene/resources/audio_stream_wav.h"3536#include "tests/test_macros.h"3738namespace TestAudioStreamWAV {3940// Default wav rate for test cases.41constexpr float WAV_RATE = 44100;42/* Default wav count for test cases. 1 second of audio is used so that the file can be listened43to manually if needed. */44constexpr int WAV_COUNT = WAV_RATE;4546float gen_wav(float frequency, float wav_rate, int wav_number) {47// formula for generating a sin wave with given frequency.48return Math::sin((Math::TAU * frequency / wav_rate) * wav_number);49}5051/* Generates a 440Hz sin wave in channel 0 (mono channel or left stereo channel)52* and a 261.63Hz wave in channel 1 (right stereo channel).53* These waves correspond to the music notes A4 and C4 respectively.54*/55Vector<uint8_t> gen_pcm8_test(float wav_rate, int wav_count, bool stereo) {56Vector<uint8_t> buffer;57buffer.resize(stereo ? wav_count * 2 : wav_count);5859uint8_t *write_ptr = buffer.ptrw();60for (int i = 0; i < buffer.size(); i++) {61float wav;62if (stereo) {63if (i % 2 == 0) {64wav = gen_wav(440, wav_rate, i / 2);65} else {66wav = gen_wav(261.63, wav_rate, i / 2);67}68} else {69wav = gen_wav(440, wav_rate, i);70}7172// Map sin wave to full range of 8-bit values.73uint8_t wav_8bit = Math::fast_ftoi(((wav + 1) / 2) * UINT8_MAX);74// Unlike the .wav format, AudioStreamWAV expects signed 8-bit wavs.75uint8_t wav_8bit_signed = wav_8bit - (INT8_MAX + 1);76write_ptr[i] = wav_8bit_signed;77}7879return buffer;80}8182// Same as gen_pcm8_test but with 16-bit wavs.83Vector<uint8_t> gen_pcm16_test(float wav_rate, int wav_count, bool stereo) {84Vector<uint8_t> buffer;85buffer.resize(stereo ? wav_count * 4 : wav_count * 2);8687uint8_t *write_ptr = buffer.ptrw();88for (int i = 0; i < buffer.size() / 2; i++) {89float wav;90if (stereo) {91if (i % 2 == 0) {92wav = gen_wav(440, wav_rate, i / 2);93} else {94wav = gen_wav(261.63, wav_rate, i / 2);95}96} else {97wav = gen_wav(440, wav_rate, i);98}99100// Map sin wave to full range of 16-bit values.101uint16_t wav_16bit = Math::fast_ftoi(((wav + 1) / 2) * UINT16_MAX);102// The .wav format expects wavs larger than 8 bits to be signed.103uint16_t wav_16bit_signed = wav_16bit - (INT16_MAX + 1);104encode_uint16(wav_16bit_signed, write_ptr + (i * 2));105}106107return buffer;108}109110void run_test(String file_name, AudioStreamWAV::Format data_format, bool stereo, float wav_rate, float wav_count) {111String save_path = TestUtils::get_temp_path(file_name);112113Vector<uint8_t> test_data;114if (data_format == AudioStreamWAV::FORMAT_8_BITS) {115test_data = gen_pcm8_test(wav_rate, wav_count, stereo);116} else {117test_data = gen_pcm16_test(wav_rate, wav_count, stereo);118}119120Ref<AudioStreamWAV> stream = memnew(AudioStreamWAV);121stream->set_mix_rate(wav_rate);122CHECK(stream->get_mix_rate() == wav_rate);123124stream->set_format(data_format);125CHECK(stream->get_format() == data_format);126127stream->set_stereo(stereo);128CHECK(stream->is_stereo() == stereo);129130stream->set_data(test_data);131CHECK(stream->get_data() == test_data);132133SUBCASE("Stream length is computed properly") {134CHECK(stream->get_length() == doctest::Approx(double(wav_count / wav_rate)));135}136137SUBCASE("Stream can be saved as .wav") {138REQUIRE(stream->save_to_wav(save_path) == OK);139140Error error;141Ref<FileAccess> wav_file = FileAccess::open(save_path, FileAccess::READ, &error);142REQUIRE(error == OK);143144Dictionary options;145Ref<AudioStreamWAV> loaded_stream = AudioStreamWAV::load_from_file(save_path, options);146147CHECK(loaded_stream->get_format() == stream->get_format());148CHECK(loaded_stream->get_loop_mode() == stream->get_loop_mode());149CHECK(loaded_stream->get_loop_begin() == stream->get_loop_begin());150CHECK(loaded_stream->get_loop_end() == stream->get_loop_end());151CHECK(loaded_stream->get_mix_rate() == stream->get_mix_rate());152CHECK(loaded_stream->is_stereo() == stream->is_stereo());153CHECK(loaded_stream->get_length() == stream->get_length());154CHECK(loaded_stream->is_monophonic() == stream->is_monophonic());155CHECK(loaded_stream->get_data() == stream->get_data());156}157}158159TEST_CASE("[Audio][AudioStreamWAV] Mono PCM8 format") {160run_test("test_pcm8_mono.wav", AudioStreamWAV::FORMAT_8_BITS, false, WAV_RATE, WAV_COUNT);161}162163TEST_CASE("[Audio][AudioStreamWAV] Mono PCM16 format") {164run_test("test_pcm16_mono.wav", AudioStreamWAV::FORMAT_16_BITS, false, WAV_RATE, WAV_COUNT);165}166167TEST_CASE("[Audio][AudioStreamWAV] Stereo PCM8 format") {168run_test("test_pcm8_stereo.wav", AudioStreamWAV::FORMAT_8_BITS, true, WAV_RATE, WAV_COUNT);169}170171TEST_CASE("[Audio][AudioStreamWAV] Stereo PCM16 format") {172run_test("test_pcm16_stereo.wav", AudioStreamWAV::FORMAT_16_BITS, true, WAV_RATE, WAV_COUNT);173}174175TEST_CASE("[Audio][AudioStreamWAV] Alternate mix rate") {176run_test("test_pcm16_stereo_38000Hz.wav", AudioStreamWAV::FORMAT_16_BITS, true, 38000, 38000);177}178179TEST_CASE("[Audio][AudioStreamWAV] save_to_wav() adds '.wav' file extension automatically") {180String save_path = TestUtils::get_temp_path("test_wav_extension");181Vector<uint8_t> test_data = gen_pcm8_test(WAV_RATE, WAV_COUNT, false);182Ref<AudioStreamWAV> stream = memnew(AudioStreamWAV);183stream->set_data(test_data);184185REQUIRE(stream->save_to_wav(save_path) == OK);186Error error;187Ref<FileAccess> wav_file = FileAccess::open(save_path + ".wav", FileAccess::READ, &error);188CHECK(error == OK);189}190191TEST_CASE("[Audio][AudioStreamWAV] Default values") {192Ref<AudioStreamWAV> stream = memnew(AudioStreamWAV);193CHECK(stream->get_format() == AudioStreamWAV::FORMAT_8_BITS);194CHECK(stream->get_loop_mode() == AudioStreamWAV::LOOP_DISABLED);195CHECK(stream->get_loop_begin() == 0);196CHECK(stream->get_loop_end() == 0);197CHECK(stream->get_mix_rate() == 44100);198CHECK(stream->is_stereo() == false);199CHECK(stream->get_length() == 0);200CHECK(stream->is_monophonic() == false);201CHECK(stream->get_data() == Vector<uint8_t>{});202CHECK(stream->get_stream_name() == "");203}204205TEST_CASE("[Audio][AudioStreamWAV] Save empty file") {206run_test("test_empty.wav", AudioStreamWAV::FORMAT_8_BITS, false, WAV_RATE, 0);207}208209TEST_CASE("[Audio][AudioStreamWAV] Saving IMA ADPCM is not supported") {210String save_path = TestUtils::get_temp_path("test_adpcm.wav");211Ref<AudioStreamWAV> stream = memnew(AudioStreamWAV);212stream->set_format(AudioStreamWAV::FORMAT_IMA_ADPCM);213ERR_PRINT_OFF;214CHECK(stream->save_to_wav(save_path) == ERR_UNAVAILABLE);215ERR_PRINT_ON;216}217218} // namespace TestAudioStreamWAV219220221