Gb_Apu.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Nintendo Game Boy PAPU sound chip emulator
  2. // Gb_Snd_Emu 0.1.5
  3. #ifndef GB_APU_H
  4. #define GB_APU_H
  5. #include "Gb_Oscs.h"
  6. class Gb_Apu {
  7. public:
  8. // Set overall volume of all oscillators, where 1.0 is full volume
  9. void volume( double );
  10. // Set treble equalization
  11. void treble_eq( const blip_eq_t& );
  12. // Outputs can be assigned to a single buffer for mono output, or to three
  13. // buffers for stereo output (using Stereo_Buffer to do the mixing).
  14. // Assign all oscillator outputs to specified buffer(s). If buffer
  15. // is NULL, silences all oscillators.
  16. void output( Blip_Buffer* mono );
  17. void output( Blip_Buffer* center, Blip_Buffer* left, Blip_Buffer* right );
  18. // Assign single oscillator output to buffer(s). Valid indicies are 0 to 3,
  19. // which refer to Square 1, Square 2, Wave, and Noise. If buffer is NULL,
  20. // silences oscillator.
  21. enum { osc_count = 4 };
  22. void osc_output( int index, Blip_Buffer* mono );
  23. void osc_output( int index, Blip_Buffer* center, Blip_Buffer* left, Blip_Buffer* right );
  24. // Reset oscillators and internal state
  25. void reset();
  26. // Reads and writes at addr must satisfy start_addr <= addr <= end_addr
  27. enum { start_addr = 0xFF10 };
  28. enum { end_addr = 0xFF3F };
  29. enum { register_count = end_addr - start_addr + 1 };
  30. // Write 'data' to address at specified time
  31. void write_register( blip_time_t, unsigned addr, int data );
  32. // Read from address at specified time
  33. int read_register( blip_time_t, unsigned addr );
  34. // Run all oscillators up to specified time, end current time frame, then
  35. // start a new frame at time 0.
  36. void end_frame( blip_time_t );
  37. void set_tempo( double );
  38. public:
  39. Gb_Apu();
  40. private:
  41. // noncopyable
  42. Gb_Apu( const Gb_Apu& );
  43. Gb_Apu& operator = ( const Gb_Apu& );
  44. Gb_Osc* oscs [osc_count];
  45. blip_time_t next_frame_time;
  46. blip_time_t last_time;
  47. blip_time_t frame_period;
  48. double volume_unit;
  49. int frame_count;
  50. Gb_Square square1;
  51. Gb_Square square2;
  52. Gb_Wave wave;
  53. Gb_Noise noise;
  54. BOOST::uint8_t regs [register_count];
  55. Gb_Square::Synth square_synth; // used by squares
  56. Gb_Wave::Synth other_synth; // used by wave and noise
  57. void update_volume();
  58. void run_until( blip_time_t );
  59. void write_osc( int index, int reg, int data );
  60. };
  61. inline void Gb_Apu::output( Blip_Buffer* b ) { output( b, b, b ); }
  62. inline void Gb_Apu::osc_output( int i, Blip_Buffer* b ) { osc_output( i, b, b, b ); }
  63. inline void Gb_Apu::volume( double vol )
  64. {
  65. volume_unit = 0.60 / osc_count / 15 /*steps*/ / 2 /*?*/ / 8 /*master vol range*/ * vol;
  66. update_volume();
  67. }
  68. #endif