IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Python Discussion :

Premier problème wave


Sujet :

Python

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Invité
    Invité(e)
    Par défaut Premier problème wave
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
     
    #!/usr/bin/env python 
    # -*- coding: utf-8 -*-
    # *
    # Application gammique évolutive
    # Opération = Envol système
    # ProgamV1epyco
    #
    from tkinter import *
    import winsound
    import wave, math, binascii
    # Partie échantillonnage
    fichwav = []
    toplo = []              # Le fichier présumé contenir le son de la note
    nbOctet = nbCanal = 1   #   # Mes lacunes: Premiers pas de conversion audio
    fech = 44100            #   # Ceci n'est que le premier problème
    niveau = float(1)
    duree = float(1/8)
    nbEch = int(duree*fech)
    for fy in range(7):
        manote = wave.open(toplo[fy],'wb')
        param = (nbCanal,nbOctet,fech,nbEch,'NONE','not compressed')
        manote.setparams(param)
        freq = 440 ; amp = 127.5*niveau
        for i in range(0,nbEch):
            val = wave.struct.pack('B',int(128.0 + amp*math.sin(2.0*math.pi*freq*i/fech)))
            manote.writeframes(val) # écriture frame
            fichwav.append(val)
        manote.close()
        for fw in range(7):
            print('wav',type(toplo[f]))
            print('wav',toplo[fw])
            winsound.PlaySound(fichwav[fw])
     
    Traceback (most recent call last):
      File "C:/Users/Vincent/Documents/developpez/develHM/tutos/progamvaudio.py", line 20, in <module>
        manote = wave.open(toplo[fy],'wb')
    IndexError: list index out of range
    J'aimerais bien un conseil pour parvenir à régler ce petit aléa

  2. #2
    Membre Expert
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2013
    Messages
    1 617
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 617
    Par défaut
    toplo est une liste vide pour laquelle tu parcours les indices de 0 à 6.

  3. #3
    Invité
    Invité(e)
    Par défaut
    Maintenant le message d'erreur est explicite, même si je le comprends...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
     
    #!/usr/bin/env python 
    # -*- coding: utf-8 -*-
    # *
    # Application gammique évolutive
    # Opération = Envol système
    # ProgamV1epyco
    #
    from tkinter import *
    import winsound
    import wave, math, binascii
    # Partie échantillonnage
    top = 0
    fichwav = []
    toplo = [top]              # Le fichier présumé contenir le son de la note
    nbOctet = nbCanal = 1   #   # Mes lacunes: Premiers pas de conversion audio
    fech = 44100            #   # Ceci n'est que le premier problème
    niveau = float(1)
    duree = float(1/8)
    nbEch = int(duree*fech)
    for fy in range(7):
        manote = wave.open(toplo[top],'wb')
        param = (nbCanal,nbOctet,fech,nbEch,'NONE','not compressed')
        manote.setparams(param)
        freq = 440 ; amp = 127.5*niveau
        for i in range(0,nbEch):
            val = wave.struct.pack('B',int(128.0 + amp*math.sin(2.0*math.pi*freq*i/fech)))
            manote.writeframes(val) # écriture frame
            fichwav.append(val)
        manote.close()
        for fw in range(7):
            print('wav',type(toplo[f]))
            print('wav',toplo[fw])
            winsound.PlaySound(fichwav[fw])
     
    #contenu = open(fichier, 'rb').read()
     
    >>> 
    Traceback (most recent call last):
      File "C:\Users\Vincent\Documents\developpez\develHM\tutos\progamvaudio.py", line 27, in <module>
        manote.writeframes(val) # écriture frame
      File "C:\Python34\lib\wave.py", line 426, in writeframes
        self.writeframesraw(data)
      File "C:\Python34\lib\wave.py", line 415, in writeframesraw
        self._ensure_header_written(len(data))
      File "C:\Python34\lib\wave.py", line 455, in _ensure_header_written
        self._write_header(datasize)
      File "C:\Python34\lib\wave.py", line 459, in _write_header
        self._file.write(b'RIFF')
    AttributeError: 'int' object has no attribute 'write'
    >>>

  4. #4
    Membre Expert
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2013
    Messages
    1 617
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2013
    Messages : 1 617
    Par défaut
    wave.open demande un fichier, non ?
    Là, tu lui mets une liste.

  5. #5
    Invité
    Invité(e)
    Par défaut
    Alors, je précise ma question : Comment déclarer mon fichier pour qu'il puisse faire l'affaire ?

  6. #6
    Expert éminent
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 743
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Architecte technique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2008
    Messages : 21 743
    Par défaut
    Citation Envoyé par marco056 Voir le message
    wave.open demande un fichier, non ?
    Là, tu lui mets une liste.
    Avec top initialisé à 0, toplo[top] retourne l'entier 0.
    Ce qui est surprenant, c'est que wave.open ne plante pas, çà se vautre bien plus tard:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
            manote.writeframes(val) # écriture frame
      File "C:\Users\Vincent\Documents\developpez\develHM\tutos\progamvaudio.py", line 27, in <module>
        manote.writeframes(val) # écriture frame
      ...
      File "C:\Python34\lib\wave.py", line 459, in _write_header
        self._file.write(b'RIFF')
    AttributeError: 'int' object has no attribute 'write'
    justement parce qu'il a gardé cet entier "as a seekable file-like object".

    Citation Envoyé par toumus Voir le message
    Alors, je précise ma question : Comment déclarer mon fichier pour qu'il puisse faire l'affaire ?
    Un fichier est désigné par une chaine de caractères, pas par un entier.

    - W
    Architectures post-modernes.
    Python sur DVP c'est aussi des FAQs, des cours et tutoriels

  7. #7
    Invité
    Invité(e)
    Par défaut
    Je progresse...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
     
    #!/usr/bin/env python 
    # -*- coding: utf-8 -*-
    # *
    # Application gammique évolutive
    # Opération = Envol système
    # ProgamV1epyco
    #
    from tkinter import *
    import winsound
    import wave, math, binascii
    # Partie échantillonnage
    NomsFichiersSons = ['ado.wav','are.wav','ami.wav','afa.wav','aso.wav','ala.wav','asi.wav']
    freq = 220
    nbOctet = nbCanal = 1
    fech = 44100
    niveau = float(1)
    duree = float(1/8)
    nbEch = int(duree*fech)
    for fy in range(7):
        son = NomsFichiersSons[fy]
        manote = wave.open(son,'wb')
        param = (nbCanal,nbOctet,fech,nbEch,'NONE','not compressed')
        manote.setparams(param)
        freq += 10 ; amp = 127.5*niveau
        for i in range(0,nbEch):
            val = wave.struct.pack('B',int(128.0 + amp*math.sin(2.0*math.pi*freq*i/fech)))
            manote.writeframes(val) # écriture frame
        manote.close()
    for f in NomsFichiersSons:
        son2 = wave.open(f,'rb')
    son2.close()
    J'aimerais bien entendre les fichiers sons finalement créés
    Avec winsound c'est réalisable, ainsi :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
     
     
    #!/usr/bin/env python 
    # -*- coding: utf-8 -*-
    # *
    # Application gammique évolutive
    # Opération = Envol système
    # ProgamV1epyco
    #
    from tkinter import *
    import winsound
    import wave, math, binascii
    # Partie échantillonnage
    NomsFichiersSons = ['ado.wav','are.wav','ami.wav','afa.wav','aso.wav','ala.wav','asi.wav']
    freq = 220
    nbOctet = nbCanal = 1
    fech = 44100
    niveau = float(1)
    duree = float(1/8)
    nbEch = int(duree*fech)
    for fy in range(7):
        son = NomsFichiersSons[fy]
        manote = wave.open(son,'wb')
        param = (nbCanal,nbOctet,fech,nbEch,'NONE','not compressed')
        manote.setparams(param)
        freq += 10 ; amp = 127.5*niveau
        for i in range(0,nbEch):
            val = wave.struct.pack('B',int(128.0 + amp*math.sin(2.0*math.pi*freq*i/fech)))
            manote.writeframes(val) # écriture frame
        manote.close()
    for f in NomsFichiersSons:
        winsound.PlaySound(f,winsound.SND_FILENAME)
    Le problème sera évident lorsque je voudrais relier les notes pour les accords par exemple...
    Dernière modification par Invité ; 25/09/2015 à 21h20.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [XL-2007] Mon premier problème sous Excel
    Par Foreverpro dans le forum Excel
    Réponses: 2
    Dernier message: 01/03/2013, 19h50
  2. Premiers pas, premiers problèmes
    Par SPACHFR dans le forum Langage SQL
    Réponses: 5
    Dernier message: 19/06/2012, 10h34
  3. Premier problème Java
    Par ionys91 dans le forum Débuter avec Java
    Réponses: 6
    Dernier message: 27/10/2011, 18h38
  4. Question d'archi-débutant :VB6 vers VB.NET premier problème
    Par zedude33 dans le forum Windows Forms
    Réponses: 9
    Dernier message: 06/02/2007, 15h29

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo