Commits

uniqx committed 78b3fe8

first steps with algorithmic oneline music

  • Participants

Comments (0)

Files changed (3)

File algorithmic-oneline-music/.hgignore

+.*.pyc$
+.*.pyo$
+.*.wav$

File algorithmic-oneline-music/oneline.py

+#! /usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+#
+#           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+#                   Version 2, December 2004
+#
+#  Copyright (C) 2011 uniqx
+#   Metalab, 1010 Vienna, Austria
+#  Everyone is permitted to copy and distribute verbatim or modified
+#  copies of this license document, and changing it is allowed as long
+#  as the name is changed.
+#
+#             DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+#    TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+#
+#   0. You just DO WHAT THE FUCK YOU WANT TO.
+#
+
+import wave
+import array
+import math
+
+def make_wav(func,frame_count=240000,Hz=8000,bytes_per_sample=1,
+             filename='out.wav',print_frames=False):
+  """
+  func             - on line function generating the sound, it must accept
+                     exactly one integer parameter representing the frame
+                     number, and return the coresponding amplitude for that
+                     frame ( 0 <= result <= 2**(8*bytes_per_sample)-1 ).
+  frame_count      - number of frames to be generated (default 240000)
+  Hz               - Hertz of the wave (defualt 8000Hz)
+  bytes_per_sample - number of bytes per sameple ... 0o
+  filename         - file name of the generated wav file
+                     (existing files will be overwritten)
+  print_frames     - prints the frames to the screen when running.
+  """
+  
+  try:
+    
+    w = wave.open(filename,'w')
+    
+    w.setnchannels(1)
+    w.setsampwidth(bytes_per_sample)
+    w.setframerate(Hz)
+    w.setnframes(0)
+    
+    module = 2**(8*bytes_per_sample)
+    
+    x = []
+    for t in range(frame_count):
+      
+      x.append(int(math.floor(func(t))%module))
+      if print_frames:
+        print x[-1], '...', module
+    
+    w.writeframes(array.array('B',x))
+    
+  finally:
+    
+    w.close()
+

File algorithmic-oneline-music/viznut_demo.py

+#! /usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+#
+#           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+#                   Version 2, December 2004
+#
+#  Copyright (C) 2011 uniqx
+#   Metalab, 1010 Vienna, Austria
+#  Everyone is permitted to copy and distribute verbatim or modified
+#  copies of this license document, and changing it is allowed as long
+#  as the name is changed.
+#
+#             DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+#    TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+#
+#   0. You just DO WHAT THE FUCK YOU WANT TO.
+#
+
+#
+# generator function by viznut.
+# imprudent copied from: https://www.youtube.com/watch?v=qlrs2Vorw2Y
+#
+
+import oneline
+
+if __name__ == '__main__':
+  
+  def viznut(t):
+    return (t>>6|t|t>>(t>>16))*10+((t>>11)&7)
+  
+  oneline.make_wav(func=viznut,filename='viznut_demo.wav',frame_count=8000*240)
+  
+
+
+