Advertisement
JoelSjogren

Untitled

May 24th, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.96 KB | None | 0 0
  1. import ossaudiodev as oss
  2. import wave
  3. import array
  4. import numpy as np
  5. from math import *
  6.  
  7.  
  8. # Open the os sound output
  9. dsp = oss.open('/dev/dsp', 'w')
  10. rate = 44100
  11. dsp.setparameters(oss.AFMT_S16_NE, 1, rate)
  12.  
  13.  
  14. # This is the part of interest
  15. def play_tones(freqs, time=1):
  16.     sampc = int(rate*time)
  17.     s = sum(np.array([sin(t/rate*2*pi*f)*2**12 for t in range(sampc)]) \
  18.             for f in freqs) #/ len(freqs)
  19.    
  20.     # Smoothen
  21.     #s *= sum(np.sin(n*np.linspace(0, pi, sampc))/n for n in range(1, 2))
  22.     #s *= np.sin(np.linspace(0, pi, sampc))
  23.     s[0:rate//100] *= np.linspace(0, 1, rate//100)
  24.     s[-rate//50:] *= np.linspace(1, 0, rate//50)
  25.    
  26.     # Avoid overflow
  27.     if abs(s).max() > 2**14:
  28.         s = s / abs(s).max() * (2**15-1)
  29.    
  30.     s = s.astype('int16')
  31.     dsp.writeall(s.tostring())
  32.  
  33.    
  34. #play_tones([440, 440*2**(1/5)])
  35.  
  36. a, b, k = 12, 7, 9  # Please read main.py for an explanation
  37.  
  38. def play_fraction():
  39.     f = 440
  40.     for i in range(2*k+1):
  41.         play_tones([f], 0.7)
  42.         f *= a/b
  43.         if f >= 880*0.995:
  44.             f /= 2
  45.  
  46. """
  47. Repeated fractions don't quite make an octave.
  48.  
  49. >>> for i in range(10): print(log2(12/7)*9*i%9)
  50. ...
  51. 0.0
  52. 6.9984682079719684
  53. 4.996936415943937
  54. 2.9954046239159062
  55. 0.9938728318878738
  56. 7.992341039859845
  57. 5.9908092478318125
  58. 3.98927745580378
  59. 1.9877456637757476
  60. 8.986213871747715
  61. """
  62.  
  63. # round((a/b)**k)**(1/k) is the rounded frequency interval
  64.  
  65. """
  66. Disturbed fractions get close enough to an octave.
  67.  
  68. >>> oq = round((a/b)**k)**(1/k)
  69. >>> for i in range(10): print(log2(oq)*k*i%k)
  70. ...
  71. 0.0
  72. 6.999999999999999
  73. 4.999999999999998
  74. 2.9999999999999964
  75. 0.9999999999999964
  76. 7.999999999999993
  77. 5.999999999999993
  78. 3.999999999999993
  79. 1.999999999999993
  80. 8.999999999999993
  81.  
  82. """
  83.  
  84. oq = round((a/b)**k)**(1/k)
  85. cq = oq**4/8  # hard-coded
  86.  
  87. def lm2(x):
  88.     while x >= 1.995:
  89.         x /= 2
  90.     return x
  91.  
  92. def play_sparse_octave():
  93.     f = 440
  94.     for i in range(2*k+1):
  95.         print(i)
  96.         play_tones([f*lm2(oq**i)], 0.7)
  97.  
  98. def play_dense_octave():
  99.     f = 440
  100.     for i in range(k+1):
  101.         play_tones([f*cq**i], 0.7)
  102.  
  103. #play_dense_octave()
  104.  
  105. def play_abk_tones(tones, time=1, cq=cq):
  106.     #play_tones([440*2**(i/28) for i in tones], time)
  107.     play_tones([440*cq**i for i in tones], time)
  108.  
  109. def play_abk_sheet(sheet, pm=120):
  110.     for i, j in sheet:
  111.         play_abk_tones(i, j*60/pm)
  112.  
  113. """
  114. Some chords:
  115.    [0, 5, 3] like major
  116.    [0, 5, 2] like minor
  117.    [0, 5, 7] like sus2
  118.    [0, 5, 8] like aug
  119.    [0, 4] like sus4
  120.    [0, 4, 7] very pure
  121.    
  122.  
  123.  
  124. """
  125.  
  126. sheet = [
  127.     ([-14, -11, -9], 1),
  128.     ([-14, -11, -9, 0], .75),
  129.     ([-14, -11, -9, -1], .25),
  130.     ([-14, -11, -9, -2], 1),
  131.     ([-14, -11, -9, -5], 1),
  132.     ([-14, -12, -9, -6], 3),
  133.     ([-14, -12, -8], 1),
  134.     ([-15, -11, -8], 1),
  135.     ([-15, -11, -8], 1),
  136. ]
  137. #play_abk_sheet(sheet)
  138.  
  139.  
  140.  
  141.  
  142.  
  143. # Close the os sound output
  144. def close():
  145.     dsp.close()
  146.  
  147. if __name__ == '__main__':
  148.     close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement