/gme/Nes_Oscs.h

http://game-music-emu.googlecode.com/ · C++ Header · 147 lines · 116 code · 24 blank · 7 comment · 0 complexity · e37b008c4167b5d0cf9be3f9be39c912 MD5 · raw file

  1. // Private oscillators used by Nes_Apu
  2. // Nes_Snd_Emu 0.1.8
  3. #ifndef NES_OSCS_H
  4. #define NES_OSCS_H
  5. #include "blargg_common.h"
  6. #include "Blip_Buffer.h"
  7. class Nes_Apu;
  8. struct Nes_Osc
  9. {
  10. unsigned char regs [4];
  11. bool reg_written [4];
  12. Blip_Buffer* output;
  13. int length_counter;// length counter (0 if unused by oscillator)
  14. int delay; // delay until next (potential) transition
  15. int last_amp; // last amplitude oscillator was outputting
  16. void clock_length( int halt_mask );
  17. int period() const {
  18. return (regs [3] & 7) * 0x100 + (regs [2] & 0xFF);
  19. }
  20. void reset() {
  21. delay = 0;
  22. last_amp = 0;
  23. }
  24. int update_amp( int amp ) {
  25. int delta = amp - last_amp;
  26. last_amp = amp;
  27. return delta;
  28. }
  29. };
  30. struct Nes_Envelope : Nes_Osc
  31. {
  32. int envelope;
  33. int env_delay;
  34. void clock_envelope();
  35. int volume() const;
  36. void reset() {
  37. envelope = 0;
  38. env_delay = 0;
  39. Nes_Osc::reset();
  40. }
  41. };
  42. // Nes_Square
  43. struct Nes_Square : Nes_Envelope
  44. {
  45. enum { negate_flag = 0x08 };
  46. enum { shift_mask = 0x07 };
  47. enum { phase_range = 8 };
  48. int phase;
  49. int sweep_delay;
  50. typedef Blip_Synth<blip_good_quality,1> Synth;
  51. Synth const& synth; // shared between squares
  52. Nes_Square( Synth const* s ) : synth( *s ) { }
  53. void clock_sweep( int adjust );
  54. void run( nes_time_t, nes_time_t );
  55. void reset() {
  56. sweep_delay = 0;
  57. Nes_Envelope::reset();
  58. }
  59. nes_time_t maintain_phase( nes_time_t time, nes_time_t end_time,
  60. nes_time_t timer_period );
  61. };
  62. // Nes_Triangle
  63. struct Nes_Triangle : Nes_Osc
  64. {
  65. enum { phase_range = 16 };
  66. int phase;
  67. int linear_counter;
  68. Blip_Synth<blip_med_quality,1> synth;
  69. int calc_amp() const;
  70. void run( nes_time_t, nes_time_t );
  71. void clock_linear_counter();
  72. void reset() {
  73. linear_counter = 0;
  74. phase = 1;
  75. Nes_Osc::reset();
  76. }
  77. nes_time_t maintain_phase( nes_time_t time, nes_time_t end_time,
  78. nes_time_t timer_period );
  79. };
  80. // Nes_Noise
  81. struct Nes_Noise : Nes_Envelope
  82. {
  83. int noise;
  84. Blip_Synth<blip_med_quality,1> synth;
  85. void run( nes_time_t, nes_time_t );
  86. void reset() {
  87. noise = 1 << 14;
  88. Nes_Envelope::reset();
  89. }
  90. };
  91. // Nes_Dmc
  92. struct Nes_Dmc : Nes_Osc
  93. {
  94. int address; // address of next byte to read
  95. int period;
  96. //int length_counter; // bytes remaining to play (already defined in Nes_Osc)
  97. int buf;
  98. int bits_remain;
  99. int bits;
  100. bool buf_full;
  101. bool silence;
  102. enum { loop_flag = 0x40 };
  103. int dac;
  104. nes_time_t next_irq;
  105. bool irq_enabled;
  106. bool irq_flag;
  107. bool pal_mode;
  108. bool nonlinear;
  109. int (*prg_reader)( void*, nes_addr_t ); // needs to be initialized to prg read function
  110. void* prg_reader_data;
  111. Nes_Apu* apu;
  112. Blip_Synth<blip_med_quality,1> synth;
  113. void start();
  114. void write_register( int, int );
  115. void run( nes_time_t, nes_time_t );
  116. void recalc_irq();
  117. void fill_buffer();
  118. void reload_sample();
  119. void reset();
  120. int count_reads( nes_time_t, nes_time_t* ) const;
  121. nes_time_t next_read_time() const;
  122. };
  123. #endif