CONNEXION
  • RetourJeux
    • Sorties
    • Hit Parade
    • Les + populaires
    • Les + attendus
    • Soluces
    • Tous les Jeux
    • Gaming
  • RetourActu Gaming
    • News
    • Astuces
    • Tests
    • Previews
    • Toute l'actu gaming
  • RetourBons plans
    • Bons plans
    • Bons plans Smartphone
    • Bons plans Hardware
    • Bons plans Image et Son
    • Bons plans Amazon
    • Bons plans Cdiscount
    • Bons plans Decathlon
    • Bons plans Fnac
    • Tous les Bons plans
  • RetourJVTech
    • Actus High-Tech
    • Intelligence Artificielle
    • Smartphones
    • Mobilité urbaine
    • Hardware
    • Image et son
    • Tutoriels
    • Tests produits High-Tech
    • Guides d'achat High-Tech
    • JVTech
  • RetourCulture
    • Actus Culture
    • Culture
  • RetourVidéos
    • A la une
    • Gaming Live
    • Vidéos Tests
    • Vidéos Previews
    • Gameplay
    • Trailers
    • Chroniques
    • Replay Web TV
    • Toutes les vidéos
  • RetourForums
    • Hardware PC
    • PS5
    • Switch 2
    • Xbox Series
    • Switch
    • Pokemon pocket
    • FC 25 Ultimate Team
    • League of Legends
    • Tous les Forums
  • PC
  • PS5
  • Xbox Series
  • Switch 2
  • PS4
  • One
  • Switch
  • iOS
  • Android
  • MMO
  • RPG
  • FPS
En ce moment Genshin Impact Valhalla Breath of the wild Animal Crossing GTA 5 Red dead 2
Liste des sujets

[Python] Problème avec une variable...

Pseudo supprimé
Pseudo supprimé 25 mai 2015 à 17:12:12

Salut à tous déjà :noel:

