diapason 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7fc96c461ada733825f021d645fe5165625c570d
4
+ data.tar.gz: 4d4a59a115240293d3c1993a427b60fd4b6a28ae
5
+ SHA512:
6
+ metadata.gz: 5dbbc8206f9e8cad04544e7961708d16386d5b4378d27720b96422a252fae0338de4beaeb3713e0252750890539b34fe3101fa1bda113beb3aec8b3f735f46d0
7
+ data.tar.gz: a37799e920950cea569657e8f2fc566152700d759843e950c5e3a031f4329b66dfea6dd00f296a43c5db97ce668525567caebfbd5af33c11b1f86c873f1cf4fb
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in diapason.gemspec
4
+ gemspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2015 Felipe Zavan http://zavan.me/
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,52 @@
1
+ # Diapason
2
+ Music with Ruby.
3
+
4
+ ## Install
5
+ Install it directly: `gem install diapason`
6
+
7
+ Or put it in your Gemfile: `gem 'diapason'`
8
+
9
+ ## Use
10
+ ```ruby
11
+ require 'diapason'
12
+
13
+ # A (La) 440Hz
14
+ tonic = Diapason::Note.a440
15
+
16
+ # Tuning system used for pianos and other keyboard instruments
17
+ twelve_tone_equal_temperament = Diapason::Tuning::EqualTemperament.new(12, tonic)
18
+
19
+ # Generates the 12 notes with their respective frequencies (the chromatic scale)
20
+ notes = twelve_tone_equal_temperament.notes
21
+
22
+ # Builds the C (Do) major scale
23
+ # 2 = tone, 1 = semitone
24
+ major_scale_intervals = [2, 2, 1, 2, 2, 2, 1]
25
+ c_major = notes[3]
26
+ c_major_scale = Diapason::Scale.build(notes, major_scale_intervals, c_major)
27
+
28
+ # Generates sine waves (like those from tunning forks) from each note of the scale
29
+ waves = c_major_scale.notes.map do |note|
30
+ Diapason::Sound::SineWave.from_note(note)
31
+ end
32
+
33
+ # Since continuous signals (like audio waves) can't be exactly represented by
34
+ # digital means (like files) we need to sample it (pick some points of the wave
35
+ # to represent it) at some sample rate. 44100Hz is used by CD's.
36
+ sample_rate = 44100
37
+
38
+ # Samples any kind of sinusoidal waves, like sine waves or cosine waves
39
+ sampler = Diapason::Sound::SinusoidalWaveSampler.new(sample_rate)
40
+
41
+ # Builds 1 second worth of samples for each wave (note)
42
+ samples = waves.map do |wave|
43
+ sampler.samples_for_duration(wave, 1)
44
+ end
45
+
46
+ # Writes wave file
47
+ writer = Diapason::Sound::WaveFileWriter.new(sample_rate)
48
+ file = writer.write_samples(samples.map(&:to_a).flatten, '/path/to/c_major_scale.wav')
49
+
50
+ # Plays the file
51
+ Diapason::Sound::WaveFilePlayer.new.play(file)
52
+ ```
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "diapason"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require "pry"
11
+ Pry.start
12
+
13
+ # require "irb"
14
+ # IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'diapason/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "diapason"
8
+ spec.version = Diapason::VERSION
9
+ spec.authors = ["Felipe Zavan"]
10
+ spec.email = ["felipe@zavan.me"]
11
+
12
+ spec.summary = %q{Music with ruby.}
13
+ spec.homepage = "http://github.com/zavan/diapason"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "wavefile", "~> 0.6.0"
22
+
23
+ spec.add_development_dependency "pry", "~> 0.10.3"
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.4"
27
+ end
@@ -0,0 +1,8 @@
1
+ require 'diapason/version'
2
+ require 'diapason/note'
3
+ require 'diapason/tuning'
4
+ require 'diapason/scale'
5
+ require 'diapason/sound'
6
+
7
+ module Diapason
8
+ end
@@ -0,0 +1,16 @@
1
+ class Diapason::Note
2
+ attr_accessor :frequency, :index
3
+
4
+ def initialize(frequency, index)
5
+ @frequency = frequency.to_f
6
+ @index = index
7
+ end
8
+
9
+ def self.a440
10
+ new(440, 0)
11
+ end
12
+
13
+ def ==(other)
14
+ frequency == other.frequency
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ class Diapason::Scale
2
+ attr_accessor :notes
3
+
4
+ def initialize(notes)
5
+ @notes = notes
6
+ end
7
+
8
+ def self.build(notes, intervals, tonic)
9
+ scale_notes = [tonic]
10
+
11
+ intervals.each do |interval|
12
+ index = notes.index(scale_notes.last) + interval
13
+
14
+ if index >= notes.size
15
+ index = notes.size - index
16
+ end
17
+
18
+ scale_notes << notes[index]
19
+ end
20
+
21
+ new(scale_notes)
22
+ end
23
+ end
@@ -0,0 +1,9 @@
1
+ module Diapason::Sound
2
+ end
3
+
4
+ require_relative 'sound/wave_sampler'
5
+ require_relative 'sound/sinusoidal_wave_sampler'
6
+ require_relative 'sound/wave'
7
+ require_relative 'sound/sine_wave'
8
+ require_relative 'sound/wave_file_writer'
9
+ require_relative 'sound/wave_file_player'
@@ -0,0 +1,5 @@
1
+ class Diapason::Sound::SineWave < Diapason::Sound::Wave
2
+ def call(x)
3
+ amplitude * Math.sin(x)
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class Diapason::Sound::SinusoidalWaveSampler < Diapason::Sound::WaveSampler
2
+ def cycle
3
+ 2 * Math::PI
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ class Diapason::Sound::Wave
2
+ attr_accessor :frequency, :amplitude
3
+
4
+ def initialize(frequency, amplitude = 1)
5
+ @frequency = frequency.to_f
6
+ @amplitude = amplitude.to_f
7
+ end
8
+
9
+ def self.from_note(note)
10
+ new(note.frequency)
11
+ end
12
+
13
+ def fn(x)
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ class Diapason::Sound::WaveFilePlayer
2
+ def play(file)
3
+ `aplay #{file.path}`
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ require 'wavefile'
2
+
3
+ class Diapason::Sound::WaveFileWriter
4
+ attr_accessor :sample_rate
5
+
6
+ def initialize(sample_rate = 44100)
7
+ @sample_rate = sample_rate.to_f
8
+ end
9
+
10
+ def write_samples(samples, path)
11
+ wave_file = WaveFile::Writer.new(path, format(:pcm_16)) do |writer|
12
+ writer.write(buffer(samples))
13
+ end
14
+
15
+ File.new(wave_file.file_name)
16
+ end
17
+
18
+ def buffer(samples)
19
+ WaveFile::Buffer.new(samples, format(:float))
20
+ end
21
+
22
+ def format(sample_format)
23
+ WaveFile::Format.new(:mono, sample_format, sample_rate)
24
+ end
25
+ end
@@ -0,0 +1,34 @@
1
+ class Diapason::Sound::WaveSampler
2
+ attr_accessor :sample_rate
3
+
4
+ def initialize(sample_rate = 44100)
5
+ @sample_rate = sample_rate.to_f
6
+ end
7
+
8
+ def each_sample(wave)
9
+ return enum_for(:each_sample, wave) unless block_given?
10
+
11
+ x = 0
12
+ loop do
13
+ yield wave.call(x)
14
+ x += cycle * cycles_per_frame(wave.frequency)
15
+ end
16
+ end
17
+
18
+ def samples_for_duration(wave, duration)
19
+ return enum_for(:samples_for_duration, wave, duration) unless block_given?
20
+
21
+ total_samples = (duration * sample_rate).round
22
+
23
+ i = 0
24
+ each_sample(wave) do |sample|
25
+ break if i == total_samples
26
+ i += 1
27
+ yield sample
28
+ end
29
+ end
30
+
31
+ def cycles_per_frame(frequency)
32
+ frequency / sample_rate
33
+ end
34
+ end
@@ -0,0 +1,4 @@
1
+ module Diapason::Tuning
2
+ end
3
+
4
+ require_relative 'tuning/equal_temperament'
@@ -0,0 +1,36 @@
1
+ class Diapason::Tuning::EqualTemperament
2
+ include Enumerable
3
+
4
+ attr_accessor :partitions, :tonic, :base_size
5
+
6
+ def initialize(partitions, tonic, base_size = 2) # 2 = 2:1 = normal octave
7
+ @partitions = partitions
8
+ @tonic = tonic
9
+ @base_size = base_size
10
+ end
11
+
12
+ def each
13
+ return enum_for(:each) unless block_given?
14
+
15
+ yield tonic
16
+
17
+ last_note = tonic
18
+
19
+ (partitions - 1).times do
20
+ last_note = Diapason::Note.new(
21
+ last_note.frequency * ratio,
22
+ last_note.index + 1
23
+ )
24
+
25
+ yield last_note
26
+ end
27
+ end
28
+
29
+ def notes
30
+ each.to_a
31
+ end
32
+
33
+ def ratio
34
+ base_size ** (1.0/partitions)
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module Diapason
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: diapason
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Felipe Zavan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: wavefile
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.6.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.6.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.10.3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.10.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.4'
83
+ description:
84
+ email:
85
+ - felipe@zavan.me
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - MIT-LICENSE
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - diapason.gemspec
100
+ - lib/diapason.rb
101
+ - lib/diapason/note.rb
102
+ - lib/diapason/scale.rb
103
+ - lib/diapason/sound.rb
104
+ - lib/diapason/sound/sine_wave.rb
105
+ - lib/diapason/sound/sinusoidal_wave_sampler.rb
106
+ - lib/diapason/sound/wave.rb
107
+ - lib/diapason/sound/wave_file_player.rb
108
+ - lib/diapason/sound/wave_file_writer.rb
109
+ - lib/diapason/sound/wave_sampler.rb
110
+ - lib/diapason/tuning.rb
111
+ - lib/diapason/tuning/equal_temperament.rb
112
+ - lib/diapason/version.rb
113
+ homepage: http://github.com/zavan/diapason
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.4.8
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: Music with ruby.
137
+ test_files: []