beeps 0.1.32 → 0.1.34

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/.doc/ext/beeps/adsr.cpp +139 -0
  3. data/.doc/ext/beeps/analyser.cpp +128 -0
  4. data/.doc/ext/beeps/beeps.cpp +9 -1
  5. data/.doc/ext/beeps/file_in.cpp +55 -9
  6. data/.doc/ext/beeps/gain.cpp +64 -0
  7. data/.doc/ext/beeps/mic_in.cpp +83 -0
  8. data/.doc/ext/beeps/native.cpp +20 -8
  9. data/.doc/ext/beeps/oscillator.cpp +88 -0
  10. data/.doc/ext/beeps/pitch_shift.cpp +64 -0
  11. data/.doc/ext/beeps/processor.cpp +31 -2
  12. data/.doc/ext/beeps/sound.cpp +90 -7
  13. data/.doc/ext/beeps/sound_player.cpp +156 -0
  14. data/.doc/ext/beeps/time_stretch.cpp +64 -0
  15. data/.github/workflows/release-gem.yml +4 -1
  16. data/ChangeLog.md +14 -0
  17. data/Rakefile +26 -4
  18. data/VERSION +1 -1
  19. data/beeps.gemspec +2 -2
  20. data/ext/beeps/adsr.cpp +150 -0
  21. data/ext/beeps/analyser.cpp +134 -0
  22. data/ext/beeps/beeps.cpp +10 -1
  23. data/ext/beeps/extconf.rb +1 -2
  24. data/ext/beeps/file_in.cpp +60 -9
  25. data/ext/beeps/gain.cpp +67 -0
  26. data/ext/beeps/mic_in.cpp +88 -0
  27. data/ext/beeps/native.cpp +20 -8
  28. data/ext/beeps/oscillator.cpp +93 -0
  29. data/ext/beeps/pitch_shift.cpp +67 -0
  30. data/ext/beeps/processor.cpp +34 -2
  31. data/ext/beeps/sound.cpp +99 -7
  32. data/ext/beeps/sound_player.cpp +169 -0
  33. data/ext/beeps/time_stretch.cpp +67 -0
  34. data/include/beeps/beeps.h +2 -1
  35. data/include/beeps/filter.h +179 -0
  36. data/include/beeps/generator.h +120 -0
  37. data/include/beeps/processor.h +37 -68
  38. data/include/beeps/ruby/filter.h +78 -0
  39. data/include/beeps/ruby/generator.h +60 -0
  40. data/include/beeps/ruby/processor.h +5 -45
  41. data/include/beeps/ruby/sound.h +14 -3
  42. data/include/beeps/signals.h +10 -4
  43. data/include/beeps/sound.h +67 -2
  44. data/lib/beeps/beeps.rb +6 -1
  45. data/lib/beeps/processor.rb +95 -15
  46. data/lib/beeps/sound.rb +29 -2
  47. data/src/adsr.cpp +245 -0
  48. data/src/analyser.cpp +254 -0
  49. data/src/beeps.cpp +14 -2
  50. data/src/file_in.cpp +94 -0
  51. data/src/gain.cpp +55 -0
  52. data/src/mic_in.cpp +271 -0
  53. data/src/mic_in.h +22 -0
  54. data/src/openal.cpp +1 -2
  55. data/src/oscillator.cpp +145 -0
  56. data/src/osx/signals.mm +83 -0
  57. data/src/pitch_shift.cpp +82 -0
  58. data/src/processor.cpp +202 -88
  59. data/src/processor.h +98 -0
  60. data/src/signals.cpp +326 -20
  61. data/src/signals.h +192 -2
  62. data/src/sound.cpp +735 -113
  63. data/src/sound.h +6 -1
  64. data/src/time_stretch.cpp +91 -0
  65. data/test/helper.rb +2 -1
  66. data/test/test_beeps.rb +10 -7
  67. data/test/test_beeps_init.rb +18 -0
  68. data/test/test_file_in.rb +15 -0
  69. data/test/test_processor.rb +50 -0
  70. data/test/test_sound.rb +87 -11
  71. data/test/test_sound_player.rb +134 -0
  72. metadata +54 -16
  73. data/.doc/ext/beeps/sawtooth_wave.cpp +0 -61
  74. data/.doc/ext/beeps/sine_wave.cpp +0 -61
  75. data/.doc/ext/beeps/square_wave.cpp +0 -61
  76. data/ext/beeps/sawtooth_wave.cpp +0 -64
  77. data/ext/beeps/sine_wave.cpp +0 -64
  78. data/ext/beeps/square_wave.cpp +0 -64
