Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/jouer_notes.py
blob: aa285b69b5f6bef2f3a02823d48311a3a5eb26b2 (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#------------------------------------------------------------------------------

# Copyright 2008-2009 : François Sénéquier
# Email : francois.senequier@netcourrier.com

# This file is part of 'Theorie'.
#
# 'Theorie' is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# 'Theorie' is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with 'Theorie'.  If not, see <http://www.gnu.org/licenses/>.

#------------------------------------------------------------------------------

try:
	import pygame
except:
		print "Erreur d'importation du module Pygame !"

#------------------------------------------------------------------------------

def iniPygameMixer():
	try:
		pygame.mixer.pre_init(44100,-16,1,1024*4)
		pygame.mixer.init()
		nbr = pygame.mixer.get_num_channels()
		print "Module Pygame initialise : %s canaux disponibles" %(nbr)
	except:
		print "Erreur d'initialisation du module Pygame !"

def delPygameMixer():
	try:
		pygame.mixer.quit()
		print "Pygame desactive : OK !"
	except:
		print "Pygame desactive : erreur !"	

#------------------------------------------------------------------------------

def jouerEchantillonsMIX(lisFIC, maxTPS=1000, wait=True):
	"""
	Methode permettant de jouer le mixage de plusieurs echantillons references 
	par leurs noms (fichier)

	@param lisFIC: liste des noms des echantillons
	"""
	try:
		lisSON = map(lambda e: pygame.mixer.Sound(e), lisFIC)
		for son in lisSON:
			son.play(0, maxTPS)
		if wait:
			while pygame.mixer.get_busy():
				pass
		for son in lisSON:
			del son
	except:
		print "Erreur de lecture des sons suivants : %s" %(str(lisFIC))

def jouerEchantillonsSEQ(lisFIC, maxTPS=1000):
	"""
	Methode permettant de jouer sequentiellement plusieurs echantillons 
	references par leurs noms (fichier)

	@param lisFIC: liste des noms des echantillons
	"""
	try:
		lisSON = map(lambda e: pygame.mixer.Sound(e), lisFIC)
		for son in lisSON:
			son.play(0, maxTPS)
			while pygame.mixer.get_busy():
				pass
		for son in lisSON:
			del son
	except:
		print "Erreur de lecture des sons suivants : %s" %(str(lisFIC))


#------------------------------------------------------------------------------

def formater(val):
	"""
	Methode permettant de definir automatique le nom du fichier son a partir de
	sa valeur : 15 => ogg/not_15.ogg
	"""
	return 'ogg/not_%d.ogg' %(val)

def transformerNotes(lisSON, dec=True):
	"""
	Les fichiers ogg des notes representent deux octaves completes de Do a 	Do.
	Les noms des fichiers ogg associes aux notes commencent a 12 (1ere octave).
	On recherche le minimum des octaves sur la liste de sons et on la ramene a
	la premiere octave (decalage des valeurs). Les noms des fichiers peuvent
	ensuite etre definis a partir de la valeur du son.
	"""
	if dec:
		lisOCT = map(lambda e: e.getOctave(), lisSON)
		difOCT = min(lisOCT) - 1
		if difOCT <> 0:
			dec = -difOCT * 12
			for son in lisSON:
				son.decaler(dec)
	lisSON = map(lambda e: e.getValeur(), lisSON)
	lisSON = map(lambda e: formater(e), lisSON)
	return lisSON

#------------------------------------------------------------------------------

def jouerNotesMIX(lisSON, maxTPS=1000, dec=True, wait=True):
	lisVAL = transformerNotes(lisSON, dec)
	jouerEchantillonsMIX(lisVAL, maxTPS, wait)

def jouerNotesSEQ(lisSON, maxTPS=1000, dec=True):
	lisVAL = transformerNotes(lisSON, dec)
	jouerEchantillonsSEQ(lisVAL, maxTPS)