2011-08-20 14:40:44 +00:00
|
|
|
#ifdef NALL_DSP_INTERNAL_HPP
|
2011-08-14 10:34:11 +00:00
|
|
|
|
2011-08-20 14:40:44 +00:00
|
|
|
#include <math.h>
|
2011-08-14 10:34:11 +00:00
|
|
|
#include <nall/stdint.hpp>
|
|
|
|
|
|
|
|
namespace nall {
|
|
|
|
|
2011-09-23 11:13:57 +00:00
|
|
|
//precision: can be float, double or long double
|
|
|
|
#define real float
|
|
|
|
|
|
|
|
struct DSP;
|
|
|
|
|
|
|
|
struct Resampler {
|
|
|
|
DSP &dsp;
|
|
|
|
real frequency;
|
|
|
|
|
|
|
|
virtual void setFrequency() = 0;
|
|
|
|
virtual void clear() = 0;
|
|
|
|
virtual void sample() = 0;
|
|
|
|
Resampler(DSP &dsp) : dsp(dsp) {}
|
|
|
|
};
|
|
|
|
|
2011-08-20 14:40:44 +00:00
|
|
|
struct DSP {
|
2011-09-23 11:13:57 +00:00
|
|
|
enum class ResampleEngine : unsigned {
|
|
|
|
Nearest,
|
2011-08-20 14:40:44 +00:00
|
|
|
Linear,
|
|
|
|
Cosine,
|
|
|
|
Cubic,
|
|
|
|
Hermite,
|
|
|
|
Average,
|
|
|
|
};
|
2011-08-14 10:34:11 +00:00
|
|
|
|
2011-09-16 11:44:07 +00:00
|
|
|
inline void setChannels(unsigned channels);
|
2011-08-20 14:40:44 +00:00
|
|
|
inline void setPrecision(unsigned precision);
|
2011-09-23 11:13:57 +00:00
|
|
|
inline void setFrequency(real frequency); //inputFrequency
|
|
|
|
inline void setVolume(real volume);
|
|
|
|
inline void setBalance(real balance);
|
2011-08-20 14:40:44 +00:00
|
|
|
|
2011-09-23 11:13:57 +00:00
|
|
|
inline void setResampler(ResampleEngine resamplingEngine);
|
|
|
|
inline void setResamplerFrequency(real frequency); //outputFrequency
|
2011-08-14 10:34:11 +00:00
|
|
|
|
2011-09-16 11:44:07 +00:00
|
|
|
inline void sample(signed channel[]);
|
2011-08-14 10:34:11 +00:00
|
|
|
inline bool pending();
|
2011-09-16 11:44:07 +00:00
|
|
|
inline void read(signed channel[]);
|
2011-08-14 10:34:11 +00:00
|
|
|
|
|
|
|
inline void clear();
|
2011-08-20 14:40:44 +00:00
|
|
|
inline DSP();
|
|
|
|
inline ~DSP();
|
2011-08-14 10:34:11 +00:00
|
|
|
|
|
|
|
protected:
|
2011-09-23 11:13:57 +00:00
|
|
|
friend class ResampleNearest;
|
|
|
|
friend class ResampleLinear;
|
|
|
|
friend class ResampleCosine;
|
|
|
|
friend class ResampleCubic;
|
|
|
|
friend class ResampleAverage;
|
|
|
|
friend class ResampleHermite;
|
|
|
|
|
2011-08-14 10:34:11 +00:00
|
|
|
struct Settings {
|
2011-09-16 11:44:07 +00:00
|
|
|
unsigned channels;
|
2011-08-14 10:34:11 +00:00
|
|
|
unsigned precision;
|
2011-09-23 11:13:57 +00:00
|
|
|
real frequency;
|
|
|
|
real volume;
|
|
|
|
real balance;
|
2011-08-14 10:34:11 +00:00
|
|
|
|
|
|
|
//internal
|
2011-09-23 11:13:57 +00:00
|
|
|
real intensity;
|
|
|
|
real intensityInverse;
|
|
|
|
} settings;
|
2011-08-14 10:34:11 +00:00
|
|
|
|
2011-09-23 11:13:57 +00:00
|
|
|
Resampler *resampler;
|
|
|
|
inline void write(real channel[]);
|
2011-08-14 10:34:11 +00:00
|
|
|
|
2011-08-20 14:40:44 +00:00
|
|
|
#include "buffer.hpp"
|
|
|
|
Buffer buffer;
|
|
|
|
Buffer output;
|
2011-08-14 10:34:11 +00:00
|
|
|
|
2011-08-20 14:40:44 +00:00
|
|
|
inline void adjustVolume();
|
|
|
|
inline void adjustBalance();
|
|
|
|
inline signed clamp(const unsigned bits, const signed x);
|
|
|
|
};
|
2011-08-14 10:34:11 +00:00
|
|
|
|
2011-09-23 11:13:57 +00:00
|
|
|
#include "resample/nearest.hpp"
|
|
|
|
#include "resample/linear.hpp"
|
|
|
|
#include "resample/cosine.hpp"
|
|
|
|
#include "resample/cubic.hpp"
|
|
|
|
#include "resample/hermite.hpp"
|
|
|
|
#include "resample/average.hpp"
|
2011-08-20 14:40:44 +00:00
|
|
|
#include "settings.hpp"
|
2011-08-14 10:34:11 +00:00
|
|
|
|
2011-09-16 11:44:07 +00:00
|
|
|
void DSP::sample(signed channel[]) {
|
|
|
|
for(unsigned c = 0; c < settings.channels; c++) {
|
2011-09-23 11:13:57 +00:00
|
|
|
buffer.write(c) = (real)channel[c] * settings.intensityInverse;
|
2011-09-16 11:44:07 +00:00
|
|
|
}
|
2011-08-14 10:34:11 +00:00
|
|
|
buffer.wroffset++;
|
2011-09-23 11:13:57 +00:00
|
|
|
resampler->sample();
|
2011-08-14 10:34:11 +00:00
|
|
|
}
|
|
|
|
|
2011-08-20 14:40:44 +00:00
|
|
|
bool DSP::pending() {
|
2011-08-14 10:34:11 +00:00
|
|
|
return output.rdoffset != output.wroffset;
|
|
|
|
}
|
|
|
|
|
2011-09-16 11:44:07 +00:00
|
|
|
void DSP::read(signed channel[]) {
|
2011-08-20 14:40:44 +00:00
|
|
|
adjustVolume();
|
|
|
|
adjustBalance();
|
2011-08-14 10:34:11 +00:00
|
|
|
|
2011-09-16 11:44:07 +00:00
|
|
|
for(unsigned c = 0; c < settings.channels; c++) {
|
Update to v082r16 release.
byuu says:
Binary output is once again called bsnes. No versioning on the title
without a system cartridge loaded. Still saving config files to
.config/batch for now.
Finally fixed NES APU frame IRQ clearing on $4015 reads.
Added mouse button/axis binding through buttons on the input capture
window.
Added advanced settings window with driver selection and focus policy
settings. Will show your default driver properly if none are selected
now, unlike old bsnes.
That exposed a small bug where phoenix isn't removing widgets on
Layout::remove, worked around it for now by hiding the panels. Damn,
sick of working on phoenix.
Added all missing input controllers, which can all now be mapped, and
bound them to the main menu, and added NES support for selecting "no
connected controller."
Added mouse capture and the requisite tools menu option for it.
Added WindowManager class that keeps track of both position and size now
(eg full geometry), so now you can resize your windows and save the
settings, unlike old bsnes.
WindowManager has more stringent geometry checks. The *client area* (not
the window border) can't be below 0,0 or above the width/height of three
30" monitors. If you have 4+ 30" monitors, then fuck you :P
settings.cfg is now also saved, captures all currently available
settings. Right now, there's only one path for the file browser to
remember. I will probably make this per-system later.
FileBrowser has been made a bit more friendly. The bottom left tells you
what type of files the list is filtered by (so you see "*.sfc" for
SNES), and the bottom right has an open button that can enter folders or
load files.
Added video shader support.
Fixed nall/dsp variadic-channel support, was only outputting the left
channel.
2011-09-19 12:25:56 +00:00
|
|
|
channel[c] = clamp(settings.precision, output.read(c) * settings.intensity);
|
2011-09-16 11:44:07 +00:00
|
|
|
}
|
2011-08-14 10:34:11 +00:00
|
|
|
output.rdoffset++;
|
|
|
|
}
|
|
|
|
|
2011-09-23 11:13:57 +00:00
|
|
|
void DSP::write(real channel[]) {
|
2011-09-16 11:44:07 +00:00
|
|
|
for(unsigned c = 0; c < settings.channels; c++) {
|
|
|
|
output.write(c) = channel[c];
|
|
|
|
}
|
2011-08-20 14:40:44 +00:00
|
|
|
output.wroffset++;
|
2011-08-14 10:34:11 +00:00
|
|
|
}
|
|
|
|
|
2011-08-20 14:40:44 +00:00
|
|
|
void DSP::adjustVolume() {
|
2011-09-16 11:44:07 +00:00
|
|
|
for(unsigned c = 0; c < settings.channels; c++) {
|
|
|
|
output.read(c) *= settings.volume;
|
|
|
|
}
|
2011-08-14 10:34:11 +00:00
|
|
|
}
|
|
|
|
|
2011-08-20 14:40:44 +00:00
|
|
|
void DSP::adjustBalance() {
|
2011-09-16 11:44:07 +00:00
|
|
|
if(settings.channels != 2) return; //TODO: support > 2 channels
|
2011-08-20 14:40:44 +00:00
|
|
|
if(settings.balance < 0.0) output.read(1) *= 1.0 + settings.balance;
|
|
|
|
if(settings.balance > 0.0) output.read(0) *= 1.0 - settings.balance;
|
2011-08-14 10:34:11 +00:00
|
|
|
}
|
|
|
|
|
2011-08-20 14:40:44 +00:00
|
|
|
signed DSP::clamp(const unsigned bits, const signed x) {
|
2011-08-14 10:34:11 +00:00
|
|
|
const signed b = 1U << (bits - 1);
|
|
|
|
const signed m = (1U << (bits - 1)) - 1;
|
|
|
|
return (x > m) ? m : (x < -b) ? -b : x;
|
|
|
|
}
|
|
|
|
|
2011-08-20 14:40:44 +00:00
|
|
|
void DSP::clear() {
|
|
|
|
buffer.clear();
|
|
|
|
output.clear();
|
2011-09-23 11:13:57 +00:00
|
|
|
resampler->clear();
|
2011-08-14 10:34:11 +00:00
|
|
|
}
|
|
|
|
|
2011-08-20 14:40:44 +00:00
|
|
|
DSP::DSP() {
|
2011-09-23 11:13:57 +00:00
|
|
|
setResampler(ResampleEngine::Hermite);
|
|
|
|
setResamplerFrequency(44100.0);
|
|
|
|
|
2011-09-16 11:44:07 +00:00
|
|
|
setChannels(2);
|
2011-08-20 14:40:44 +00:00
|
|
|
setPrecision(16);
|
|
|
|
setFrequency(44100.0);
|
|
|
|
setVolume(1.0);
|
|
|
|
setBalance(0.0);
|
2011-09-23 11:13:57 +00:00
|
|
|
|
2011-08-14 10:34:11 +00:00
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2011-08-20 14:40:44 +00:00
|
|
|
DSP::~DSP() {
|
2011-09-23 11:13:57 +00:00
|
|
|
if(resampler) delete resampler;
|
2011-08-14 10:34:11 +00:00
|
|
|
}
|
|
|
|
|
2011-09-23 11:13:57 +00:00
|
|
|
#undef real
|
|
|
|
|
2011-08-14 10:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|