ppsspp/Core/HW/SasAudio.h

250 lines
5.2 KiB
C
Raw Normal View History

2012-12-16 19:40:49 +00:00
// Copyright (c) 2012- PPSSPP Project.
// This program 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, version 2.0 or later versions.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
// This is not really hardware, it's a software audio mixer running on the Media Engine.
// From the perspective of a PSP app though, it might as well be.
#pragma once
#include "../Globals.h"
#include "ChunkFile.h"
2012-12-16 19:40:49 +00:00
enum {
PSP_SAS_VOICES_MAX = 32,
PSP_SAS_PITCH_MIN = 1,
PSP_SAS_PITCH_BASE = 0x1000,
PSP_SAS_PITCH_MAX = 0x4000,
PSP_SAS_VOL_MAX = 0x1000,
PSP_SAS_ADSR_CURVE_MODE_LINEAR_INCREASE = 0,
PSP_SAS_ADSR_CURVE_MODE_LINEAR_DECREASE = 1,
PSP_SAS_ADSR_CURVE_MODE_LINEAR_BENT = 2,
PSP_SAS_ADSR_CURVE_MODE_EXPONENT_DECREASE = 3,
PSP_SAS_ADSR_CURVE_MODE_EXPONENT_INCREASE = 4,
PSP_SAS_ADSR_CURVE_MODE_DIRECT = 5,
PSP_SAS_ADSR_ATTACK = 1,
PSP_SAS_ADSR_DECAY = 2,
PSP_SAS_ADSR_SUSTAIN = 4,
PSP_SAS_ADSR_RELEASE = 8,
PSP_SAS_ENVELOPE_HEIGHT_MAX = 0x40000000,
PSP_SAS_ENVELOPE_FREQ_MAX = 0x7FFFFFFF,
2012-12-16 19:40:49 +00:00
};
struct WaveformEffect
{
int type;
int delay;
int feedback;
int leftVol;
int rightVol;
int isDryOn;
int isWetOn;
};
enum VoiceType {
VOICETYPE_OFF,
VOICETYPE_VAG, // default
VOICETYPE_PCM,
VOICETYPE_NOISE,
VOICETYPE_ATRAC3,
VOICETYPE_TRIWAVE, // are these used? there are functions for them (sceSetTriangularWave)
VOICETYPE_PULSEWAVE,
2012-12-16 19:40:49 +00:00
};
// VAG is a Sony ADPCM audio compression format, which goes all the way back to the PSX.
// It compresses 28 16-bit samples into a block of 16 bytes.
class VagDecoder {
2012-12-16 19:40:49 +00:00
public:
VagDecoder() : data_(0), read_(0) {}
void Start(u32 dataPtr, int vagSize, bool loopEnabled);
2012-12-16 19:40:49 +00:00
void GetSamples(s16 *outSamples, int numSamples);
void DecodeBlock(u8 *&readp);
2012-12-16 19:40:49 +00:00
bool End() const { return end_; }
void DoState(PointerWrap &p);
2012-12-16 19:40:49 +00:00
private:
int samples[28];
2012-12-16 19:40:49 +00:00
int curSample;
u32 data_;
u32 read_;
int curBlock_;
int loopStartBlock_;
int numBlocks_;
2012-12-16 19:40:49 +00:00
// rolling state. start at 0, should probably reset to 0 on loops?
int s_1;
int s_2;
2012-12-16 19:40:49 +00:00
bool loopEnabled_;
bool loopAtNextBlock_;
2012-12-16 19:40:49 +00:00
bool end_;
};
// Max height: 0x40000000 I think
class ADSREnvelope
{
public:
ADSREnvelope();
2012-12-16 19:40:49 +00:00
void SetSimpleEnvelope(u32 ADSREnv1, u32 ADSREnv2);
void WalkCurve(int rate, int type);
void KeyOn();
void KeyOff();
void Step();
int GetHeight() const {
return height_ > PSP_SAS_ENVELOPE_HEIGHT_MAX ? PSP_SAS_ENVELOPE_HEIGHT_MAX : height_;
}
bool HasEnded() const {
return state_ == STATE_OFF;
}
2012-12-16 19:40:49 +00:00
int attackRate;
int decayRate;
int sustainRate;
int releaseRate;
int attackType;
int decayType;
int sustainType;
int sustainLevel;
int releaseType;
void DoState(PointerWrap &p);
private:
enum ADSRState {
STATE_ATTACK,
STATE_DECAY,
STATE_SUSTAIN,
STATE_RELEASE,
STATE_OFF,
};
void SetState(ADSRState state);
ADSRState state_;
int steps_;
s64 height_; // s64 to avoid having to care about overflow when calculatimg. TODO: this should be fine as s32
2012-12-16 19:40:49 +00:00
};
// A SAS voice.
// TODO: Look into pre-decoding the VAG samples on SetVoice instead of decoding them on the fly.
// It's not very likely that games encode VAG dynamically.
struct SasVoice
{
SasVoice()
2013-02-16 12:02:53 +00:00
: playing(false),
paused(false),
on(false),
type(VOICETYPE_OFF),
vagAddr(0),
vagSize(0),
pcmAddr(0),
pcmSize(0),
sampleRate(44100),
sampleFrac(0),
pitch(PSP_SAS_PITCH_BASE),
loop(true), // true = ignore VAG loop , false = process VAG loop
noiseFreq(0),
volumeLeft(0),
volumeRight(0),
volumeLeftSend(0),
2013-02-16 12:02:53 +00:00
volumeRightSend(0),
effectLeft(0),
effectRight(0) {
memset(resampleHist, 0, sizeof(resampleHist));
}
void Reset();
void KeyOn();
void KeyOff();
void ChangedParams(bool changedVag);
void DoState(PointerWrap &p);
2012-12-16 19:40:49 +00:00
bool playing;
bool paused; // a voice can be playing AND paused. In that case, it won't play.
bool on; // key-on, key-off.
2012-12-16 19:40:49 +00:00
VoiceType type;
u32 vagAddr;
int vagSize;
u32 pcmAddr;
int pcmSize;
int sampleRate;
int sampleFrac;
2012-12-16 19:40:49 +00:00
int pitch;
bool loop;
2012-12-16 19:40:49 +00:00
int noiseFreq;
int volumeLeft;
int volumeRight;
int volumeLeftSend; // volume to "Send" (audio-lingo) to the effects processing engine, like reverb
int volumeRightSend;
2013-01-27 14:34:14 +00:00
int effectLeft;
int effectRight;
s16 resampleHist[2];
2012-12-16 19:40:49 +00:00
ADSREnvelope envelope;
VagDecoder vag;
};
class SasInstance
{
public:
SasInstance();
~SasInstance();
2012-12-16 19:40:49 +00:00
void ClearGrainSize();
void SetGrainSize(int newGrainSize);
2012-12-16 19:40:49 +00:00
int GetGrainSize() const { return grainSize; }
int maxVoices;
int sampleRate;
int outputMode;
int *mixBuffer;
int *sendBuffer;
s16 *resampleBuffer;
2012-12-16 19:40:49 +00:00
FILE *audioDump;
2012-12-16 19:40:49 +00:00
void Mix(u32 outAddr);
void DoState(PointerWrap &p);
SasVoice voices[PSP_SAS_VOICES_MAX];
WaveformEffect waveformEffect;
2012-12-16 19:40:49 +00:00
private:
int grainSize;
};