Paula.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Paula.h
  3. * -------
  4. * Purpose: Emulating the Amiga's sound chip, Paula, by implementing resampling using band-limited steps (BLEPs)
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * Antti S. Lankila
  8. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  9. */
  10. #pragma once
  11. #include "openmpt/all/BuildSettings.hpp"
  12. #include "Snd_defs.h"
  13. #include "Mixer.h"
  14. OPENMPT_NAMESPACE_BEGIN
  15. namespace Paula
  16. {
  17. inline constexpr int PAULA_HZ = 3546895;
  18. inline constexpr int MINIMUM_INTERVAL = 4; // Tradeoff between quality and speed (lower = less aliasing)
  19. inline constexpr int BLEP_SCALE = 17; // TODO: Should be 1 for float mixer
  20. inline constexpr int BLEP_SIZE = 2048;
  21. using BlepArray = std::array<mixsample_t, BLEP_SIZE>;
  22. class BlepTables
  23. {
  24. enum AmigaFilter
  25. {
  26. A500Off = 0,
  27. A500On,
  28. A1200Off,
  29. A1200On,
  30. Unfiltered,
  31. NumFilterTypes
  32. };
  33. std::array<Paula::BlepArray, AmigaFilter::NumFilterTypes> WinSincIntegral;
  34. public:
  35. void InitTables();
  36. const Paula::BlepArray &GetAmigaTable(Resampling::AmigaFilter amigaType, bool enableFilter) const;
  37. };
  38. class State
  39. {
  40. // MAX_BLEPS configures how many BLEPs (volume steps) are being kept track of per channel,
  41. // and thus directly influences how much memory this feature wastes per virtual channel.
  42. // Paula::BLEP_SIZE / Paula::MINIMUM_INTERVAL would be a safe maximum,
  43. // but even a sample (alternating at +1/-1, thus causing a step on every frame) played at 200 kHz,
  44. // which is way out of spec for the Amiga, will only get close to 128 active BLEPs with Paula::MINIMUM_INTERVAL == 4.
  45. // Hence 128 is chosen as a tradeoff between quality and memory consumption.
  46. static constexpr uint16 MAX_BLEPS = 128;
  47. struct Blep
  48. {
  49. int16 level;
  50. uint16 age;
  51. };
  52. public:
  53. SamplePosition remainder, stepRemainder;
  54. int numSteps; // Number of full-length steps
  55. private:
  56. uint16 activeBleps = 0, firstBlep = 0; // Count of simultaneous bleps to keep track of
  57. int16 globalOutputLevel = 0; // The instantenous value of Paula output
  58. Blep blepState[MAX_BLEPS];
  59. public:
  60. State(uint32 sampleRate = 48000);
  61. void Reset();
  62. void InputSample(int16 sample);
  63. int OutputSample(const BlepArray &WinSincIntegral);
  64. void Clock(int cycles);
  65. };
  66. }
  67. OPENMPT_NAMESPACE_END