Advertisement
chironex

Cython Dissc

Mar 16th, 2011
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.74 KB | None | 0 0
  1. import numpy as np
  2. cimport numpy as np
  3.  
  4. # DTYPE = np.float
  5. ctypedef np.float_t DTYPE_t
  6.  
  7. np.seterr(divide='raise', over='raise', under='ignore', invalid='raise')
  8.    
  9. """
  10. I define a timbre as the following 2d numpy array:
  11. [[f0, a0], [f1, a1], [f2, a2]...] where f describes the frequency
  12. of the given partial and a is its amplitude from 0 to 1. Phase is ignored.
  13. """
  14.  
  15. #Test Timbre
  16. # cdef np.ndarray[DTYPE_t,ndim=2] t1 = np.array( [[440,1],[880,.5],[(440*3),.333]])
  17.  
  18. # Calculates the inherent dissonance of one timbres of the above form
  19. # using the diss2Partials function
  20. cdef DTYPE_t diss1Timbre(np.ndarray[DTYPE_t,ndim=2] t):
  21.     cdef DTYPE_t runningDiss1
  22.     runningDiss1 = 0.0
  23.     cdef unsigned int len = np.shape(t)[0]
  24.     cdef unsigned int i
  25.     cdef unsigned int j
  26.     for i from 0 <= i < len:
  27.         for j from i+1 <= j < len:
  28.             runningDiss1 += diss2Partials(t[i], t[j])
  29.     return runningDiss1
  30.  
  31. # Calculates the dissonance between two timbres of the above form
  32. cdef DTYPE_t diss2Timbres(np.ndarray[DTYPE_t,ndim=2] t1, np.ndarray[DTYPE_t,ndim=2] t2):
  33.     cdef DTYPE_t runningDiss2
  34.     runningDiss2 = 0.0
  35.     cdef unsigned int len1 = np.shape(t1)[0]
  36.     cdef unsigned int len2 = np.shape(t2)[0]
  37.     runningDiss2 += diss1Timbre(t1)
  38.     runningDiss2 += diss1Timbre(t2)
  39.     cdef unsigned int i1
  40.     cdef unsigned int i2
  41.     for i1 from 0 <= i1 < len1:
  42.         for i2 from 0 <= i2 < len2:
  43.             runningDiss2 += diss2Partials(t1[i1], t2[i2])
  44.     return runningDiss2
  45.  
  46. cdef inline DTYPE_t float_min(DTYPE_t a, DTYPE_t b): return a if a <= b else b
  47.    
  48. # Calculates the dissonance of two partials of the form [f,a]
  49. cdef DTYPE_t diss2Partials(np.ndarray[DTYPE_t,ndim=1] p1, np.ndarray[DTYPE_t,ndim=1] p2):
  50.     cdef DTYPE_t f1 = p1[0]
  51.     cdef DTYPE_t f2 = p2[0]
  52.     cdef DTYPE_t a1 = abs(p1[1])
  53.     cdef DTYPE_t a2 = abs(p2[1])
  54.    
  55.     # In order to insure that f2 > f1:
  56.     if (f2 < f1):
  57.         (f1,f2,a1,a2) = (f2,f1,a2,a1)
  58.    
  59.     # Constants of the dissonance curves
  60.     cdef DTYPE_t _xStar
  61.     _xStar = 0.24
  62.     cdef DTYPE_t _s1
  63.     _s1 = 0.021
  64.     cdef DTYPE_t _s2
  65.     _s2 = 19
  66.     cdef DTYPE_t _b1
  67.     _b1 = 3.5
  68.     cdef DTYPE_t _b2
  69.     _b2 = 5.75
  70.    
  71.     cdef DTYPE_t a = float_min(a1,a2)
  72.     cdef DTYPE_t s = _xStar/(_s1*f1 + _s2)
  73.     return (a * (np.exp(-_b1*s*(f2-f1)) - np.exp(-_b2*s*(f2-f1)) ) )
  74.  
  75. cpdef dissTimbreScale(np.ndarray[DTYPE_t,ndim=2] t,np.ndarray[DTYPE_t,ndim=1] s):
  76.     cdef DTYPE_t currDiss
  77.     currDiss = 0.0;
  78.     cdef unsigned int i
  79.     for i from 0 <= i < s.size:
  80.         currDiss += diss2Timbres(t, transpose(t,s[i]))
  81.     return currDiss
  82.    
  83. cdef np.ndarray[DTYPE_t,ndim=2] transpose(np.ndarray[DTYPE_t,ndim=2] t, DTYPE_t ratio):
  84.     return np.dot(t, np.array([[ratio,0],[0,1]]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement