mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-01-08 09:37:53 +00:00
965aaebbdb
Instead of getting the loudness data for the whole file in advance, we now get it piece by piece as the sound is streamed. The benefit is that we need to decode the audio just once instead of twice. We no longer need to rewind() the stream when the first decoding is done, this should hopefully fix bug #3453 .
58 lines
1.8 KiB
C++
58 lines
1.8 KiB
C++
#ifndef GAME_SOUND_LOUDNESS_H
|
|
#define GAME_SOUND_LOUDNESS_H
|
|
|
|
#include <vector>
|
|
#include <deque>
|
|
|
|
#include "sound_decoder.hpp"
|
|
|
|
namespace MWSound
|
|
{
|
|
|
|
class Sound_Loudness {
|
|
float mSamplesPerSec;
|
|
int mSampleRate;
|
|
ChannelConfig mChannelConfig;
|
|
SampleType mSampleType;
|
|
|
|
// Loudness sample info
|
|
std::vector<float> mSamples;
|
|
|
|
std::deque<char> mQueue;
|
|
|
|
public:
|
|
/**
|
|
* @param samplesPerSecond How many loudness values per second of audio to compute.
|
|
* @param sampleRate the sample rate of the sound buffer
|
|
* @param chans channel layout of the buffer
|
|
* @param type sample type of the buffer
|
|
*/
|
|
Sound_Loudness(float samplesPerSecond, int sampleRate, ChannelConfig chans, SampleType type)
|
|
: mSamplesPerSec(samplesPerSecond)
|
|
, mSampleRate(sampleRate)
|
|
, mChannelConfig(chans)
|
|
, mSampleType(type)
|
|
{ }
|
|
|
|
/**
|
|
* Analyzes the energy (closely related to loudness) of a sound buffer.
|
|
* The buffer will be divided into segments according to \a valuesPerSecond,
|
|
* and for each segment a loudness value in the range of [0,1] will be computed.
|
|
* The computed values are then added to the mSamples vector. This method should be called continuously
|
|
* with chunks of audio until the whole audio file is processed.
|
|
* If the size of \a data does not exactly fit a number of loudness samples, the remainder
|
|
* will be kept in the mQueue and used in the next call to analyzeLoudness.
|
|
* @param data the sound buffer to analyze, containing raw samples
|
|
*/
|
|
void analyzeLoudness(const std::vector<char>& data);
|
|
|
|
/**
|
|
* Get loudness at a particular time. Before calling this, the stream has to be analyzed up to that point in time (see analyzeLoudness()).
|
|
*/
|
|
float getLoudnessAtTime(float sec) const;
|
|
};
|
|
|
|
}
|
|
|
|
#endif /* GAME_SOUND_LOUDNESS_H */
|