Pour mon projet d'ISN cette année, avec notre groupe, on doit coder un petit synthétiseur.
On est presque à la fin, là je suis en train de tout "relier" ensemble (l'interface graphique et le code "d'en dessous") et je suis face à un problème.

Je vais mettre les parties du code qui sont intéressantes (elles sont dans l'ordre) :

# Merci Python... --'
volume = 1
freq = 0
onde = 1
attack = 0
release = 0
octave = 0
Wave = 0

def ApplyChanges() :
    global onde
    global attack
    global release
    global volume
    global octave

    attack = AttackSlider.get()
    attack = int(attack)
    print attack
AttackSlider = DoubleVar()
Attack = Scale(Attfram, from_=127, to=0,background='black', fg='red',highlightbackground='gray4',activebackground='red',troughcolor='snow2',variable=AttackSlider)
Attack.pack()
Label(Attfram,text="Attack", fg='snow2',background='black').pack()
Apply = Button(Exfram, text="Appliquer", command=ApplyChanges, background='black', fg='green')
Apply.pack()
def printa() :
    print attack

button1 = Button(topframe,padx=0, height = 6, pady=0, bd=0, text="C#", bg="black",fg="white",command=lambda x=100,y=1,z=onde,a=int(attack),b=release,c=0:PlaySound(x,y,z,a,b,c))
#button1 = Button(topframe,padx=0, height = 6, pady=0, bd=0, text="C#", bg="black",fg="white",command=lambda:printa())
Fen_Controls.mainloop()
root.mainloop()

Fen_Controls.destroy()
pygame.quit()

Alors voilà le problème (enfin) :

Quand je clique sur le bouton "Apply" (qui appelle ApplyChanges() ), la valeur correcte de la variable attack (qui a été réglé via le scale, qui peut aller de 0 à 128) s'affiche avec la bonne valeur dans la console python.
Par contre, lorsque je clique sur le Button1, je constate (avec la fonction qui est appellée) que la variable reste à 0, quoi qu'il arrive.

Encore plus bizarre : si par contre, je commente ce bouton-là et que je décommente l'autre bouton (qui lui appelle la fonction printa() ), la valeur affichée par printa() est bel et bien la bonne.
Alors que l'autre version du bouton envoie 0 comme valeur... et je ne vois vraiment pas pourquoi. :(

Merci d'avance pour votre aide si vous avez une solution, car pour moi cela dépasse la logique. :(

(Je vous mets le code complet du programme si vous voulez, mais sachez quand même que c'est un gros bordel, surtout après les 2h d'essais que je viens de faire pour comprendre) :


import pygame
from Tkinter import *
from pygame.locals import *

import math
import numpy

pygame.init()
size = (50, 50)

bits = 16
pygame.mixer.pre_init(4096, -bits, 2, 4)
pygame.init()
_display_surf = pygame.display.set_mode(size, pygame.HWSURFACE | pygame.DOUBLEBUF)


# Merci Python... --'
volume = 1
freq = 0
onde = 1
attack = 0
release = 0
octave = 0
Wave = 0


def ApplyChanges() :
    global onde
    global attack
    global release
    global volume
    global octave

    attack = AttackSlider.get()
    attack = int(attack)
    print attack


#We define our window
Fen_Controls = Tk()
#Fen_Controls.configure(width=500,height=400)
Fen_Controls.title('Syrapi')
Canvas = Canvas(Fen_Controls, background='black')
Canvas.pack()

#These are the Frames, inside them is where the magic happens
Wavfram = Frame(Canvas, relief = GROOVE, background='black')
Wavfram.pack(side=LEFT, padx=30, pady=30)

Octfram = Frame(Canvas, relief = GROOVE, background='black')
Octfram.pack(side=LEFT, padx=10, pady=10)

Attfram = Frame(Canvas, relief = GROOVE, background='black')
Attfram.pack(side = LEFT, padx=10, pady=10)

Relfram = Frame(Canvas, relief = GROOVE, background='black')
Relfram.pack(side = LEFT, padx=10,pady=10)

Volfram = Frame(Canvas, relief = GROOVE, background='black')
Volfram.pack(side = LEFT, padx=10,pady=10)

Exfram = Frame(Canvas,relief = GROOVE, background='black')
Exfram.pack(side=LEFT, padx=30, pady=30)


#Now for all the buttons, scales and all the good stuff

Wave =StringVar()
Sine =Radiobutton(Wavfram, text="Sinusoïdale", variable=Wave, value =1,background='black', fg='red')
Square =Radiobutton(Wavfram, text="Carrée", variable=Wave, value =2,background='black', fg='red')
Sine.pack()
Square.pack()
Label(Wavfram, text="Forme d'onde", fg='snow2',background='black').pack()

Octave = Scale(Octfram,from_=2, to=-2,background='black', fg='red',highlightbackground='gray4',activebackground='red',troughcolor='snow2')
Octave.pack()
Label(Octfram,text="Octave", fg='snow2',background='black').pack()

AttackSlider = DoubleVar()
Attack = Scale(Attfram, from_=127, to=0,background='black', fg='red',highlightbackground='gray4',activebackground='red',troughcolor='snow2',variable=AttackSlider)
Attack.pack()
Label(Attfram,text="Attack", fg='snow2',background='black').pack()

Release = Scale(Relfram, from_=127, to=0,background='black', fg='red',highlightbackground='gray4',activebackground='red',troughcolor='snow2')
Release.pack()
Label(Relfram, text="Release", fg='snow2',background='black').pack()

Volume = Scale(Volfram, from_=100, to=0,background='black', fg='red',highlightbackground='gray4',activebackground='red',troughcolor='snow2')
Volume.pack()
Label(Volfram, text="Volume", fg='snow2', background='black').pack()

Apply = Button(Exfram, text="Appliquer", command=ApplyChanges, background='black', fg='green')
Apply.pack()

Exit = Button(Exfram, text="Exit", command=Fen_Controls.quit,background='black', fg='red')
Exit.pack()


def PlaySound(freq, volume, onde, attack, release, octave) :
    temps = 1 # en secondes
    volume = float(volume)
    freq = int(freq)
    onde = int(onde)
    attack = int(attack*16) # On multiplie par 16 car le slider va de 0 ?? 128, alors que le son est de 4096 b/s
    release = int(4096-release*16)
    ampli = 1 # Variable servant pour l'attaque et release
    octave = 0

    samples = 4096 # bits par seconde. Ne pas toucher !
    n_samples = int(round(temps*samples)) # Nombre total de bits

    tableau = numpy.zeros((n_samples, 2), dtype = numpy.int16) # Cr??ation du tableau, rempli de 0
    max_sample = 2**(bits - 1) - 1

    for s in range(n_samples):
        x = float(s)/samples # le x pour les fonctions math??matiques

        # Attaque
        if s < attack : # Si on est dans la phase attaque : ok
            ampli = float(s+1)/float(attack+1)

        # Release
        if s > release : # Si on est dans la phase release : ok
            ampli = float(samples-s+1)/float(samples-release+1)


        if onde == 1 : # sinuso??dale
            tableau[s][0] = int(round(max_sample*math.sin(math.pi*freq*x)*volume*ampli))
            tableau[s][1] = int(round(max_sample*math.sin(math.pi*freq*x)*volume*ampli))
        elif onde == 2 : # carr??e
            if (int(round(max_sample*math.sin(math.pi*freq*x)*volume*ampli))) >= 0 : # si oui = phase 1
                tableau[s][0] = int(round(max_sample*volume*ampli))
                tableau[s][1] = int(round(max_sample*volume*ampli))
            else : # sinon = phase 0
                tableau[s][0] = int(0)
                tableau[s][1] = int(0)

    sound = pygame.sndarray.make_sound(tableau)
    sound.play(loops = 0)


root = Tk()
topframe=Frame(root)
topframe.pack()

root.title('PIANO')
num1=StringVar()

topframe=Frame(root)

topframe.pack(side=TOP)
txtdisplay=Entry(topframe,textvariable = num1,bd=20,insertwidth=1,font=30 ,justify= 'center',width=4,)
txtdisplay.pack(side=TOP)

def printa() :
    print attack

button1 = Button(topframe,padx=0, height = 6, pady=0, bd=0, text="C#", bg="black",fg="white",command=lambda x=100,y=1,z=onde,a=int(attack),b=release,c=0:PlaySound(x,y,z,a,b,c))
#button1 = Button(topframe,padx=0, height = 6, pady=0, bd=0, text="C#", bg="black",fg="white",command=lambda:printa())
button1.pack(side=LEFT)
button22 = Button(topframe, state=DISABLED,height=7,width=1,padx=0, pady=0, relief=RIDGE)
button22.pack(side=LEFT)
button2= Button(topframe,padx=0, height = 6, pady=0, bd=0, text="D#", bg="black",fg="white")
button2.pack(side=LEFT)
button22= Button(topframe,state=DISABLED,height=7, width=1,padx=0,pady=0, relief= RIDGE)
button22.pack(side=LEFT)
button3 = Button(topframe,padx=0, height = 6, pady=0, bd=0, text="f#" , bg="black",fg="white")
button3.pack(side=LEFT)
button22= Button(topframe,state=DISABLED,height=7, width=1,padx=0,pady=0, relief=RIDGE)
button22.pack(side=LEFT)
button4= Button(topframe,padx=0,height= 6,pady=0,bd=0,text="G#", bg="black",fg="white")
button22= Button(topframe,state=DISABLED,height=7, width=1,padx=0,pady=0, relief=RIDGE)
button22.pack(side=LEFT)
button2= Button(topframe,padx=0, height = 6, pady=0, bd=0, text="D#", bg="black",fg="white")
button2.pack(side=LEFT)


Fen_Controls.mainloop()
root.mainloop()

Fen_Controls.destroy()
pygame.quit()
FrenchDeath
FrenchDeath
Niveau 10
26 mai 2015 à 19:31:29

je vient de tester le code que tu a m'envoyer , je n'ai aucun de tes problème , ce qui est étrange O_o
la variable attack est bien de la valeur que j'ai indiquer

Pseudo supprimé
Pseudo supprimé 26 mai 2015 à 21:26:59

Normalement, si le code marche bien, si on modifie le slider associé à la variable attack, le son qu'on obtient arrive en "fondu" (il met un petit peu de temps à venir)
C'est bien ça que tu as ?

Enfin de toute façon c'est pas très grave, car finalement on ne passera pas avec le code "intégral" du projet, mais juste avec les codes de nos parties ^^

Sous forums
  • Aide à l'achat Mac
  • Macintosh
  • Création de Jeux
  • Programmation
  • Création de sites web
  • Linux
  • Internet
  • Steam Deck
  • Hardware
La vidéo du moment