Seite 1 von 1

Beep ...

Verfasst: Mittwoch 3. August 2005, 14:46
von xStream
Habe gestern ein nettes Python Modul entdeckt ;)

Code: Alles auswählen

import winsound
import time

beat = 1000

c = 262
d = 294
e = 330
f = 349
g = 392
a = 440

winsound.Beep(c, beat / 4)
time.sleep(0.1)
winsound.Beep(d, beat / 4)
time.sleep(0.1)
winsound.Beep(e, beat / 4)
time.sleep(0.1)
winsound.Beep(f, beat / 4)
time.sleep(0.1)
winsound.Beep(g, beat / 2)
time.sleep(0.1)
winsound.Beep(g, beat / 2)
time.sleep(0.1)
winsound.Beep(a, beat / 4)
time.sleep(0.1)
winsound.Beep(a, beat / 4)
time.sleep(0.1)
winsound.Beep(a, beat / 4)
time.sleep(0.1)
winsound.Beep(a, beat / 4)
time.sleep(0.1)
winsound.Beep(g, beat / 1)
time.sleep(0.1)
winsound.Beep(a, beat / 4)
time.sleep(0.1)
winsound.Beep(a, beat / 4)
time.sleep(0.1)
winsound.Beep(a, beat / 4)
time.sleep(0.1)
winsound.Beep(a, beat / 4)
time.sleep(0.1)
winsound.Beep(g, beat / 1)
time.sleep(0.1)
winsound.Beep(f, beat / 4)
time.sleep(0.1)
winsound.Beep(f, beat / 4)
time.sleep(0.1)
winsound.Beep(f, beat / 4)
time.sleep(0.1)
winsound.Beep(f, beat / 4)
time.sleep(0.1)
winsound.Beep(e, beat / 2)
time.sleep(0.1)
winsound.Beep(e, beat / 2)
time.sleep(0.1)
winsound.Beep(d, beat / 4)
time.sleep(0.1)
winsound.Beep(d, beat / 4)
time.sleep(0.1)
winsound.Beep(d, beat / 4)
time.sleep(0.1)
winsound.Beep(d, beat / 4)
time.sleep(0.1)
winsound.Beep(c, beat / 1)
time.sleep(0.1)

Verfasst: Mittwoch 3. August 2005, 22:08
von BlackJack
Das wirkt wesentlich augeräumter, wenn man Code und Daten trennt:

Code: Alles auswählen

import winsound 
import time 

beat = 1000 

c = 262 
d = 294 
e = 330 
f = 349 
g = 392 
a = 440 

song = ((c, 4), (d, 4), (e, 4), (f, 4), (g, 2), (g, 2)) # und so weiter...

for note, duration in song:
    winsound.Beep(note, beat / duration)
    time.sleep(0.1)
Dann kann man auch viel einfacher die Musik ändern, oder mehrere Stücke zur Auswahl anbieten, von Platte laden usw.

Verfasst: Mittwoch 3. August 2005, 22:37
von xStream
Vielen Dank, der viele Code hat mich auch gestört ;)

Verfasst: Samstag 27. Juni 2009, 22:03
von Pascal
Ist zwar schon ein sehr alter Theard, aber ich hab auch etwas dazu beizusteuern.

Code: Alles auswählen

import winsound 
import time 

beat = 1000 

c = 262 
d = 294 
e = 330 
f = 349
fis=370
g = 392 
a = 440
h = 486
c2= 500
d2= 520




tonleiter= ((c, 2, 'c' ) , (d, 2, 'd' ) , (e, 2, 'e' ) , (f, 2, 'f' ) , (g, 2, 'g' ) , (a, 2, 'a' ),  \
            (h, 2, 'h' ) , (c2, 2, 'c2' )) 

# Happy Birthday:
name='Na-me'
assert name.split('-')[1] == name.split('-')[-1]
song1= ((d, 4, 'Hap-'), (d, 4, 'py '), (e, 2, 'Birth-'), (d, 3, 'day'), (g, 3, 'to '), (fis, 2, 'you'), \
        (d, 4, '\nHap-'), (d, 4, 'py '), (e, 2, 'Birth-'), (d, 3, 'day'), (a, 3, 'to'), (g, 2, 'you'), \
        (d, 4, '\nHap-'), (d, 4, 'py '), (d2, 3, 'Birth-'), (h, 3, 'day'), \
        (g, 3, 'dear'), (fis, 3, name.split('-')[0]+'-'),  (e, 3, name.split('-')[1]), \
        (c2, 3, '\nHap-'), (c2, 3, 'py'), (h, 3, 'Birth-'), (g, 3, 'day'), (a, 3, 'to'), (g, 1, 'youuuu'))




