Multi_Buffer.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Multi-channel sound buffer interface, and basic mono and stereo buffers
  2. // Blip_Buffer 0.4.1
  3. #ifndef MULTI_BUFFER_H
  4. #define MULTI_BUFFER_H
  5. #include "blargg_common.h"
  6. #include "Blip_Buffer.h"
  7. // Interface to one or more Blip_Buffers mapped to one or more channels
  8. // consisting of left, center, and right buffers.
  9. class Multi_Buffer {
  10. public:
  11. Multi_Buffer( int samples_per_frame );
  12. virtual ~Multi_Buffer() { }
  13. // Set the number of channels available
  14. virtual blargg_err_t set_channel_count( int );
  15. // Get indexed channel, from 0 to channel count - 1
  16. struct channel_t {
  17. Blip_Buffer* center;
  18. Blip_Buffer* left;
  19. Blip_Buffer* right;
  20. };
  21. enum { type_index_mask = 0xFF };
  22. enum { wave_type = 0x100, noise_type = 0x200, mixed_type = wave_type | noise_type };
  23. virtual channel_t channel( int index, int type ) = 0;
  24. // See Blip_Buffer.h
  25. virtual blargg_err_t set_sample_rate( long rate, int msec = blip_default_length ) = 0;
  26. virtual void clock_rate( long ) = 0;
  27. virtual void bass_freq( int ) = 0;
  28. virtual void clear() = 0;
  29. long sample_rate() const;
  30. // Length of buffer, in milliseconds
  31. int length() const;
  32. // See Blip_Buffer.h
  33. virtual void end_frame( blip_time_t ) = 0;
  34. // Number of samples per output frame (1 = mono, 2 = stereo)
  35. int samples_per_frame() const;
  36. // Count of changes to channel configuration. Incremented whenever
  37. // a change is made to any of the Blip_Buffers for any channel.
  38. unsigned channels_changed_count() { return channels_changed_count_; }
  39. // See Blip_Buffer.h
  40. virtual long read_samples( blip_sample_t*, long ) = 0;
  41. virtual long samples_avail() const = 0;
  42. public:
  43. BLARGG_DISABLE_NOTHROW
  44. protected:
  45. void channels_changed() { channels_changed_count_++; }
  46. private:
  47. // noncopyable
  48. Multi_Buffer( const Multi_Buffer& );
  49. Multi_Buffer& operator = ( const Multi_Buffer& );
  50. unsigned channels_changed_count_;
  51. long sample_rate_;
  52. int length_;
  53. int const samples_per_frame_;
  54. };
  55. // Uses a single buffer and outputs mono samples.
  56. class Mono_Buffer : public Multi_Buffer {
  57. Blip_Buffer buf;
  58. channel_t chan;
  59. public:
  60. // Buffer used for all channels
  61. Blip_Buffer* center() { return &buf; }
  62. public:
  63. Mono_Buffer();
  64. ~Mono_Buffer();
  65. blargg_err_t set_sample_rate( long rate, int msec = blip_default_length );
  66. void clock_rate( long rate ) { buf.clock_rate( rate ); }
  67. void bass_freq( int freq ) { buf.bass_freq( freq ); }
  68. void clear() { buf.clear(); }
  69. long samples_avail() const { return buf.samples_avail(); }
  70. long read_samples( blip_sample_t* p, long s ) { return buf.read_samples( p, s ); }
  71. channel_t channel( int, int ) { return chan; }
  72. void end_frame( blip_time_t t ) { buf.end_frame( t ); }
  73. };
  74. // Uses three buffers (one for center) and outputs stereo sample pairs.
  75. class Stereo_Buffer : public Multi_Buffer {
  76. public:
  77. // Buffers used for all channels
  78. Blip_Buffer* center() { return &bufs [0]; }
  79. Blip_Buffer* left() { return &bufs [1]; }
  80. Blip_Buffer* right() { return &bufs [2]; }
  81. public:
  82. Stereo_Buffer();
  83. ~Stereo_Buffer();
  84. blargg_err_t set_sample_rate( long, int msec = blip_default_length );
  85. void clock_rate( long );
  86. void bass_freq( int );
  87. void clear();
  88. channel_t channel( int, int ) { return chan; }
  89. void end_frame( blip_time_t );
  90. long samples_avail() const { return bufs [0].samples_avail() * 2; }
  91. long read_samples( blip_sample_t*, long );
  92. private:
  93. enum { buf_count = 3 };
  94. Blip_Buffer bufs [buf_count];
  95. channel_t chan;
  96. int stereo_added;
  97. int was_stereo;
  98. void mix_stereo_no_center( blip_sample_t*, blargg_long );
  99. void mix_stereo( blip_sample_t*, blargg_long );
  100. void mix_mono( blip_sample_t*, blargg_long );
  101. };
  102. // Silent_Buffer generates no samples, useful where no sound is wanted
  103. class Silent_Buffer : public Multi_Buffer {
  104. channel_t chan;
  105. public:
  106. Silent_Buffer();
  107. blargg_err_t set_sample_rate( long rate, int msec = blip_default_length );
  108. void clock_rate( long ) { }
  109. void bass_freq( int ) { }
  110. void clear() { }
  111. channel_t channel( int, int ) { return chan; }
  112. void end_frame( blip_time_t ) { }
  113. long samples_avail() const { return 0; }
  114. long read_samples( blip_sample_t*, long ) { return 0; }
  115. };
  116. inline blargg_err_t Multi_Buffer::set_sample_rate( long rate, int msec )
  117. {
  118. sample_rate_ = rate;
  119. length_ = msec;
  120. return 0;
  121. }
  122. inline blargg_err_t Silent_Buffer::set_sample_rate( long rate, int msec )
  123. {
  124. return Multi_Buffer::set_sample_rate( rate, msec );
  125. }
  126. inline int Multi_Buffer::samples_per_frame() const { return samples_per_frame_; }
  127. inline long Multi_Buffer::sample_rate() const { return sample_rate_; }
  128. inline int Multi_Buffer::length() const { return length_; }
  129. #endif