Python Forum
Project to create music
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Project to create music
#1
Good evening
I am a student and I have a computer project in python. The goal is to create music from a given score. So I tried to make the code below but I have a problem in my "créer_harmonique" function, the error message indicates that there is a tuple in the cosinus but I don't see why? Plus would you know how I can read my sound file once the file is created? Thank you in advance for your answers. Big Grin

from scipy.io import wavfile as wav
import numpy as np
import math


def fréquence_gamme(Notes):
    for i in range(len(Demi_ton)):
        frequence.append(fLa/(2**(Demi_ton[i]/12)))
    return(frequence)


def fréquences_partition(partition):
    for i in range(len(partition)):
        if Notes[0]==partition[i]:
            partition[i]=frequence[0]

        elif Notes[1]==partition[i]:
            partition[i]=frequence[1]

        elif Notes[2]==partition[i]:
            partition[i]=frequence[2]

        elif Notes[3]==partition[i]:
            partition[i]=frequence[3]

        elif Notes[4]==partition[i]:
            partition[i]=frequence[4]

        elif Notes[5]==partition[i]:
            partition[i]=frequence[5]

        elif Notes[6]==partition[i]:
            partition[i]=frequence[6]

        mon_son.append(int(partition[i]))

    return(mon_son)


def creer_harmonique(fLa,mon_son,freq_echant):
    #'Génére un signal sinusoidal.'
    echantillons=[]
    A=32767
    for i in range(len(mon_son)):
        duree_signal=1
        n_échant=int(duree_signal*freq_echant)
        for j in range(n_échant):
            t=int(i/freq_echant) # t=durée entre deux echantillons
            a=2*3*mon_son[i]*t
            v = A * math.cos(a)
            echantillons.append(v)
    return(echantillons)



fLa=440
freq_echant=44100

Notes=['Si','Do','Re','Mi','Fa','Sol','La']
Demi_ton=[10,9,7,5,4,2,0]
mon_son=[]
frequence=[]

partition=['Do','Mi','Sol','Do','Mi','Sol','Do','Mi','Do','Mi','Sol','Do','Mi','Sol','Do','Mi','Do','Re','La','Re','Fa','La','Re','Fa','Do','Re','La','Re','Fa','La','Re','Fa','Re','Si','Re','Si','Do']
h = creer_harmonique(fLa,partition,freq_echant)
wav.write("mon_son_a_moi",44100,np.array(h,np.int16))
Larz60+ write Feb-04-2021, 02:53 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2
Quote:the error message indicates that there is a tuple in the cosinus but I don't see why
Please show the entire error message (unmodified) in error tags.
It contains information that is important for discovering the problem.
Reply
#3
partition = ['Do', 'Mi', 'Sol', 'Do', 'Mi', 'Sol', 'Do', 'Mi', 'Do', 'Mi', 'Sol', 'Do', 'Mi', 'Sol', 'Do', 'Mi', 'Do',
             'Re', 'La', 'Re', 'Fa', 'La', 'Re', 'Fa', 'Do', 'Re', 'La', 'Re', 'Fa', 'La', 'Re', 'Fa', 'Re', 'Si', 'Re',
             'Si', 'Do']
h = creer_harmonique(fLa, partition, freq_echant)
def creer_harmonique(fLa, mon_son, freq_echant):
a = 2 * 3 * mon_son[i] * t
v = A * math.cos(a)
Error:
TypeError: must be real number, not str
so a is str. Smile
buran write Feb-04-2021, 05:29 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
m270 likes this post
Reply
#4
Also, you're not using fLa. Big Grin
Reply
#5
Thanks for your reply but I don't understand why it has become a character string so I'm not used to it before. Do you know why?
And you're right I don't need to put fLa in my function.
Reply
#6
There are several thing wrong here:
  1. you cannot call a function before it is defined
  2. Function internals must be indented.
  3. fLa has not been defined
In the following code, you must still define fLa
partition = [
    'Do', 'Mi', 'Sol', 'Do', 'Mi', 'Sol', 'Do', 'Mi', 'Do', 'Mi', 'Sol', 'Do', 
    'Mi', 'Sol', 'Do', 'Mi', 'Do', 'Re', 'La', 'Re', 'Fa', 'La', 'Re', 'Fa', 
    'Do', 'Re', 'La', 'Re', 'Fa', 'La', 'Re', 'Fa', 'Re', 'Si', 'Re', 'Si', 'Do']

def creer_harmonique(fLa, mon_son, freq_echant):
    a = 2 * 3 * mon_son[i] * t
    v = A * math.cos(a)

h = creer_harmonique(fLa, partition, freq_echant)
Error:
Traceback (most recent call last): File "/media/captainkirk/Projects/projects/QRST/T/TryStuffNew/src/Feb_4_2021_1.py", line 10, in <module> h = creer_harmonique(fLa, partition, freq_echant) NameError: name 'fLa' is not defined
Reply
#7
A variable in a function is a local variable.
a=1
def hello(a):
    print(a)
hello(2)
Output:
2
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020