Move AudioFormat to new header

This commit is contained in:
Ted John 2017-01-02 19:06:23 +00:00
parent 6ae1356af6
commit 8b1590e248
7 changed files with 58 additions and 35 deletions

View File

@ -429,6 +429,7 @@
<ItemGroup>
<ClInclude Include="resources\resource.h" />
<ClInclude Include="src\audio\AudioChannel.h" />
<ClInclude Include="src\audio\AudioFormat.h" />
<ClInclude Include="src\audio\AudioSource.h" />
<ClInclude Include="src\FileClassifier.h" />
<ClInclude Include="src\rct2\addresses.h" />

View File

@ -18,6 +18,7 @@
#include <speex/speex_resampler.h>
#include "../common.h"
#include "AudioFormat.h"
#include "mixer.h"
interface IAudioSource;

48
src/audio/AudioFormat.h Normal file
View File

@ -0,0 +1,48 @@
#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
/*****************************************************************************
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
*
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* A full copy of the GNU General Public License can be found in licence.txt
*****************************************************************************/
#pragma endregion
#pragma once
#include "../common.h"
#include <SDL.h>
/**
* Represents the size, frequency and number of channels for
* an audio stream or buffer.
*/
struct AudioFormat
{
int freq;
SDL_AudioFormat format;
int channels;
int BytesPerSample() const
{
return (SDL_AUDIO_BITSIZE(format)) / 8;
}
};
inline bool operator ==(const AudioFormat& lhs, const AudioFormat& rhs)
{
return lhs.freq == rhs.freq &&
lhs.format == rhs.format &&
lhs.channels == rhs.channels;
}
inline bool operator !=(const AudioFormat& lhs, const AudioFormat& rhs)
{
return !(lhs == rhs);
}

View File

@ -17,6 +17,7 @@
#pragma once
#include "../common.h"
#include "AudioFormat.h"
#include "mixer.h"
/**

View File

@ -17,6 +17,7 @@
#include "../core/Math.hpp"
#include "../core/Memory.hpp"
#include "AudioSource.h"
#include "mixer.h"
#pragma pack(push, 1)
struct WaveFormatEx
@ -158,9 +159,7 @@ public:
bool Convert(const AudioFormat * format)
{
if (_format.format != format->format ||
_format.channels != format->channels ||
_format.freq != format->freq)
if (*format != _format)
{
SDL_AudioCVT cvt;
if (SDL_BuildAudioCVT(&cvt, _format.format, _format.channels, _format.freq, format->format, format->channels, format->freq) >= 0)

View File

@ -254,9 +254,6 @@ private:
_adjust_music_vol = powf(_setting_music_vol / 100.f, 10.f / 6.f);
}
AudioFormat streamformat = channel->GetFormat();
SDL_AudioCVT cvt;
cvt.len_ratio = 1;
int sampleSize = _format.channels * _format.BytesPerSample();
int samples = length / sampleSize;
double rate = 1;
@ -265,8 +262,12 @@ private:
rate = channel->GetRate();
}
int samplestoread = (int)(samples * rate);
bool mustConvert = false;
if (MustConvert(&streamformat))
SDL_AudioCVT cvt;
cvt.len_ratio = 1;
AudioFormat streamformat = channel->GetFormat();
if (streamformat != _format)
{
if (SDL_BuildAudioCVT(&cvt, streamformat.format, streamformat.channels, streamformat.freq, _format.format, _format.channels, _format.freq) == -1)
{
@ -486,17 +487,6 @@ private:
}
}
bool MustConvert(const AudioFormat * sourceFormat)
{
if (sourceFormat->format != _format.format ||
sourceFormat->channels != _format.channels ||
sourceFormat->freq != _format.freq)
{
return true;
}
return false;
}
bool Convert(SDL_AudioCVT * cvt, const void * src, size_t len)
{
// tofix: there seems to be an issue with converting audio using SDL_ConvertAudio in the callback vs preconverted, can cause pops and static depending on sample rate and channels

View File

@ -17,7 +17,6 @@
#ifndef _MIXER_H_
#define _MIXER_H_
#include <SDL.h>
#include "../common.h"
#ifdef __cplusplus
@ -46,22 +45,6 @@ enum MIXER_GROUP
interface IAudioSource;
interface IAudioChannel;
/**
* Represents the size, frequency and number of channels for
* an audio stream or buffer.
*/
struct AudioFormat
{
int freq;
SDL_AudioFormat format;
int channels;
int BytesPerSample() const
{
return (SDL_AUDIO_BITSIZE(format)) / 8;
}
};
/**
* Provides an audio stream by mixing multiple audio channels together.
*/