| 1 | /* |
|---|
| 2 | * Hydrogen |
|---|
| 3 | * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net] |
|---|
| 4 | * |
|---|
| 5 | * http://www.hydrogen-music.org |
|---|
| 6 | * |
|---|
| 7 | * This program is free software; you can redistribute it and/or modify |
|---|
| 8 | * it under the terms of the GNU General Public License as published by |
|---|
| 9 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 10 | * (at your option) any later version. |
|---|
| 11 | * |
|---|
| 12 | * This program is distributed in the hope that it will be useful, |
|---|
| 13 | * but WITHOUT ANY WARRANTY, without even the implied warranty of |
|---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | * GNU General Public License for more details. |
|---|
| 16 | * |
|---|
| 17 | * You should have received a copy of the GNU General Public License |
|---|
| 18 | * along with this program; if not, write to the Free Software |
|---|
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 20 | * |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | #include <hydrogen/basics/pattern.h> |
|---|
| 24 | |
|---|
| 25 | #include <cassert> |
|---|
| 26 | |
|---|
| 27 | #include <hydrogen/basics/note.h> |
|---|
| 28 | #include <hydrogen/audio_engine.h> |
|---|
| 29 | |
|---|
| 30 | namespace H2Core |
|---|
| 31 | { |
|---|
| 32 | |
|---|
| 33 | const char* Pattern::__class_name = "Pattern"; |
|---|
| 34 | |
|---|
| 35 | Pattern::Pattern( const QString& name, const QString& category, int length ) |
|---|
| 36 | : Object( __class_name ) |
|---|
| 37 | , __length( length ) |
|---|
| 38 | , __name( name ) |
|---|
| 39 | , __category( category ) |
|---|
| 40 | { |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | Pattern::Pattern( Pattern* other) |
|---|
| 44 | : Object( __class_name ) |
|---|
| 45 | , __length( other->get_length() ) |
|---|
| 46 | , __name( other->get_name() ) |
|---|
| 47 | , __category( other->get_category() ) |
|---|
| 48 | { |
|---|
| 49 | std::multimap <int, Note*>::const_iterator pos; |
|---|
| 50 | for ( pos = note_map.begin(); pos != note_map.end(); pos++ ) { |
|---|
| 51 | note_map.insert( std::make_pair( pos->first, new Note( pos->second ) ) ); |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | Pattern::~Pattern() |
|---|
| 56 | { |
|---|
| 57 | std::multimap <int, Note*>::const_iterator pos; |
|---|
| 58 | for ( pos = note_map.begin(); pos != note_map.end(); pos++ ) { |
|---|
| 59 | delete pos->second; |
|---|
| 60 | } |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | bool Pattern::references( Instrument* instr ) |
|---|
| 64 | { |
|---|
| 65 | std::multimap <int, Note*>::const_iterator pos; |
|---|
| 66 | for ( pos = note_map.begin(); pos != note_map.end(); pos++ ) { |
|---|
| 67 | Note *note = pos->second; |
|---|
| 68 | assert( note ); |
|---|
| 69 | if ( note->get_instrument() == instr ) { |
|---|
| 70 | return true; |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | return false; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | void Pattern::purge_instrument( Instrument* instr ) |
|---|
| 77 | { |
|---|
| 78 | bool locked = false; |
|---|
| 79 | std::list< Note* > slate; |
|---|
| 80 | std::multimap <int, Note*>::iterator pos; |
|---|
| 81 | for ( pos = note_map.begin(); pos != note_map.end(); pos++ ) { |
|---|
| 82 | Note* note = pos->second; |
|---|
| 83 | assert( note ); |
|---|
| 84 | if ( note->get_instrument() == instr ) { |
|---|
| 85 | if ( !locked ) { |
|---|
| 86 | H2Core::AudioEngine::get_instance()->lock( RIGHT_HERE ); |
|---|
| 87 | locked = true; |
|---|
| 88 | } |
|---|
| 89 | slate.push_back( note ); |
|---|
| 90 | note_map.erase( pos ); |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | if ( locked ) { |
|---|
| 94 | H2Core::AudioEngine::get_instance()->unlock(); |
|---|
| 95 | while ( slate.size() ) { |
|---|
| 96 | delete slate.front(); |
|---|
| 97 | slate.pop_front(); |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | void Pattern::set_to_old() { |
|---|
| 103 | std::multimap <int, Note*>::const_iterator pos; |
|---|
| 104 | for ( pos = note_map.begin(); pos != note_map.end(); pos++ ) { |
|---|
| 105 | Note *note = pos->second; |
|---|
| 106 | assert( note ); |
|---|
| 107 | note->set_just_recorded( false ); |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | }; |
|---|
| 112 | |
|---|
| 113 | /* vim: set softtabstop=4 expandtab: */ |
|---|