for note, duration, text in song1:
    print text, 
    winsound.Beep(note, beat / duration)
    time.sleep(0.1)

Allerdings stimmen die töne nicht ganz, vllt kann da jemand helfen :idea:

Verfasst: Dienstag 7. Juli 2009, 14:58
von krisi12345
Toll!!! :D

Verfasst: Dienstag 7. Juli 2009, 15:01
von krisi12345
Hmm...
Also Ich hab momentan kein Bock das selber zu machen aber man könnte es doch aus einer Datei abspielen.

Verfasst: Mittwoch 8. Juli 2009, 13:10
von jens
MIDI Datei einlesen und "übersetzten" :P

Verfasst: Mittwoch 8. Juli 2009, 18:34
von Poolboy
print text,

was macht print text? ich seh da keine Variable für 'text'?
Bin noch Anfänger ;)

Verfasst: Mittwoch 8. Juli 2009, 18:37
von krisi12345
Zeile 35!?:

Code: Alles auswählen

for note, duration, text in song1: 

Verfasst: Mittwoch 8. Juli 2009, 19:32
von lunar
@Poolboy, krisi12345:
Beides ist im Handbuch erklärt, letzteres unter dem Stichwort "Tuple Unpacking".

Verfasst: Dienstag 14. Juli 2009, 20:36
von jerch
Beeping mal anders:

Code: Alles auswählen

import sys, re
from time import sleep

REGEX = re.compile(r'([a-g][b#]?[1-2]):([1-6])')

TONE_MAP = {
'c1': 60, 'c#1': 61, 'db1': 61, 'd1': 62, 'd#1': 63, 'eb1': 63, 'e1': 64,
'e#1': 65, 'f1': 65, 'f#1': 66, 'gb1': 66, 'g1': 67, 'g#1': 68, 'ab1': 68,
'a1': 69, 'a#1': 70, 'bb1': 70, 'b1': 71, 'cb2': 71, 'c2': 72, 'c#2': 73,
'db2': 73, 'd2': 74, 'd#2': 75, 'eb2': 75, 'e2': 76, 'e#2': 77, 'f2': 77,
'f#2': 78, 'gb2': 78, 'g2': 79, 'g#2': 80}

# p = 69 + 12 * lb(f/440)
def pitch_flat(num, pitch):
  return (num, (2**((float(num)-69)/12))*pitch)

def beep_play(s, pitch=440, metrum=200, freqs=None):
    if not freqs:
        freqs = dict(pitch_flat(i, pitch) for i in xrange(60,81))
    for i in re.findall(REGEX, s):
        sys.stdout.write(("\033[10;%d]\033[11;%d]\007") %
                         (freqs[TONE_MAP[i[0]]], float(i[1])*metrum))
        sys.stdout.flush()
        sleep(float(i[1])*metrum/1000)


s = """

 a1:2 | c2:4 d2:2 | e2:3 f2:1 e2:2  | d2:4 b1:2  | g1:3 a1:1 b1:2 |
        c2:4 a1:2 | a1:3 g#1:1 a1:2 | b1:4 g#1:2 | e1:4

    """

beep_play(s)
Wer weiß, was s ist, hat gewonnen!

Verfasst: Mittwoch 11. November 2009, 17:56
von Nocta
Man kann die Halbtonschritte auch ausrechnen, ich glaube das ging mit frequenz * 2^(x/12) oder so ähnlich.
Wobei x für die Halbtonschritte steht.
Jedenfalls irgendwas mit zwölfter Wurzel (da eine Oktave aus 12 Halbtonschritten besteht)

Ich hatte mal ein kleines Programm geschrieben, das mir auf dieser Basis jede Tonleiter abspielen konnte. Ich hab dann auch zum Spaß einen Teil vom Star Wars Main Theme "einprogrammiert".

Vielleicht schreib ich noch mal sowas in der Richtung und poste es hier :)

Ich weiß, etwas alt das Thema

Edit:

Code: Alles auswählen

(freq * 2**((halbtonschritte/12)))