@@ -4,6 +4,7 @@
4
4
  #define __BEEPS_PROCESSOR_H__
5
5
 
6
6
 
7
+ #include <xot/ref.h>
7
8
  #include <xot/pimpl.h>
8
9
  #include <beeps/defs.h>
9
10
 
@@ -15,118 +16,86 @@ namespace Beeps
15
16
  class Signals;
16
17
 
17
18
 
18
- class Processor
19
+ class Processor : public Xot::RefCountable<>
19
20
  {
20
21
 
21
- public:
22
-
23
- virtual ~Processor ();
24
-
25
- virtual void process (Signals* signals);
26
-
27
- virtual operator bool () const;
28
-
29
- virtual bool operator ! () const;
30
-
31
- protected:
32
-
33
- Processor ();
34
-
35
- };// Processor
36
-
37
-
38
- class SineWave : public Processor
39
- {
40
-
41
- typedef Processor Super;
22
+ typedef Processor This;
42
23
 
43
24
  public:
44
25
 
45
- SineWave ();
26
+ class Context {};
46
27
 
47
- virtual ~SineWave ();
28
+ typedef Xot::Ref<This> Ref;
48
29
 
49
- virtual void set_frequency (float frequency);
30
+ virtual ~Processor ();
50
31
 
51
- virtual float frequency () const;
32
+ virtual void reset ();
52
33
 
53
- virtual void process (Signals* signals);
34
+ virtual void set_input (Processor* input);
54
35
 
55
- struct Data;
36
+ virtual Processor* input ();
56
37
 
57
- Xot::PSharedImpl<Data> self;
58
-
59
- };// SineWave
38
+ virtual const Processor* input () const;
60
39
 
40
+ virtual operator bool () const;
61
41
 
62
- class SquareWave : public Processor
63
- {
42
+ virtual bool operator ! () const;
64
43
 
65
- typedef Processor Super;
44
+ struct Data;
66
45
 
67
- public:
46
+ Xot::PImpl<Data> self;
68
47
 
69
- SquareWave ();
48
+ protected:
70
49
 
71
- virtual ~SquareWave ();
50
+ Processor (bool generator = false);
72
51
 
73
- virtual void set_frequency (float frequency);
52
+ virtual void process (Context* context, Signals* signals, uint* offset) final;
74
53
 
75
- virtual float frequency () const;
54
+ virtual void generate (Context* context, Signals* signals, uint* offset);
76
55
 
77
- virtual void process (Signals* signals);
56
+ virtual void filter (Context* context, Signals* signals, uint* offset);
78
57
 
79
- struct Data;
58
+ virtual void set_updated ();
80
59
 
81
- Xot::PSharedImpl<Data> self;
60
+ friend class ProcessorContext;
82
61
 
83
- };// SquareWave
62
+ };// Processor
84
63
 
85
64
 
86
- class SawtoothWave : public Processor
65
+ class Generator : public Processor
87
66
  {
88
67
 
89
68
  typedef Processor Super;
90
69
 
91
- public:
92
-
93
- SawtoothWave ();
94
-
95
- virtual ~SawtoothWave ();
96
-
97
- virtual void set_frequency (float frequency);
70
+ protected:
98
71
 
99
- virtual float frequency () const;
72
+ Generator ();
100
73
 
101
- virtual void process (Signals* signals);
74
+ private:
102
75
 
103
- struct Data;
76
+ void filter (
77
+ Context* context, Signals* signals, uint* offset) override final;
104
78
 
105
- Xot::PSharedImpl<Data> self;
79
+ };// Generator
106
80
 
