URL: https://linuxfr.org/users/fantome_asthmatique/journaux/batterie-et-rock-n-roll-goret-style Title: Batterie et Rock'n'Roll, goret style Authors: fasthm Date: 2018年01月08日T21:44:42+01:00 License: CC By-SA Tags: batterie, rockband, joystick, python, pygame et diy Score: 22 TilK avait publié 2 excellents journaux racontant comment il avait transformé une « batterie » pour Rock Band (un jeu video) en batterie MIDI : https://linuxfr.org/users/tilk/journaux/orange-batterie-et-rock-and-roll https://linuxfr.org/users/tilk/journaux/orange-batterie-et-rock-and-roll-partie-2 Il se trouve que j'ai la même batterie dans ma cave, et que le marmot l'a repérée ce soir... il a bien sûr fallu remonter l'engin et le transformer en simili batterie Playskool. Pour référence, lsusb indique : Bus 002 Device 009: ID 12ba:0210 Licensed by Sony Computer Entertainment America Harmonix Drum Kit for PlayStation(R)3 Ça se comporte comme un joystick de Playstation. Là où Tilk a fait un travail propre, réfléchi, et élégant (matériel dédié, intégration avec alsa, possibles évolution), j'ai plutôt pondu dans l'urgence un code parfaitement goret, constitué essentiellement de copiés/collés de trucs piqués ça et là sur github ou stackexchange (ne niez pas, vous aussi vous faîtes ça parfois !), qui a néanmoins le mérite de satisfaire un enfant de 3 ans et demi. Je vous laisse trouver des samples par vous même si vous voulez essayer. Le seul point technique intéressant est l'initialisation du mixer de pygame en deux temps qui permet de ne pas avoir de latence... et je suis bien incapable de vous expliquer pourquoi. Je vous avais prévenu, c'est goret style :p ```python #!/usr/bin/env python # -*- coding: utf8 import sys import pygame from pygame.locals import * # mixer def init_mixer(): #pygame.mixer.init() -> latence importante # pre_init/init/quit/init -> latence très faible... pygame.mixer.pre_init(22050, -16, 2, 1024) pygame.mixer.init() pygame.mixer.quit() pygame.mixer.init(22050, -16, 2, 1024) # joystick def init_joystick(): pygame.joystick.init() joystick_count = pygame.joystick.get_count() if joystick_count == 0: print("No joystick detected") return False pygame.joystick.Joystick(0).init() return True def quit(code=0): pygame.quit() sys.exit(code) # start here pygame.init() pygame.display.init() w,h=320,200 r,g,b=0,0,0 background = (r,g,b) screen = pygame.display.set_mode((w,h)) screen.fill((background)) pygame.display.flip() if not init_joystick(): quit(1) init_mixer() sample = { 2 : pygame.mixer.Sound('Ensoniq-ESQ-1-Hi-Synth-Tom.wav'), # rouge 3 : pygame.mixer.Sound('Ensoniq-SQ-1-Ride-Cymbal.wav'), # jaune 0 : pygame.mixer.Sound('Hi-Bongo.wav'), # bleu 1 : pygame.mixer.Sound('High-Conga-1.wav'), # vert 4 : pygame.mixer.Sound('Closed-Hi-Hat-7.wav'), # pédale #4 : pygame.mixer.Sound('Bass-Drum-1.wav'), } done = False while not done: for event in pygame.event.get(): if event.type == pygame.JOYBUTTONDOWN: print event.button try: sample[event.button].play() except Exception as ex: pass elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: done = True elif event.type == pygame.QUIT: done = True elif event.type == pygame.JOYHATMOTION: (x,y) = event.value # au cas où ça soit utile un jour print event.hat, x, y quit() ```