yurez_nsk

Nokia 3310 Composer by GrakovNe

Jan 28th, 2015
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.97 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import wave
  5. from struct import pack
  6. import math
  7. import re
  8.  
  9. print "\n ================================\n| Nokia Composer Emulator v 1.00 |\n| Author: GrakovNe               |\n| Date:14-12-2014                |\n| Web: http://grakovne.org       |\n ================================\n"
  10.  
  11.  
  12. from sys import argv
  13. Arguments = argv[1:]
  14. def Parse_Tone(Note):
  15.     Note = Note.upper()
  16.     if Note.find("-") == -1:
  17.         try:
  18.             (Duration, Octave) = re.findall(r"[0-9]+", Note)
  19.         except:
  20.             pass
  21.     else:
  22.         Duration = re.findall(r"[0-9]+", Note)[0]
  23.         Octave = 1
  24.     Tone = re.findall(r"[A-Z,#,-]+", Note)[0]
  25.     Duration = int(Duration)
  26.     Octave = int(Octave)
  27.     if Note.find(".") != -1:
  28.         Duration = Duration/1.5
  29.    
  30.     return (32/Duration, Tone, Octave)
  31.  
  32. Frames = []
  33.  
  34. Notes = {"-" : 0 ,"C" : 261.626, "#C" : 277.183, "D" : 293.665, "#D" : 311.127, "E": 329.628, "#E" : 349.228, "F" : 349.228, "#F" : 369.994, "G" : 391.995, "#G" : 415.305, "A" : 440.000, "#A" : 466.164, "B" : 493.883, "#B" : 523.251}
  35.  
  36. def Clear():
  37.     Frames = []
  38.  
  39. def Append_Freq(VOL,FREQ, TIME):
  40.     for i in range(0,TIME/10*441):
  41.         Result = (32765*VOL*math.sin(6.28*FREQ*i/44100))
  42.         Frames.append(Result)
  43.  
  44.  
  45. def Append_Note(VOL, TIME, NOTE, OCTAVE):
  46.     for i in range(0,int(TIME/10.0*441)):
  47.         FREQ = Notes[NOTE]*OCTAVE
  48.         Result = (32765*VOL*math.sin(6.28*FREQ*i/44100))
  49.         Frames.append(Result)
  50.        
  51.         #making clear sound
  52.     if (abs(math.sin(6.28*FREQ*i/44100))>0.01):
  53.         while (abs(math.sin(6.28*FREQ*i/44100))>0.01):
  54.             Result = (32765*VOL*math.sin(6.28*FREQ*i/44100))
  55.             Frames.append(Result)
  56.             i+=1
  57.  
  58. def Append_Notes(VOL, LIST, BPM):
  59.     for Each in LIST:
  60.         (Duration, Tone, Octave) = Parse_Tone(Each)
  61.         try:
  62.             Append_Note(VOL, int(Duration*1000*7.5/BPM), Tone, Octave) 
  63.         except:
  64.             print "Ошибка! Не могу обработать %s" %Each
  65.         Append_Note(0, int(250*7.5/BPM), "-", 1)
  66.  
  67. def Write_Wave(Name):
  68.     File = wave.open(Name, 'w')
  69.     File.setparams((1, 2, 44100, 0, 'NONE', 'not compressed'))
  70.     Result = []
  71.     for frame in Frames:
  72.         Result.append(pack('h', frame))
  73.     for Each in Result:
  74.         File.writeframes(Each)
  75.  
  76. Frames = []
  77. Append_Freq(1, 4000, 5000)
  78. Write_Wave('Sound.wave')
  79.  
  80. def MakeTune():
  81.  
  82.     if (len(Arguments)!=3):
  83.         print 'ERROR!\n USAGE:\n    Composer "Notes" BMP FileName\nExample:\n   Composer "16c2 16#a1 4c2 2f1 16#c2 16c2 8#c2 8c2 2#a1 16#c2 16c2 4#c2 2f1 16#a1 16#g1 8#a1 8#g1 8g1 8#a1 2#g1 16g1 16#g1 2#a1 16#g1 16#a1 8c2 8#a1 8#g1 8g1 4f1 4#c2 1c2 16c2 16#c2 16c2 16#a1 1c2" 120 Music.wave'
  84.         return 1
  85.  
  86.     List = Arguments[0].split(' ')
  87.  
  88.     BPM = int(Arguments[1])
  89.  
  90.     OutFile = Arguments[2]
  91.    
  92.     print "\nFile information:\n\n  Note number: %s\n   Tempo: %s BPM\n\nGeneration of amplitude..." % (len(List), BPM)
  93.  
  94.     Append_Notes(1, List, BPM)
  95.     print "\nOk!\n\nWriting Wave File..."
  96.     Write_Wave(OutFile)
  97.     File = open(OutFile,'rb').read()
  98.     Size = len(File)
  99.     print "\n   File size: %.2f MB\n    Duration: %.2f c. \n\nAll Done." % (Size/1024.0/1024, Size/44100/2)
  100.  
  101. MakeTune()
Advertisement
Add Comment
Please, Sign In to add comment