107
- };// SawtoothWave
108
81
 
109
-
110
- class FileIn : public Processor
82
+ class Filter : public Processor
111
83
  {
112
84
 
113
85
  typedef Processor Super;
114
86
 
115
- public:
116
-
117
- FileIn (const char* path = NULL);
87
+ protected:
118
88
 
119
- virtual ~FileIn ();
89
+ Filter (Processor* input = NULL);
120
90
 
121
- virtual void process (Signals* signals);
91
+ void set_buffering_seconds (float seconds);
122
92
 
123
- virtual operator bool () const;
124
-
125
- struct Data;
93
+ private:
126
94
 
127
- Xot::PSharedImpl<Data> self;
95
+ void generate (
96
+ Context* context, Signals* signals, uint* offset) override final;
128
97
 
129
- };// FileIn
98
+ };// Filter
130
99
 
131
100
 
132
101
  }// Beeps
@@ -0,0 +1,78 @@
1
+ // -*- c++ -*-
2
+ #pragma once
3
+ #ifndef __BEEPS_RUBY_FILTER_H__
4
+ #define __BEEPS_RUBY_FILTER_H__
5
+
6
+
7
+ #include <rucy/class.h>
8
+ #include <rucy/extension.h>
9
+ #include <beeps/filter.h>
10
+
11
+
12
+ RUCY_DECLARE_WRAPPER_VALUE_FROM_TO(Beeps::TimeStretch)
13
+
14
+ RUCY_DECLARE_WRAPPER_VALUE_FROM_TO(Beeps::PitchShift)
15
+
16
+
17
+ namespace Beeps
18
+ {
19
+
20
+
21
+ Rucy::Class gain_class ();
22
+ // class Beeps::Gain
23
+
24
+ Rucy::Class adsr_class ();
25
+ // class Beeps::ADSR
26
+
27
+ Rucy::Class time_stretch_class ();
28
+ // class Beeps::TimeStretch
29
+
30
+ Rucy::Class pitch_shift_class ();
31
+ // class Beeps::PitchShift
32
+
33
+ Rucy::Class analyser_class ();
34
+ // class Beeps::Analyser
35
+
36
+
37
+ }// Beeps
38
+
39
+
40
+ namespace Rucy
41
+ {
42
+
43
+
44
+ template <> inline Class
45
+ get_ruby_class<Beeps::Gain> ()
46
+ {
47
+ return Beeps::gain_class();
48
+ }
49
+
50
+ template <> inline Class
51
+ get_ruby_class<Beeps::ADSR> ()
52
+ {
53
+ return Beeps::adsr_class();
54
+ }
55
+
56
+ template <> inline Class
57
+ get_ruby_class<Beeps::TimeStretch> ()
58
+ {
59
+ return Beeps::time_stretch_class();
60
+ }
61
+
62
+ template <> inline Class
63
+ get_ruby_class<Beeps::PitchShift> ()
64
+ {
65
+ return Beeps::pitch_shift_class();
66
+ }
67
+
68
+ template <> inline Class
69
+ get_ruby_class<Beeps::Analyser> ()
70
+ {
71
+ return Beeps::analyser_class();
72
+ }
73
+
74
+
75
+ }// Rucy
76
+
77
+
78
+ #endif//EOH
@@ -0,0 +1,60 @@
1
+ // -*- c++ -*-
2
+ #pragma once
3
+ #ifndef __BEEPS_RUBY_GENERATOR_H__
4
+ #define __BEEPS_RUBY_GENERATOR_H__
5
+
6
+
7
+ #include <rucy/class.h>
8
+ #include <rucy/extension.h>
9
+ #include <beeps/generator.h>
10
+
11
+
12
+ RUCY_DECLARE_WRAPPER_VALUE_FROM_TO(Beeps::Oscillator)
13
+
14
+ RUCY_DECLARE_WRAPPER_VALUE_FROM_TO(Beeps::FileIn)
15
+
16
+
17
+ namespace Beeps
18
+ {
19
+
20
+
21
+ Rucy::Class oscillator_class ();
22
+ // class Beeps::Oscillator
23
+
24
+ Rucy::Class file_in_class ();
25
+ // class Beeps::FileIn
26
+
27
+ Rucy::Class mic_in_class ();
28
+ // class Beeps::MicIn
29
+
30
+
31
+ }// Beeps
32
+
33
+
34
+ namespace Rucy
35
+ {
36
+
37
+
38
+ template <> inline Class
39
+ get_ruby_class<Beeps::Oscillator> ()
40
+ {
41
+ return Beeps::oscillator_class();
42
+ }
43
+
44
+ template <> inline Class
45
+ get_ruby_class<Beeps::FileIn> ()
46
+ {
47
+ return Beeps::file_in_class();
48
+ }
49
+
50
+ template <> inline Class
51
+ get_ruby_class<Beeps::MicIn> ()
52
+ {
53
+ return Beeps::mic_in_class();
54
+ }
55
+
56
+
57
+ }// Rucy
58
+
59
+
60
+ #endif//EOH
@@ -9,6 +9,9 @@
9
9
  #include <beeps/processor.h>
