X-Git-Url: https://scm.cri.ensmp.fr/git/minwii.git/blobdiff_plain/46f3ffd7fdde386f41020171b5733e55a8e64a85..8a654eb43381bf77f3bade5a228809c90217d291:/src/minwii/widgets/playingscreen.py diff --git a/src/minwii/widgets/playingscreen.py b/src/minwii/widgets/playingscreen.py index 0735825..b1dbe80 100755 --- a/src/minwii/widgets/playingscreen.py +++ b/src/minwii/widgets/playingscreen.py @@ -7,22 +7,23 @@ $Id$ $URL$ """ import pygame -from cursors import WarpingCursor -from column import Column -import events -from eventutils import event_handler, EventDispatcher, EventHandlerMixin import types -from musicxml import Tone -from config import FRAMERATE -from config import FIRST_HUE -from config import MIDI_VELOCITY_RANGE -from config import MIDI_PAN_RANGE -from config import MIDI_VELOCITY_WRONG_NOTE_ATTN +import minwii.events as events +from minwii.log import eventLogger +from minwii.eventutils import event_handler, EventDispatcher, EventHandlerMixin +from minwii.musicxml import Tone +from minwii.config import FRAMERATE +from minwii.config import FIRST_HUE +from minwii.config import MIDI_VELOCITY_RANGE +from minwii.config import MIDI_PAN_RANGE +from minwii.config import MIDI_VELOCITY_WRONG_NOTE_ATTN +from minwii.globals import BACKGROUND_LAYER +from minwii.globals import CURSOR_LAYER +from minwii.globals import PLAYING_MODES_DICT -from globals import BACKGROUND_LAYER -from globals import CURSOR_LAYER -from globals import PLAYING_MODES_DICT +from cursors import WarpingCursor +from column import Column class PlayingScreenBase(pygame.sprite.LayeredDirty, EventHandlerMixin) : @@ -58,11 +59,16 @@ class PlayingScreenBase(pygame.sprite.LayeredDirty, EventHandlerMixin) : columnWidth = int(round(float(dispWidth) / self.keyboardLength)) rects = [] - for i in range(self.keyboardLength) : + for i in range(self.keyboardLength - 1) : upperLeftCorner = (i*columnWidth, 0) rect = pygame.Rect(upperLeftCorner, (columnWidth, dispHeight)) rects.append(rect) + # la dernière colonne à la largeur du reste + upperLeftCorner = ((i+1) * columnWidth, 0) + rect = pygame.Rect(upperLeftCorner, (dispWidth - (self.keyboardLength - 1) * columnWidth , dispHeight)) + rects.append(rect) + self.keyboardRects = rects def _initColumns(self) : @@ -99,7 +105,9 @@ class PlayingScreenBase(pygame.sprite.LayeredDirty, EventHandlerMixin) : @event_handler(pygame.KEYDOWN) def handleKeyDown(self, event) : - if event.key == pygame.K_q or event.unicode == u'q': + if event.key == pygame.K_q or \ + event.unicode == u'q' or \ + pygame.K_ESCAPE: self.stop() @event_handler(pygame.MOUSEBUTTONDOWN) @@ -160,29 +168,34 @@ class PlayingScreen(PlayingScreenBase) : def __init__(self, synth) : distinctNotes = [] + self.currentColumn = None for midi in self.scale : tone = Tone(midi) distinctNotes.append(tone) super(PlayingScreen, self).__init__(synth, distinctNotes) - @event_handler(events.NOTEON) - def noteon(self, evt) : - tone = evt.tone - self.synth.noteon(0, tone.midi, 96) - - @event_handler(events.NOTEOFF) - def noteoff(self, evt) : - tone = evt.tone - self.synth.noteoff(0, tone.midi) + @event_handler(events.COLDOWN) + def noteon(self, event) : + col = event.column + col.update(True) + self.currentColumn = col + self.playnote(col, event.pos) + @event_handler(events.COLUP) + def noteoff(self, event) : + if self.currentColumn : + self.currentColumn.update(False) + self.synth.noteoff(0, self.currentColumn.tone.midi) + class SongPlayingScreen(PlayingScreenBase) : - def __init__(self, synth, song, mode=PLAYING_MODES_DICT['NORMAL']) : + def __init__(self, synth, song, mode=PLAYING_MODES_DICT['NORMAL'], tempoTrim=0) : super(SongPlayingScreen, self).__init__(synth, song.distinctNotes) self.song = song self.quarterNoteDuration = song.quarterNoteDuration + self.tempoTrim = tempoTrim self.currentColumn = None self.noteIterator = self.song.iterNotes() self.displayNext() @@ -217,29 +230,23 @@ class SongPlayingScreen(PlayingScreenBase) : col = event.column if col.state and not self.currentNotePlayed : self.playnote(col, event.pos) - SongPlayingScreen.setNoteTimeout( - int(self.currentNote.duration * \ - self.quarterNoteDuration) - ) + self.setNoteTimeout() self.currentNotePlayed = True def handleEasyColumnOver(self, event) : col = event.column if col.state and \ - any(event.mouseEvent.buttons) and \ + self.cursor.pressed and \ not self.currentNotePlayed : self.playnote(col, event.pos) - SongPlayingScreen.setNoteTimeout( - int(self.currentNote.duration * \ - self.quarterNoteDuration) - ) + self.setNoteTimeout() self.currentNotePlayed = True def handleNormalColumnOver(self, event) : col = event.column if col.state and \ - any(event.mouseEvent.buttons) and \ + self.cursor.pressed and \ not self.currentNotePlayed : self.playnote(col, event.pos) self.currentNotePlayed = True @@ -255,10 +262,7 @@ class SongPlayingScreen(PlayingScreenBase) : if col.state and \ not self.currentNotePlayed : self.playnote(col, event.pos) - SongPlayingScreen.setNoteTimeout( - int(self.currentNote.duration * \ - self.quarterNoteDuration) - ) + self.setNoteTimeout() self.currentNotePlayed = True @@ -292,8 +296,17 @@ class SongPlayingScreen(PlayingScreenBase) : def displayNext(self, event=None) : if self.currentColumn: self.currentColumn.update(False) - note, verseIndex = self.noteIterator.next() - syllabus = note.lyrics[verseIndex].syllabus() + try : + note, verseIndex = self.noteIterator.next() + except StopIteration : + self.noteIterator = self.song.iterNotes() + note, verseIndex = self.noteIterator.next() + eventLogger.info(pygame.event.Event(events.SONGEND)) + try : + syllabus = note.lyrics[verseIndex].syllabus() + except IndexError : + syllabus = u'…' + column = self.columns[note.midi] column.update(True, syllabus) self.currentColumn = column @@ -306,10 +319,17 @@ class SongPlayingScreen(PlayingScreenBase) : self.synth.noteoff(0, self.currentNote.midi) self.displayNext() - @staticmethod - def setNoteTimeout(delay) : + def setNoteTimeout(self) : + delay = self.currentNote.duration * self.quarterNoteDuration + delay = delay + delay * self.tempoTrim pygame.time.set_timer(events.NOTEEND, delay) + def tempoTrimUp(self, step=0.1) : + self.tempoTrim = round(self.tempoTrim - step, 1) + + def tempoTrimDown(self, step=0.1) : + self.tempoTrim = round(self.tempoTrim + step, 1) + def stop(self) : pygame.time.set_timer(events.NOTEEND, 0) super(SongPlayingScreen, self).stop()