10
10
 
11
11
 
12
+ RUCY_DECLARE_WRAPPER_VALUE_FROM_TO(Beeps::Processor)
13
+
14
+
12
15
  namespace Beeps
13
16
  {
14
17
 
@@ -16,33 +19,14 @@ namespace Beeps
16
19
  Rucy::Class processor_class ();
17
20
  // class Beeps::Processor
18
21
 
19
- Rucy::Class sine_wave_class ();
20
- // class Beeps::SineWave
21
-
22
- Rucy::Class square_wave_class ();
23
- // class Beeps::SineWave
24
-
25
- Rucy::Class sawtooth_wave_class ();
26
- // class Beeps::SawtoothWave
27
22
 
28
- Rucy::Class file_in_class ();
29
- // class Beeps::FileIn
23
+ template <typename T>
24
+ class RubyProcessor : public Rucy::ClassWrapper<T> {};
30
25
 
31
26
 
32
27
  }// Beeps
33
28
 
34
29
 
35
- RUCY_DECLARE_VALUE_FROM_TO(Beeps::Processor)
36
-
37
- RUCY_DECLARE_VALUE_FROM_TO(Beeps::SineWave)
38
-
39
- RUCY_DECLARE_VALUE_FROM_TO(Beeps::SquareWave)
40
-
41
- RUCY_DECLARE_VALUE_FROM_TO(Beeps::SawtoothWave)
42
-
43
- RUCY_DECLARE_VALUE_FROM_TO(Beeps::FileIn)
44
-
45
-
46
30
  namespace Rucy
47
31
  {
48
32
 
@@ -53,30 +37,6 @@ namespace Rucy
53
37
  return Beeps::processor_class();
54
38
  }
55
39
 
56
- template <> inline Class
57
- get_ruby_class<Beeps::SineWave> ()
58
- {
59
- return Beeps::sine_wave_class();
60
- }
61
-
62
- template <> inline Class
63
- get_ruby_class<Beeps::SquareWave> ()
64
- {
65
- return Beeps::square_wave_class();
66
- }
67
-
68
- template <> inline Class
69
- get_ruby_class<Beeps::SawtoothWave> ()
70
- {
71
- return Beeps::sawtooth_wave_class();
72
- }
73
-
74
- template <> inline Class
75
- get_ruby_class<Beeps::FileIn> ()
76
- {
77
- return Beeps::file_in_class();
78
- }
79
-
80
40
 
81
41
  }// Rucy
82
42
 
@@ -9,10 +9,18 @@
9
9
  #include <beeps/sound.h>
10
10
 
11
11
 
12
+ RUCY_DECLARE_VALUE_FROM_TO(Beeps::SoundPlayer)
13
+
14
+ RUCY_DECLARE_VALUE_FROM_TO(Beeps::Sound)
15
+
16
+
12
17
  namespace Beeps
13
18
  {
14
19
 
15
20
 
21
+ Rucy::Class sound_player_class ();
22
+ // class Beeps::SoundPlayer
23
+
16
24
  Rucy::Class sound_class ();
17
25
  // class Beeps::Sound
18
26
 
@@ -20,13 +28,16 @@ namespace Beeps
20
28
  }// Beeps
21
29
 
22
30
 
23
- RUCY_DECLARE_VALUE_FROM_TO(Beeps::Sound)
24
-
25
-
26
31
  namespace Rucy
27
32
  {
28
33
 
29
34
 
35
+ template <> inline Class
36
+ get_ruby_class<Beeps::SoundPlayer> ()
37
+ {
38
+ return Beeps::sound_player_class();
39
+ }
40
+
30
41
  template <> inline Class
31
42
  get_ruby_class<Beeps::Sound> ()
32
43
  {
@@ -17,17 +17,23 @@ namespace Beeps
17
17
 
18
18
  public:
19
19
 
20
- Signals (float seconds = 1, uint channels = 1);
20
+ Signals ();
21
21
 
22
22
  ~Signals ();
23
23
 
24
24
  Signals dup () const;
25
25
 
26
- float seconds () const;
26
+ double sample_rate () const;
27
27
 
28
- uint samples () const;
28
+ uint nchannels () const;
29
29
 
30
- uint channels () const;
30
+ uint nsamples () const;
31
+
32
+ uint capacity () const;
33
+
34
+ bool empty () const;
35
+
36
+ const double* samples () const;
31
37
 
32
38
  operator bool () const;
33
39
 
@@ -15,6 +15,48 @@ namespace Beeps
15
15
  class Processor;
16
16
 
17
17
 
18
+ class SoundPlayer
19
+ {
20
+
21
+ public:
22
+
23
+ SoundPlayer ();
24
+
25
+ ~SoundPlayer ();
26
+
27
+ void play ();
28
+
29
+ void pause ();
30
+
31
+ void rewind ();
32
+
33
+ void stop ();
34
+
35
+ bool is_playing () const;
36
+
37
+ bool is_paused () const;
38
+
39
+ bool is_stopped () const;
40
+
41
+ void set_gain (float gain);
42
+
43
+ float gain () const;
44
+
45
+ void set_loop (bool loop);
46
+
47
+ bool loop () const;
48
+
49
+ operator bool () const;
50
+
51
+ bool operator ! () const;
52
+
53
+ struct Data;
54
+
55
+ Xot::PSharedImpl<Data> self;
56
+
57
+ };// SoundPlayer
58
+
59
+
18
60
  class Sound
19
61
  {
20
62
 
@@ -22,11 +64,29 @@ namespace Beeps
22
64
 
23
65
  Sound ();
24
66
 
25
- Sound (Processor* processor, float seconds);
67
+ Sound (
68
+ Processor* processor, float seconds,
69
+ uint nchannels = 1, double sample_rate = 0);
26
70
 
27
71
  ~Sound ();
28
72
 
29
- void play ();
73
+ SoundPlayer play ();
74
+
75
+ void save (const char* path) const;
76
+
77
+ double sample_rate () const;
78
+
79
+ uint nchannels () const;
80
+
81
+ float seconds () const;
82
+
83
+ void set_gain (float gain);
84
+
85
+ float gain () const;
86
+
87
+ void set_loop (bool loop);
88
+
89
+ bool loop () const;
30
90
 
31
91
  operator bool () const;
32
92
 
@@ -39,6 +99,11 @@ namespace Beeps
39
99
  };// Sound
40
100
 
41
101
 
102
+ void stop_all_sound_players ();
103
+
104
+ Sound load_sound (const char* path);
105
+
106
+
42
107
  }// Beeps
43
108
 
44
109
 
data/lib/beeps/beeps.rb CHANGED
@@ -35,10 +35,15 @@ module Beeps
35
35
  beep_sound(2).play
36
36
  end
37
37
 
38
+ def beep_processor=(processor)
39
+ @beep_processor = processor
40
+ end
41
+
38
42
  private
39
43
 
40
44
  def beep_sound(time)
41
- Sound.new SquareWave.new, time
45
+ @beep_processor ||= Oscillator.new(:square)
46
+ Sound.new @beep_processor, time
42
47
  end
43
48
 
44
49
  self
@@ -2,6 +2,7 @@
2
2
 
3
3
 
4
4
  require 'xot/setter'
5
+ require 'xot/const_symbol_accessor'
5
6
  require 'xot/universal_accessor'
6
7
  require 'xot/block_util'
7
8
  require 'beeps/ext'
@@ -14,43 +15,122 @@ module Beeps
14
15
 
15
16
  include Xot::Setter
16
17
 
17
- def initialize(options = nil, &block)
18
+ def initialize(**options, &block)
18
19
  super()
19
- set options if options
20
+ set options unless options.empty?
20
21
  Xot::BlockUtil.instance_eval_or_block_call self, &block if block
21
22
  end
22
23
 
24
+ def >>(o)
25
+ o.input = self
26
+ o
27
+ end
28
+
29
+ def <<(o)
30
+ self.input = o
31
+ o
32
+ end
33
+
34
+ universal_accessor :input
35
+
23
36
  end# Processor
24
37
 
25
38
 
26
- class SineWave
39
+ class Oscillator
40
+
41
+ const_symbol_accessor :type, **{
42
+ none: NONE,
43
+ sine: SINE,
44
+ triangle: TRIANGLE,
45
+ square: SQUARE,
46
+ sawtooth: SAWTOOTH
47
+ }
48
+
49
+ def initialize(type = :sine, *args, **kwargs, &block)
50
+ super(*args, **kwargs, &block)
51
+ self.type = type
52
+ end
27
53
 
28
54
  alias freq= frequency=
29
55
  alias freq frequency
30
56
 
31
- universal_accessor :frequency, :freq
57
+ universal_accessor :type, :frequency, :freq
32
58
 
33
- end# SineWave
59
+ end# Oscillator
34
60
 
35
61
 
36
- class SquareWave
62
+ class FileIn
37
63
 
38
- alias freq= frequency=
39
- alias freq frequency
64
+ def initialize(path = nil, *args, **kwargs, &block)
65
+ super(*args, **kwargs, &block)
66
+ self.path = path if path
67
+ end
40
68
 
41
- universal_accessor :frequency, :freq
69
+ universal_accessor :path
42
70
 
43
- end# SquareWave
71
+ end# FileIn
44
72
 
45
73
 
46
- class SawtoothWave
74
+ class Gain
47
75
 
48
- alias freq= frequency=
49
- alias freq frequency
76
+ universal_accessor :gain
77
+
78
+ end# Gain
79
+
80
+
81
+ class ADSR
82
+
83
+ def note_on(delay = 0)
84
+ note_on! delay
85
+ end
86
+
87
+ def note_off(delay = 0)
88
+ note_off! delay
89
+ end
90
+
91
+ universal_accessor :attack_time, :decay_time, :sustain_level, :release_time
92
+
93
+ alias attack= attack_time=
94
+ alias attack attack_time
95
+ alias decay= decay_time=
96
+ alias decay decay_time
97
+ alias sustain= sustain_level=
98
+ alias sustain sustain_level
99
+ alias release= release_time=
100
+ alias release release_time
101
+
102
+ end# ADSR
103
+
104
+
105
+ class TimeStretch
106
+
107
+ universal_accessor :scale
108
+
109
+ end# TimeStretch
110
+
111
+
112
+ class PitchShift
113
+
114
+ universal_accessor :shift
115
+
116
+ end# PitchShift
117
+
118
+
119
+ class Analyser
120
+
121
+ def each_signal(nsamples = fft_size, &block)
122
+ return enum_for(:each_signal, nsamples) unless block
123
+ each_signal!(nsamples, &block)
124
+ end
125
+
126
+ def each_spectrum(&block)
127
+ return enum_for(:each_spectrum) unless block
128
+ each_spectrum!(&block)
129
+ end
50
130
 
51
- universal_accessor :frequency, :freq
131
+ universal_accessor :fft_size
52
132
 
53
- end# SawtoothWave
133
+ end# Analyser
54
134
 
55
135
 
56
136
  end# Beeps