Changeset 465
- Timestamp:
- 09/13/08 13:24:59 (5 years ago)
- Files:
-
- 1 modified
-
trunk/libs/hydrogen/src/hydrogen.cpp (modified) (81 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/libs/hydrogen/src/hydrogen.cpp
r443 r465 20 20 * 21 21 */ 22 23 #include "config.h" 22 24 23 25 #ifdef WIN32 … … 112 114 // overload the the > operator of Note objects for priority_queue 113 115 bool operator> (const Note& pNote1, const Note &pNote2) { 114 return (pNote1.m_nHumanizeDelay + pNote1.get_position() * m_pAudioDriver->m_transport.m_nTickSize) > \ 115 (pNote2.m_nHumanizeDelay + pNote2.get_position() * m_pAudioDriver->m_transport.m_nTickSize); 116 } 117 118 std::priority_queue<Note*, std::deque<Note*>, std::greater<Note> > m_songNoteQueue; /// Song Note FIFO 116 return (pNote1.m_nHumanizeDelay 117 + pNote1.get_position() * m_pAudioDriver->m_transport.m_nTickSize) 118 > 119 (pNote2.m_nHumanizeDelay 120 + pNote2.get_position() * m_pAudioDriver->m_transport.m_nTickSize); 121 } 122 123 /// Song Note FIFO 124 std::priority_queue<Note*, std::deque<Note*>, std::greater<Note> > m_songNoteQueue; 119 125 std::deque<Note*> m_midiNoteQueue; ///< Midi Note FIFO 120 126 121 127 Song *m_pSong; ///< Current song 122 128 PatternList* m_pNextPatterns; ///< Next pattern (used only in Pattern mode) 123 bool m_bAppendNextPattern; ///< Add the next pattern to the list instead of replace. 129 bool m_bAppendNextPattern; ///< Add the next pattern to the list instead 130 /// of replace. 124 131 bool m_bDeleteNextPattern; ///< Delete the next pattern from the list. 125 132 … … 140 147 141 148 142 Hydrogen* hydrogenInstance = NULL; ///< Hydrogen class instance (used for log)149 Hydrogen* hydrogenInstance = NULL; ///< Hydrogen class instance (used for log) 143 150 144 151 … … 235 242 { 236 243 float sampleRate = ( float )m_pAudioDriver->getSampleRate(); 237 m_pAudioDriver->m_transport.m_nTickSize = ( sampleRate * 60.0 / m_pSong->__bpm / m_pSong->__resolution ); 244 m_pAudioDriver->m_transport.m_nTickSize = 245 ( sampleRate * 60.0 / m_pSong->__bpm / m_pSong->__resolution ); 238 246 } 239 247 … … 267 275 268 276 // Create metronome instrument 269 QString sMetronomeFilename = QString( "%1/click.wav" ).arg( DataPath::get_data_path() ); 270 m_pMetronomeInstrument = new Instrument( sMetronomeFilename, "metronome", new ADSR() ); 271 m_pMetronomeInstrument->set_layer( new InstrumentLayer( Sample::load( sMetronomeFilename ) ), 0 ); 277 QString sMetronomeFilename = QString( "%1/click.wav" ) 278 .arg( DataPath::get_data_path() ); 279 m_pMetronomeInstrument = 280 new Instrument( sMetronomeFilename, "metronome", new ADSR() ); 281 m_pMetronomeInstrument->set_layer( 282 new InstrumentLayer( Sample::load( sMetronomeFilename ) ), 283 0 284 ); 272 285 273 286 // Change the current audio engine state … … 429 442 if ( ( m_audioEngineState == STATE_READY ) || ( m_audioEngineState == STATE_PLAYING ) ) { 430 443 431 float fNewTickSize = m_pAudioDriver->getSampleRate() * 60.0 / m_pSong->__bpm / m_pSong->__resolution; 444 float fNewTickSize = 445 m_pAudioDriver->getSampleRate() * 60.0 446 / m_pSong->__bpm 447 / m_pSong->__resolution; 432 448 433 449 if ( fNewTickSize != m_pAudioDriver->m_transport.m_nTickSize ) { 434 450 // cerco di convertire ... 435 float fTickNumber = ( float )m_pAudioDriver->m_transport.m_nFrames / ( float )m_pAudioDriver->m_transport.m_nTickSize; 451 float fTickNumber = 452 ( float )m_pAudioDriver->m_transport.m_nFrames 453 / ( float )m_pAudioDriver->m_transport.m_nTickSize; 436 454 437 455 m_pAudioDriver->m_transport.m_nTickSize = fNewTickSize; … … 446 464 m_pAudioDriver->m_transport.m_nFrames = nNewFrames; 447 465 #ifdef JACK_SUPPORT 448 if ( "JackOutput" == m_pAudioDriver->get_class_name() && m_audioEngineState == STATE_PLAYING ) { 449 static_cast< JackOutput* >( m_pAudioDriver )->calculateFrameOffset(); 466 if ( "JackOutput" == m_pAudioDriver->get_class_name() 467 && m_audioEngineState == STATE_PLAYING ) { 468 static_cast< JackOutput* >( m_pAudioDriver ) 469 ->calculateFrameOffset(); 450 470 } 451 471 #endif 452 453 /* 454 455 // delete all copied notes in the song notes queue 456 while(!m_songNoteQueue.empty()){ 457 delete m_songNoteQueue.top(); 458 m_songNoteQueue.pop(); 459 } 460 461 // send a note-off event to all notes present in the playing note queue 462 for ( int i = 0; i < m_playingNotesQueue.size(); ++i ) { 463 Note *pNote = m_playingNotesQueue[ i ]; 464 pNote->m_pADSR->release(); 465 } 466 467 // delete all copied notes in the midi notes queue 468 for (unsigned i = 0; i < m_midiNoteQueue.size(); ++i) { 469 delete m_midiNoteQueue[i]; 470 } 471 m_midiNoteQueue.clear(); 472 */ 473 } 474 } 475 } 476 477 472 } 473 } 474 } 478 475 479 476 inline void audioEngine_process_playNotes( unsigned long nframes ) … … 493 490 494 491 // verifico se la nota rientra in questo ciclo 495 unsigned int noteStartInFrames = (int)( pNote->get_position() * m_pAudioDriver->m_transport.m_nTickSize ); 496 497 // if there is a negative Humanize delay, take into account so we don't miss the time slice. 498 // ignore positive delay, or we might end the queue processing prematurely based on NoteQueue placement. 499 // the sampler handles positive delay. 492 unsigned int noteStartInFrames = 493 (int)( pNote->get_position() * m_pAudioDriver->m_transport.m_nTickSize ); 494 495 // if there is a negative Humanize delay, take into account so 496 // we don't miss the time slice. ignore positive delay, or we 497 // might end the queue processing prematurely based on NoteQueue 498 // placement. the sampler handles positive delay. 500 499 if (pNote->m_nHumanizeDelay < 0) { 501 500 noteStartInFrames += pNote->m_nHumanizeDelay; … … 503 502 504 503 // m_nTotalFrames <= NotePos < m_nTotalFrames + bufferSize 505 bool isNoteStart = ( ( noteStartInFrames >= framepos ) && ( noteStartInFrames < ( framepos + nframes ) ) ); 504 bool isNoteStart = ( ( noteStartInFrames >= framepos ) 505 && ( noteStartInFrames < ( framepos + nframes ) ) ); 506 506 bool isOldNote = noteStartInFrames < framepos; 507 507 if ( isNoteStart || isOldNote ) { … … 509 509 // Humanize - Velocity parameter 510 510 if ( m_pSong->get_humanize_velocity_value() != 0 ) { 511 float random = m_pSong->get_humanize_velocity_value() * getGaussian( 0.2 ); 512 pNote->set_velocity( pNote->get_velocity() + ( random - ( m_pSong->get_humanize_velocity_value() / 2.0 ) ) ); 511 float random = m_pSong->get_humanize_velocity_value() 512 * getGaussian( 0.2 ); 513 pNote->set_velocity( 514 pNote->get_velocity() 515 + ( random 516 - ( m_pSong->get_humanize_velocity_value() / 2.0 ) ) 517 ); 513 518 if ( pNote->get_velocity() > 1.0 ) { 514 519 pNote->set_velocity( 1.0 ); … … 520 525 // Random Pitch ;) 521 526 const float fMaxPitchDeviation = 2.0; 522 pNote->set_pitch( pNote->get_pitch() + ( fMaxPitchDeviation * getGaussian( 0.2 ) - fMaxPitchDeviation / 2.0 ) * pNote->get_instrument()->get_random_pitch_factor() ); 523 524 AudioEngine::get_instance()->get_sampler()->note_on( pNote ); // aggiungo la nota alla lista di note da eseguire 527 pNote->set_pitch( pNote->get_pitch() 528 + ( fMaxPitchDeviation * getGaussian( 0.2 ) 529 - fMaxPitchDeviation / 2.0 ) 530 * pNote->get_instrument()->get_random_pitch_factor() ); 531 532 // aggiungo la nota alla lista di note da eseguire 533 AudioEngine::get_instance()->get_sampler()->note_on( pNote ); 525 534 526 m_songNoteQueue.pop(); // rimuovo la nota dalla lista di note535 m_songNoteQueue.pop(); // rimuovo la nota dalla lista di note 527 536 pNote->get_instrument()->dequeue(); 528 537 // raise noteOn event 529 int nInstrument = m_pSong->get_instrument_list()->get_pos( pNote->get_instrument() ); 538 int nInstrument = m_pSong->get_instrument_list() 539 ->get_pos( pNote->get_instrument() ); 530 540 EventQueue::get_instance()->push_event( EVENT_NOTEON, nInstrument ); 531 541 continue; … … 549 559 550 560 char tmp[200]; 551 sprintf( tmp, "seek in %lld (old pos = %d)", nFrames, ( int )m_pAudioDriver->m_transport.m_nFrames ); 561 sprintf( tmp, "seek in %lld (old pos = %d)", 562 nFrames, 563 ( int )m_pAudioDriver->m_transport.m_nFrames ); 552 564 _INFOLOG( tmp ); 553 565 554 566 m_pAudioDriver->m_transport.m_nFrames = nFrames; 555 567 556 int tickNumber_start = ( unsigned )( m_pAudioDriver->m_transport.m_nFrames / m_pAudioDriver->m_transport.m_nTickSize ); 568 int tickNumber_start = ( unsigned )( 569 m_pAudioDriver->m_transport.m_nFrames 570 / m_pAudioDriver->m_transport.m_nTickSize ); 557 571 // sprintf(tmp, "[audioEngine_seek()] tickNumber_start = %d", tickNumber_start); 558 572 // hydrogenInstance->infoLog(tmp); … … 575 589 inline void audioEngine_process_transport() 576 590 { 577 if ( ( m_audioEngineState == STATE_READY ) || ( m_audioEngineState == STATE_PLAYING ) ) { 591 if ( ( m_audioEngineState == STATE_READY ) 592 || ( m_audioEngineState == STATE_PLAYING ) ) { 578 593 m_pAudioDriver->updateTransportInfo(); 579 594 unsigned long nNewFrames = m_pAudioDriver->m_transport.m_nFrames; 580 595 581 // ??? audioEngine_seek returns IMMEDIATELY when nNewFrames == m_pAudioDriver->m_transport.m_nFrames ??? 582 // audioEngine_seek( nNewFrames, true ); 596 // ??? audioEngine_seek returns IMMEDIATELY 597 // when nNewFrames == m_pAudioDriver->m_transport.m_nFrames ??? 598 // audioEngine_seek( nNewFrames, true ); 583 599 584 600 switch ( m_pAudioDriver->m_transport.m_status ) { 585 601 case TransportInfo::ROLLING: 586 602 587 // hydrogenInstance->infoLog( "[audioEngine_process_transport] ROLLING - frames: " + to_string(m_pAudioDriver->m_transport.m_nFrames) );588 603 if ( m_audioEngineState == STATE_READY ) { 589 //hydrogenInstance->infoLog( "[audioEngine_process_transport] first start frames is " + to_string(m_pAudioDriver->m_transport.m_nFrames) );590 604 audioEngine_start( false, nNewFrames ); // no engine lock 591 605 } 592 606 593 607 if ( m_pSong->__bpm != m_pAudioDriver->m_transport.m_nBPM ) { 594 _INFOLOG( QString( "song bpm: (%1) gets transport bpm: (%2)" ).arg( m_pSong->__bpm ).arg( m_pAudioDriver->m_transport.m_nBPM ) ); 608 _INFOLOG( 609 QString( "song bpm: (%1) gets transport bpm: (%2)" ) 610 .arg( m_pSong->__bpm ) 611 .arg( m_pAudioDriver->m_transport.m_nBPM ) ); 595 612 596 613 m_pSong->__bpm = m_pAudioDriver->m_transport.m_nBPM; … … 603 620 case TransportInfo::STOPPED: 604 621 605 // hydrogenInstance->infoLog( "[audioEngine_process_transport] STOPPED - frames: " + to_string(m_pAudioDriver->m_transport.m_nFrames) );606 622 if ( m_audioEngineState == STATE_PLAYING ) { 607 623 audioEngine_stop( false ); // no engine lock … … 626 642 //_INFOLOG( "clear notes..."); 627 643 628 while (!m_songNoteQueue.empty()) { // delete all copied notes in the song notes queue 644 // delete all copied notes in the song notes queue 645 while (!m_songNoteQueue.empty()) { 629 646 m_songNoteQueue.top()->get_instrument()->dequeue(); 630 647 delete m_songNoteQueue.top(); … … 632 649 } 633 650 634 /* for (unsigned i = 0; i < m_playingNotesQueue.size(); ++i) { // delete all copied notes in the playing notes queue635 delete m_playingNotesQueue[i];636 }637 m_playingNotesQueue.clear();638 */639 651 AudioEngine::get_instance()->get_sampler()->stop_playing_notes(); 640 652 641 for ( unsigned i = 0; i < m_midiNoteQueue.size(); ++i ) { // delete all copied notes in the midi notes queue 653 // delete all copied notes in the midi notes queue 654 for ( unsigned i = 0; i < m_midiNoteQueue.size(); ++i ) { 642 655 delete m_midiNoteQueue[i]; 643 656 } … … 651 664 inline void audioEngine_process_clearAudioBuffers( uint32_t nFrames ) 652 665 { 666 // clear main out Left and Right 653 667 if ( m_pMainBuffer_L ) { 654 memset( m_pMainBuffer_L, 0, nFrames * sizeof( float ) ); // clear main out Left668 memset( m_pMainBuffer_L, 0, nFrames * sizeof( float ) ); 655 669 } 656 670 if ( m_pMainBuffer_R ) { 657 memset( m_pMainBuffer_R, 0, nFrames * sizeof( float ) ); // clear main out Right 658 } 659 660 if ( ( m_audioEngineState == STATE_READY ) || ( m_audioEngineState == STATE_PLAYING ) ) { 671 memset( m_pMainBuffer_R, 0, nFrames * sizeof( float ) ); 672 } 673 674 if ( ( m_audioEngineState == STATE_READY ) 675 || ( m_audioEngineState == STATE_PLAYING ) ) { 661 676 #ifdef LADSPA_SUPPORT 662 677 Effects* pEffects = Effects::getInstance(); … … 684 699 685 700 if ( m_nBufferSize != nframes ) { 686 _INFOLOG( QString( "Buffer size changed. Old size = %1, new size = %2" ).arg( m_nBufferSize ).arg( nframes ) ); 701 _INFOLOG( 702 QString( "Buffer size changed. Old size = %1, new size = %2" ) 703 .arg( m_nBufferSize ) 704 .arg( nframes ) 705 ); 687 706 m_nBufferSize = nframes; 688 707 } 689 708 690 audioEngine_process_transport(); // m_pAudioDriver->bpm updates Song->__bpm. (!!(Calls audioEngine_seek)) 709 // m_pAudioDriver->bpm updates Song->__bpm. (!!(Calls audioEngine_seek)) 710 audioEngine_process_transport(); 691 711 audioEngine_process_clearAudioBuffers( nframes ); 692 audioEngine_process_checkBPMChanged(); // m_pSong->__bpm decides tick size712 audioEngine_process_checkBPMChanged(); // m_pSong->__bpm decides tick size 693 713 694 714 bool sendPatternChange = false; 695 // always update note queue.. could come from pattern or realtime input (midi, keyboard) 696 int res2 = audioEngine_updateNoteQueue( nframes ); // update the notes queue 715 // always update note queue.. could come from pattern or realtime input 716 // (midi, keyboard) 717 int res2 = audioEngine_updateNoteQueue( nframes ); 697 718 if ( res2 == -1 ) { // end of song 698 719 _INFOLOG( "End of song received, calling engine_stop()" ); 699 720 AudioEngine::get_instance()->unlock(); 700 721 m_pAudioDriver->stop(); 701 m_pAudioDriver->locate( 0 ); // locate 0, reposition from start of the song 702 703 if ( ( m_pAudioDriver->get_class_name() == "DiskWriterDriver" ) || ( m_pAudioDriver->get_class_name() == "FakeDriver" ) ) { 722 m_pAudioDriver->locate( 0 ); // locate 0, reposition from start of the song 723 724 if ( ( m_pAudioDriver->get_class_name() == "DiskWriterDriver" ) 725 || ( m_pAudioDriver->get_class_name() == "FakeDriver" ) ) { 704 726 _INFOLOG( "End of song." ); 705 727 return 1; // kill the audio AudioDriver thread … … 748 770 #ifdef LADSPA_SUPPORT 749 771 // Process LADSPA FX 750 if ( ( m_audioEngineState == STATE_READY ) || ( m_audioEngineState == STATE_PLAYING ) ) { 772 if ( ( m_audioEngineState == STATE_READY ) 773 || ( m_audioEngineState == STATE_PLAYING ) ) { 751 774 for ( unsigned nFX = 0; nFX < MAX_FX; ++nFX ) { 752 775 LadspaFX *pFX = Effects::getInstance()->getLadspaFX( nFX ); … … 755 778 float *buf_L = NULL; 756 779 float *buf_R = NULL; 757 if ( pFX->getPluginType() == LadspaFX::STEREO_FX ) { // STEREO FX780 if ( pFX->getPluginType() == LadspaFX::STEREO_FX ) { 758 781 buf_L = pFX->m_pBuffer_L; 759 782 buf_R = pFX->m_pBuffer_R; … … 765 788 m_pMainBuffer_L[ i ] += buf_L[ i ]; 766 789 m_pMainBuffer_R[ i ] += buf_R[ i ]; 767 if ( buf_L[ i ] > m_fFXPeak_L[nFX] ) m_fFXPeak_L[nFX] = buf_L[ i ]; 768 if ( buf_R[ i ] > m_fFXPeak_R[nFX] ) m_fFXPeak_R[nFX] = buf_R[ i ]; 790 if ( buf_L[ i ] > m_fFXPeak_L[nFX] ) 791 m_fFXPeak_L[nFX] = buf_L[ i ]; 792 if ( buf_R[ i ] > m_fFXPeak_R[nFX] ) 793 m_fFXPeak_R[nFX] = buf_R[ i ]; 769 794 } 770 795 } … … 777 802 float val_L; 778 803 float val_R; 779 if ( m_audioEngineState == STATE_PLAYING || m_audioEngineState == STATE_READY ) { 804 if ( m_audioEngineState == STATE_PLAYING 805 || m_audioEngineState == STATE_READY ) { 780 806 for ( unsigned i = 0; i < nframes; ++i ) { 781 807 val_L = m_pMainBuffer_L[i]; … … 796 822 797 823 // float fRenderTime = (renderTime_end.tv_sec - renderTime_start.tv_sec) * 1000.0 + (renderTime_end.tv_usec - renderTime_start.tv_usec) / 1000.0; 798 float fLadspaTime = ( ladspaTime_end.tv_sec - ladspaTime_start.tv_sec ) * 1000.0 + ( ladspaTime_end.tv_usec - ladspaTime_start.tv_usec ) / 1000.0; 799 824 float fLadspaTime = 825 ( ladspaTime_end.tv_sec - ladspaTime_start.tv_sec ) * 1000.0 826 + ( ladspaTime_end.tv_usec - ladspaTime_start.tv_usec ) / 1000.0; 800 827 801 828 timeval finishTimeval = currentTime2(); 802 m_fProcessTime = ( finishTimeval.tv_sec - startTimeval.tv_sec ) * 1000.0 + ( finishTimeval.tv_usec - startTimeval.tv_usec ) / 1000.0; 829 m_fProcessTime = 830 ( finishTimeval.tv_sec - startTimeval.tv_sec ) * 1000.0 831 + ( finishTimeval.tv_usec - startTimeval.tv_usec ) / 1000.0; 803 832 804 833 float sampleRate = ( float )m_pAudioDriver->getSampleRate(); 805 834 m_fMaxProcessTime = 1000.0 / ( sampleRate / nframes ); 806 835 807 808 //DEBUG 836 #ifdef CONFIG_DEBUG 809 837 if ( m_fProcessTime > m_fMaxProcessTime ) { 810 838 _WARNINGLOG( "" ); 811 839 _WARNINGLOG( "----XRUN----" ); 812 _WARNINGLOG( QString( "XRUN of %1 msec (%2 > %3)" ).arg( ( m_fProcessTime - m_fMaxProcessTime ) ).arg( m_fProcessTime ).arg( m_fMaxProcessTime ) ); 840 _WARNINGLOG( QString( "XRUN of %1 msec (%2 > %3)" ) 841 .arg( ( m_fProcessTime - m_fMaxProcessTime ) ) 842 .arg( m_fProcessTime ).arg( m_fMaxProcessTime ) ); 813 843 _WARNINGLOG( QString( "Ladspa process time = %1" ).arg( fLadspaTime ) ); 814 844 _WARNINGLOG( "------------" ); … … 817 847 EventQueue::get_instance()->push_event( EVENT_XRUN, -1 ); 818 848 } 849 #endif 819 850 820 851 AudioEngine::get_instance()->unlock(); … … 1002 1033 int tickNumber_start = 0; 1003 1034 1004 // We need to look ahead in the song for notes with negative offsets from LeadLag or Humanize. 1005 // When starting from the beginning, we prime the note queue with notes between 0 and nFrames 1006 // plus lookahead. lookahead should be equal or greater than the nLeadLagFactor + nMaxTimeHumanize. 1035 // We need to look ahead in the song for notes with negative offsets 1036 // from LeadLag or Humanize. When starting from the beginning, we prime 1037 // the note queue with notes between 0 and nFrames plus 1038 // lookahead. lookahead should be equal or greater than the 1039 // nLeadLagFactor + nMaxTimeHumanize. 1007 1040 int lookahead = nLeadLagFactor + nMaxTimeHumanize + 1; 1008 if ( framepos == 0 || ( m_audioEngineState == STATE_PLAYING && m_pSong->get_mode() == Song::SONG_MODE && m_nSongPos == -1 ) ) { 1009 tickNumber_start = (int)( framepos / m_pAudioDriver->m_transport.m_nTickSize ); 1041 if ( framepos == 0 1042 || ( m_audioEngineState == STATE_PLAYING 1043 && m_pSong->get_mode() == Song::SONG_MODE 1044 && m_nSongPos == -1 ) ) { 1045 tickNumber_start = (int)( framepos 1046 / m_pAudioDriver->m_transport.m_nTickSize ); 1010 1047 } 1011 1048 else { 1012 tickNumber_start = (int)( (framepos + lookahead) / m_pAudioDriver->m_transport.m_nTickSize ); 1013 } 1014 int tickNumber_end = (int)( (framepos + nFrames + lookahead) / m_pAudioDriver->m_transport.m_nTickSize ); 1049 tickNumber_start = (int)( (framepos + lookahead) 1050 / m_pAudioDriver->m_transport.m_nTickSize ); 1051 } 1052 int tickNumber_end = (int)( (framepos + nFrames + lookahead) 1053 / m_pAudioDriver->m_transport.m_nTickSize ); 1015 1054 1016 1055 int tick = tickNumber_start; 1017 1056 1018 // _WARNINGLOG( "Lookahead: " + to_string( lookahead / m_pAudioDriver->m_transport.m_nTickSize ) ); 1057 // _WARNINGLOG( "Lookahead: " + to_string( lookahead 1058 // / m_pAudioDriver->m_transport.m_nTickSize ) ); 1019 1059 // get initial timestamp for first tick 1020 1060 gettimeofday( &m_currentTickTime, NULL ); … … 1030 1070 1031 1071 1032 // midi events now get put into the m_songNoteQueue as well, based on their timestamp 1072 // midi events now get put into the m_songNoteQueue as well, 1073 // based on their timestamp 1033 1074 while ( m_midiNoteQueue.size() > 0 ) { 1034 1075 Note *note = m_midiNoteQueue[0]; … … 1050 1091 1051 1092 // if ( m_nPatternStartTick == -1 ) { // for debugging pattern mode :s 1052 // _WARNINGLOG( "m_nPatternStartTick == -1; tick = " + to_string( tick ) ); 1093 // _WARNINGLOG( "m_nPatternStartTick == -1; tick = " 1094 // + to_string( tick ) ); 1053 1095 // } 1054 1096 … … 1063 1105 } 1064 1106 1065 m_nSongPos = findPatternInTick( tick, m_pSong->is_loop_enabled(), &m_nPatternStartTick ); 1107 m_nSongPos = findPatternInTick( tick, 1108 m_pSong->is_loop_enabled(), 1109 &m_nPatternStartTick ); 1066 1110 if ( m_nSongSizeInTicks != 0 ) { 1067 m_nPatternTickPosition = ( tick - m_nPatternStartTick ) % m_nSongSizeInTicks; 1111 m_nPatternTickPosition = ( tick - m_nPatternStartTick ) 1112 % m_nSongSizeInTicks; 1068 1113 } else { 1069 1114 m_nPatternTickPosition = tick - m_nPatternStartTick; … … 1074 1119 } 1075 1120 1076 //PatternList *pPatternList = (*(m_pSong->getPatternGroupVector()))[m_nSongPos]; 1121 // PatternList *pPatternList = 1122 // (*(m_pSong->getPatternGroupVector()))[m_nSongPos]; 1077 1123 if ( m_nSongPos == -1 ) { 1078 1124 _INFOLOG( "song pos = -1" ); 1079 if ( m_pSong->is_loop_enabled() == true ) { // check if the song loop is enabled 1080 m_nSongPos = findPatternInTick( 0, true, &m_nPatternStartTick ); 1125 if ( m_pSong->is_loop_enabled() == true ) { 1126 m_nSongPos = findPatternInTick( 0, 1127 true, 1128 &m_nPatternStartTick ); 1081 1129 } else { 1082 1130 _INFOLOG( "End of Song" ); … … 1084 1132 } 1085 1133 } 1086 PatternList *pPatternList = ( *( m_pSong->get_pattern_group_vector() ) )[m_nSongPos]; 1134 PatternList *pPatternList = 1135 ( *( m_pSong->get_pattern_group_vector() ) )[m_nSongPos]; 1087 1136 // copio tutti i pattern 1088 1137 m_pPlayingPatterns->clear(); … … 1096 1145 // PATTERN MODE 1097 1146 else if ( m_pSong->get_mode() == Song::PATTERN_MODE ) { 1098 //hydrogenInstance->warningLog( "pattern mode not implemented yet" ); 1099 1100 // per ora considero solo il primo pattern, se ce ne saranno piu' di uno 1101 // bisognera' prendere quello piu' piccolo 1147 // per ora considero solo il primo pattern, se ce ne 1148 // saranno piu' di uno bisognera' prendere quello piu' 1149 // piccolo 1102 1150 1103 1151 //m_nPatternTickPosition = tick % m_pCurrentPattern->getSize(); … … 1108 1156 { 1109 1157 m_pPlayingPatterns->clear(); 1110 Pattern * pSelectedPattern = m_pSong->get_pattern_list()->get(m_nSelectedPatternNumber); 1158 Pattern * pSelectedPattern = 1159 m_pSong->get_pattern_list() 1160 ->get(m_nSelectedPatternNumber); 1111 1161 m_pPlayingPatterns->add( pSelectedPattern ); 1112 1162 } … … 1122 1172 } 1123 1173 1124 if ( ( tick == m_nPatternStartTick + nPatternSize ) || ( m_nPatternStartTick == -1 ) ) { 1174 if ( ( tick == m_nPatternStartTick + nPatternSize ) 1175 || ( m_nPatternStartTick == -1 ) ) { 1125 1176 if ( m_pNextPatterns->get_size() > 0 ) { 1126 //hydrogenInstance->errorLog( "[audioEngine_updateNoteQueue] Aggiorno con nextpattern: " + to_string( (int)m_pNextPattern ) );1127 /* if ( m_bDeleteNextPattern ) {1128 m_pPlayingPatterns->del( m_pNextPattern );1129 }1130 else {1131 if ( m_bAppendNextPattern == false ) {1132 m_pPlayingPatterns->clear();1133 }1134 m_pPlayingPatterns->add( m_pNextPattern );1135 }*/1136 // _WARNINGLOG( "uh-oh, next patterns..." );1137 1177 Pattern * p; 1138 for ( uint i = 0; i < m_pNextPatterns->get_size(); i++ ) { 1178 for ( uint i = 0; 1179 i < m_pNextPatterns->get_size(); 1180 i++ ) { 1139 1181 p = m_pNextPatterns->get( i ); 1140 // _WARNINGLOG( QString( "Got pattern # %1" ).arg( i + 1 ) ); 1141 // if the pattern isn't playing already, start it now. 1182 // _WARNINGLOG( QString( "Got pattern # %1" ) 1183 // .arg( i + 1 ) ); 1184 // if the pattern isn't playing 1185 // already, start it now. 1142 1186 if ( ( m_pPlayingPatterns->del( p ) ) == NULL ) { 1143 1187 m_pPlayingPatterns->add( p ); … … 1149 1193 if ( m_nPatternStartTick == -1 ) { 1150 1194 m_nPatternStartTick = tick - (tick % nPatternSize); 1151 // _WARNINGLOG( "set Pattern Start Tick to " + to_string( m_nPatternStartTick ) ); 1195 // _WARNINGLOG( "set Pattern Start Tick to " 1196 // + to_string( m_nPatternStartTick ) ); 1152 1197 } else { 1153 1198 m_nPatternStartTick = tick; … … 1161 1206 1162 1207 // metronome 1163 // if ( ( m_nPatternStartTick == tick ) || ( ( tick - m_nPatternStartTick ) % 48 == 0 ) ) { 1208 // if ( ( m_nPatternStartTick == tick ) 1209 // || ( ( tick - m_nPatternStartTick ) % 48 == 0 ) ) { 1164 1210 if ( m_nPatternTickPosition % 48 == 0 ) { 1165 1211 float fPitch; 1166 1212 float fVelocity; 1167 // _INFOLOG( "Beat: " + to_string(m_nPatternTickPosition / 48 + 1) + "@ " + to_string( tick ) ); 1213 // _INFOLOG( "Beat: " + to_string(m_nPatternTickPosition / 48 + 1) 1214 // + "@ " + to_string( tick ) ); 1168 1215 if ( m_nPatternTickPosition == 0 ) { 1169 1216 fPitch = 3; … … 1176 1223 } 1177 1224 if ( Preferences::getInstance()->m_bUseMetronome ) { 1178 m_pMetronomeInstrument->set_volume( Preferences::getInstance()->m_fMetronomeVolume ); 1179 1180 Note *pMetronomeNote = new Note( m_pMetronomeInstrument, tick, fVelocity, 0.5, 0.5, -1, fPitch ); 1225 m_pMetronomeInstrument->set_volume( 1226 Preferences::getInstance()->m_fMetronomeVolume 1227 ); 1228 Note *pMetronomeNote = new Note( m_pMetronomeInstrument, 1229 tick, 1230 fVelocity, 1231 0.5, 1232 0.5, 1233 -1, 1234 fPitch 1235 ); 1181 1236 m_pMetronomeInstrument->enqueue(); 1182 1237 m_songNoteQueue.push( pMetronomeNote ); … … 1184 1239 } 1185 1240 1186 1187 1188 1241 // update the notes queue 1189 1242 if ( m_pPlayingPatterns->get_size() != 0 ) { 1190 for ( unsigned nPat = 0; nPat < m_pPlayingPatterns->get_size(); ++nPat ) { 1243 for ( unsigned nPat = 0 ; 1244 nPat < m_pPlayingPatterns->get_size() ; 1245 ++nPat ) { 1191 1246 Pattern *pPattern = m_pPlayingPatterns->get( nPat ); 1192 1247 assert( pPattern != NULL ); 1193 1248 1194 1249 std::multimap <int, Note*>::iterator pos; 1195 for ( pos = pPattern->note_map.lower_bound( m_nPatternTickPosition ); pos != pPattern->note_map.upper_bound( m_nPatternTickPosition ); ++pos ) { 1250 for ( pos = pPattern->note_map.lower_bound( m_nPatternTickPosition ) ; 1251 pos != pPattern->note_map.upper_bound( m_nPatternTickPosition ) ; 1252 ++pos ) { 1196 1253 Note *pNote = pos->second; 1197 1254 if ( pNote ) { … … 1200 1257 // Swing 1201 1258 float fSwingFactor = m_pSong->get_swing_factor(); 1202 if ( ( ( m_nPatternTickPosition % 12 ) == 0 ) && ( ( m_nPatternTickPosition % 24 ) != 0 ) ) { // da l'accento al tick 4, 12, 20, 36... 1203 nOffset += ( int )( ( 6.0 * m_pAudioDriver->m_transport.m_nTickSize ) * fSwingFactor ); 1259 1260 if ( ( ( m_nPatternTickPosition % 12 ) == 0 ) 1261 && ( ( m_nPatternTickPosition % 24 ) != 0 ) ) { 1262 // da l'accento al tick 4, 12, 20, 36... 1263 nOffset += ( int )( 1264 6.0 1265 * m_pAudioDriver->m_transport.m_nTickSize 1266 * fSwingFactor 1267 ); 1204 1268 } 1205 1269 1206 1270 // Humanize - Time parameter 1207 1271 if ( m_pSong->get_humanize_time_value() != 0 ) { 1208 nOffset += ( int )( (getGaussian( 0.3 ) * m_pSong->get_humanize_time_value() * nMaxTimeHumanize ) ); 1272 nOffset += ( int )( 1273 getGaussian( 0.3 ) 1274 * m_pSong->get_humanize_time_value() 1275 * nMaxTimeHumanize 1276 ); 1209 1277 } 1210 1278 //~ 1211 1279 // Lead or Lag - timing parameter 1212 nOffset += (int) (pNote->get_leadlag() * nLeadLagFactor); 1280 nOffset += (int) ( pNote->get_leadlag() 1281 * nLeadLagFactor); 1213 1282 //~ 1214 1283 1215 1284 // cannot play note before 0 frame 1216 if (tick + nOffset / m_pAudioDriver->m_transport.m_nTickSize < tickNumber_start ) { 1285 if (tick 1286 + nOffset / m_pAudioDriver->m_transport.m_nTickSize 1287 < tickNumber_start ) { 1217 1288 _INFOLOG(" offset before 0 frame "); 1218 nOffset = tickNumber_start - (int) (tick * m_pAudioDriver->m_transport.m_nTickSize); 1289 nOffset = tickNumber_start 1290 - (int) (tick * m_pAudioDriver->m_transport.m_nTickSize); 1219 1291 } 1220 1292 Note *pCopiedNote = new Note( pNote ); 1221 1293 pCopiedNote->set_position( tick ); 1222 1294 1223 pCopiedNote->m_nHumanizeDelay = nOffset; // humanize time 1295 // humanize time 1296 pCopiedNote->m_nHumanizeDelay = nOffset; 1224 1297 pNote->get_instrument()->enqueue(); 1225 1298 m_songNoteQueue.push( pCopiedNote ); … … 1257 1330 PatternList *pColumn = ( *pPatternColumns )[ i ]; 1258 1331 if ( pColumn->get_size() != 0 ) { 1259 // tengo in considerazione solo il primo pattern. I pattern nel gruppo devono avere la stessa lunghezza. 1332 // tengo in considerazione solo il primo pattern. I 1333 // pattern nel gruppo devono avere la stessa lunghezza. 1260 1334 nPatternSize = pColumn->get( 0 )->get_lenght(); 1261 1335 } else { … … 1280 1354 PatternList *pColumn = ( *pPatternColumns )[ i ]; 1281 1355 if ( pColumn->get_size() != 0 ) { 1282 // tengo in considerazione solo il primo pattern. I pattern nel gruppo devono avere la stessa lunghezza. 1356 // tengo in considerazione solo il primo 1357 // pattern. I pattern nel gruppo devono avere la 1358 // stessa lunghezza. 1283 1359 nPatternSize = pColumn->get( 0 )->get_lenght(); 1284 1360 } else { … … 1286 1362 } 1287 1363 1288 if ( ( nLoopTick >= nTotalTick ) && ( nLoopTick < nTotalTick + nPatternSize ) ) { 1364 if ( ( nLoopTick >= nTotalTick ) 1365 && ( nLoopTick < nTotalTick + nPatternSize ) ) { 1289 1366 ( *pPatternStartTick ) = nTotalTick; 1290 1367 return i; … … 1305 1382 { 1306 1383 // check current state 1307 if ( ( m_audioEngineState != STATE_READY ) && ( m_audioEngineState != STATE_PLAYING ) ) { 1384 if ( ( m_audioEngineState != STATE_READY ) 1385 && ( m_audioEngineState != STATE_PLAYING ) ) { 1308 1386 _ERRORLOG( "Error the audio engine is not in READY state" ); 1309 1387 delete note; … … 1325 1403 1326 1404 // check current state 1327 if ( ( m_audioEngineState != STATE_READY ) && ( m_audioEngineState != STATE_PLAYING ) ) { 1405 if ( ( m_audioEngineState != STATE_READY ) 1406 && ( m_audioEngineState != STATE_PLAYING ) ) { 1328 1407 _ERRORLOG( "Error the audio engine is not in READY state" ); 1329 1408 delete note; … … 1332 1411 } 1333 1412 1334 /*1335 for ( unsigned i = 0; i < m_playingNotesQueue.size(); ++i ) { // delete old note1336 Note *oldNote = m_playingNotesQueue[ i ];1337 1338 if ( oldNote->getInstrument() == note->getInstrument() ) {1339 m_playingNotesQueue.erase( m_playingNotesQueue.begin() + i );1340 delete oldNote;1341 break;1342 }1343 }1344 */1345 1413 AudioEngine::get_instance()->get_sampler()->note_off( note ); 1346 1414 … … 1377 1445 } else { 1378 1446 #ifdef JACK_SUPPORT 1379 ( ( JackOutput* ) pDriver )->setConnectDefaults( Preferences::getInstance()->m_bJackConnectDefaults ); 1447 static_cast<JackOutput*>(pDriver)->setConnectDefaults( 1448 Preferences::getInstance()->m_bJackConnectDefaults 1449 ); 1380 1450 #endif 1381 1451 } … … 1436 1506 // check current state 1437 1507 if ( m_audioEngineState != STATE_INITIALIZED ) { 1438 _ERRORLOG( QString( "Error the audio engine is not in INITIALIZED state. state=%1" ).arg( m_audioEngineState ) ); 1508 _ERRORLOG( QString( "Error the audio engine is not in INITIALIZED" 1509 " state. state=%1" ) 1510 .arg( m_audioEngineState ) ); 1439 1511 AudioEngine::get_instance()->unlock(); 1440 1512 return; … … 1551 1623 EventQueue::get_instance()->push_event( EVENT_STATE, STATE_READY ); 1552 1624 } 1553 1554 AudioEngine::get_instance()->unlock(); // Unlocking earlier might execute the jack process() callback before we are fully initialized. 1625 // Unlocking earlier might execute the jack process() callback before we 1626 // are fully initialized. 1627 AudioEngine::get_instance()->unlock(); 1555 1628 } 1556 1629 … … 1569 1642 } 1570 1643 1571 if ( ( m_audioEngineState != STATE_PREPARED ) && ( m_audioEngineState != STATE_READY ) ) { 1572 _ERRORLOG( QString( "Error: the audio engine is not in PREPARED or READY state. state=%1" ).arg( m_audioEngineState ) ); 1644 if ( ( m_audioEngineState != STATE_PREPARED ) 1645 && ( m_audioEngineState != STATE_READY ) ) { 1646 _ERRORLOG( QString( "Error: the audio engine is not in PREPARED" 1647 " or READY state. state=%1" ) 1648 .arg( m_audioEngineState ) ); 1573 1649 return; 1574 1650 } … … 1617 1693 //---------------------------------------------------------------------------- 1618 1694 1619 1620 Hydrogen* Hydrogen::__instance = NULL; /// static reference of Hydrogen class (Singleton)1695 /// static reference of Hydrogen class (Singleton) 1696 Hydrogen* Hydrogen::__instance = NULL; 1621 1697 1622 1698 … … 1719 1795 1720 1796 1721 void Hydrogen::addRealtimeNote( int instrument, float velocity, float pan_L, float pan_R, float pitch, bool forcePlay ) 1797 void Hydrogen::addRealtimeNote( int instrument, 1798 float velocity, 1799 float pan_L, 1800 float pan_R, 1801 float pitch, 1802 bool forcePlay ) 1722 1803 { 1723 1804 UNUSED( pitch ); … … 1730 1811 bool hearnote = forcePlay; 1731 1812 1732 1733 1734 AudioEngine::get_instance()->lock( "Hydrogen::addRealtimeNote" ); // lock the audio engine 1813 AudioEngine::get_instance()->lock( "Hydrogen::addRealtimeNote" ); 1735 1814 1736 1815 Song *song = getSong(); … … 1758 1837 Pattern* currentPattern = NULL; 1759 1838 PatternList *pPatternList = m_pSong->get_pattern_list(); 1760 if ( ( m_nSelectedPatternNumber != -1 ) && ( m_nSelectedPatternNumber < ( int )pPatternList->get_size() ) ) { 1839 if ( ( m_nSelectedPatternNumber != -1 ) 1840 && ( m_nSelectedPatternNumber < ( int )pPatternList->get_size() ) ) { 1761 1841 currentPattern = pPatternList->get( m_nSelectedPatternNumber ); 1762 1842 } … … 1769 1849 if ( currentPattern && ( getState() == STATE_PLAYING ) ) { 1770 1850 bool bNoteAlreadyExist = false; 1771 for ( unsigned nNote = 0; nNote < currentPattern->get_lenght(); nNote++ ) { 1851 for ( unsigned nNote = 0 ; 1852 nNote < currentPattern->get_lenght() ; 1853 nNote++ ) { 1772 1854 std::multimap <int, Note*>::iterator pos; 1773 for ( pos = currentPattern->note_map.lower_bound( nNote ); pos != currentPattern->note_map.upper_bound( nNote ); ++pos ) { 1855 for ( pos = currentPattern->note_map.lower_bound( nNote ) ; 1856 pos != currentPattern->note_map.upper_bound( nNote ) ; 1857 ++pos ) { 1774 1858 Note *pNote = pos->second; 1775 1859 if ( pNote!=NULL ) { 1776 if ( pNote->get_instrument() == instrRef && nNote==column ) { 1860 if ( pNote->get_instrument() == instrRef 1861 && nNote==column ) { 1777 1862 bNoteAlreadyExist = true; 1778 1863 break; … … 1785 1870 // in this case, we'll leave the note alone 1786 1871 // hear note only if not playing too 1787 if ( pref->getHearNewNotes() && getState() == STATE_READY ) { 1872 if ( pref->getHearNewNotes() 1873 && getState() == STATE_READY ) { 1788 1874 hearnote = true; 1789 1875 } 1790 1876 } else if ( !pref->getRecordEvents() ) { 1791 if ( pref->getHearNewNotes() && ( getState() == STATE_READY || getState() == STATE_PLAYING ) ) { 1877 if ( pref->getHearNewNotes() 1878 && ( getState() == STATE_READY 1879 || getState() == STATE_PLAYING ) ) { 1792 1880 hearnote = true; 1793 1881 } 1794 1882 } else { 1795 1883 // create the new note 1796 Note *note = new Note( instrRef, position, velocity, pan_L, pan_R, -1, 0 ); 1797 currentPattern->note_map.insert( std::make_pair( column, note ) ); 1884 Note *note = new Note( instrRef, 1885 position, 1886 velocity, 1887 pan_L, 1888 pan_R, 1889 -1, 1890 0 ); 1891 currentPattern->note_map.insert( 1892 std::make_pair( column, note ) 1893 ); 1798 1894 1799 1895 // hear note if its not in the future 1800 if ( pref->getHearNewNotes() && position <= getTickPosition() ) { 1896 if ( pref->getHearNewNotes() 1897 && position <= getTickPosition() ) { 1801 1898 hearnote = true; 1802 1899 } … … 1811 1908 1812 1909 if ( hearnote && instrRef ) { 1813 Note *note2 = new Note( instrRef, realcolumn, velocity, pan_L, pan_R, -1, 0 ); 1910 Note *note2 = new Note( instrRef, 1911 realcolumn, 1912 velocity, 1913 pan_L, 1914 pan_R, 1915 -1, 1916 0 ); 1814 1917 midi_noteOn( note2 ); 1815 1918 } … … 1844 1947 { 1845 1948 //unsigned long initTick = audioEngine_getTickPosition(); 1846 unsigned int initTick = ( unsigned int )( m_nRealtimeFrames / m_pAudioDriver->m_transport.m_nTickSize ); 1949 unsigned int initTick = ( unsigned int )( m_nRealtimeFrames 1950 / m_pAudioDriver->m_transport.m_nTickSize ); 1847 1951 unsigned long retTick; 1848 1952 … … 1856 1960 1857 1961 // add a buffers worth for jitter resistance 1858 double deltaSec = ( double ) deltatime.tv_sec + ( deltatime.tv_usec / 1000000.0 ) + ( m_pAudioDriver->getBufferSize() / ( double )sampleRate ); 1859 1860 retTick = ( unsigned long ) ( ( sampleRate / ( double ) m_pAudioDriver->m_transport.m_nTickSize ) * deltaSec ); 1962 double deltaSec = 1963 ( double ) deltatime.tv_sec 1964 + ( deltatime.tv_usec / 1000000.0 ) 1965 + ( m_pAudioDriver->getBufferSize() / ( double )sampleRate ); 1966 1967 retTick = ( unsigned long ) ( ( sampleRate 1968 / ( double ) m_pAudioDriver->m_transport.m_nTickSize ) 1969 * deltaSec ); 1861 1970 1862 1971 retTick = initTick + retTick; … … 1897 2006 }*/ 1898 2007 } else { 1899 _ERRORLOG( QString( "pos not in patternList range. pos=%1 patternListSize=%2" ).arg( pos ).arg( patternList->get_size() ) ); 2008 _ERRORLOG( QString( "pos not in patternList range. pos=%1 " 2009 "patternListSize=%2" ) 2010 .arg( pos ) 2011 .arg( patternList->get_size() ) ); 1900 2012 m_pNextPatterns->clear(); 1901 2013 } … … 1962 2074 int res = m_pAudioDriver->init( pPref->m_nBufferSize ); 1963 2075 if ( res != 0 ) { 1964 _ERRORLOG( "Error starting disk writer driver [DiskWriterDriver::init()]" ); 2076 _ERRORLOG( "Error starting disk writer driver " 2077 "[DiskWriterDriver::init()]" ); 1965 2078 } 1966 2079 … … 1974 2087 res = m_pAudioDriver->connect(); 1975 2088 if ( res != 0 ) { 1976 _ERRORLOG( "Error starting disk writer driver [DiskWriterDriver::connect()]" ); 2089 _ERRORLOG( "Error starting disk writer driver " 2090 "[DiskWriterDriver::connect()]" ); 1977 2091 } 1978 2092 } … … 2120 2234 } else { 2121 2235 pInstr = Instrument::create_empty(); 2122 // The instrument isn't playing yet; no need for locking :-) - Jakob Lund. 2123 // AudioEngine::get_instance()->lock( "Hydrogen::loadDrumkit" ); 2236 // The instrument isn't playing yet; no need for locking 2237 // :-) - Jakob Lund. AudioEngine::get_instance()->lock( 2238 // "Hydrogen::loadDrumkit" ); 2124 2239 songInstrList->add( pInstr ); 2125 2240 // AudioEngine::get_instance()->unlock(); … … 2128 2243 Instrument *pNewInstr = pDrumkitInstrList->get( nInstr ); 2129 2244 assert( pNewInstr ); 2130 _INFOLOG( QString( "Loading instrument (%1 of %2) [%3]" ).arg( nInstr ).arg( pDrumkitInstrList->get_size() ).arg( pNewInstr->get_name() ) ); 2245 _INFOLOG( QString( "Loading instrument (%1 of %2) [%3]" ) 2246 .arg( nInstr ) 2247 .arg( pDrumkitInstrList->get_size() ) 2248 .arg( pNewInstr->get_name() ) ); 2131 2249 2132 2250 // creo i nuovi layer in base al nuovo strumento … … 2139 2257 if ( instrumentDiff >=0 ){ 2140 2258 for ( int i = 0; i < instrumentDiff ; i++ ){ 2141 removeInstrument( m_pSong->get_instrument_list()->get_size() - 1, true ); 2259 removeInstrument( 2260 m_pSong->get_instrument_list()->get_size() - 1, 2261 true 2262 ); 2142 2263 } 2143 2264 } … … 2153 2274 2154 2275 2155 //this is also a new function and will used from the new delete function in Hydrogen::loadDrumkit to delete the instruments by number 2276 //this is also a new function and will used from the new delete function in 2277 //Hydrogen::loadDrumkit to delete the instruments by number 2156 2278 void Hydrogen::removeInstrument( int instrumentnumber, bool conditional ) 2157 2279 { … … 2162 2284 2163 2285 if ( conditional ) { 2164 // new! this check if a pattern has an active note 2165 //if there is an note inside the pattern the intrument would not be deleted 2166 for ( int nPattern = 0; nPattern < (int)pPatternList->get_size(); ++nPattern ) { 2167 if( pPatternList->get( nPattern )->references_instrument( pInstr ) ) { 2286 // new! this check if a pattern has an active note if there is an note 2287 //inside the pattern the intrument would not be deleted 2288 for ( int nPattern = 0 ; 2289 nPattern < (int)pPatternList->get_size() ; 2290 ++nPattern ) { 2291 if( pPatternList 2292 ->get( nPattern ) 2293 ->references_instrument( pInstr ) ) { 2168 2294 return; 2169 2295 } … … 2173 2299 } 2174 2300 2175 // if the instrument was the last on the instruments list, select the next-last 2176 if ( instrumentnumber >= (int)getSong()->get_instrument_list()->get_size() -1 ) { 2177 Hydrogen::get_instance()->setSelectedInstrumentNumber(std::max(0, instrumentnumber - 1) ); 2301 // if the instrument was the last on the instruments list, select the 2302 // next-last 2303 if ( instrumentnumber 2304 >= (int)getSong()->get_instrument_list()->get_size() - 1 ) { 2305 Hydrogen::get_instance() 2306 ->setSelectedInstrumentNumber( 2307 std::max(0, instrumentnumber - 1) 2308 ); 2178 2309 } 2179 2310 // delete the instrument from the instruments list … … 2183 2314 AudioEngine::get_instance()->unlock(); 2184 2315 2185 // At this point the instrument has been removed from both the instrument list and every pattern in the song. 2186 // Hence there's no way (NOTE) to play on that instrument, and once all notes have stopped playing it will be save to delete. 2316 // At this point the instrument has been removed from both the 2317 // instrument list and every pattern in the song. Hence there's no way 2318 // (NOTE) to play on that instrument, and once all notes have stopped 2319 // playing it will be save to delete. 2187 2320 // the ugly name is just for debugging... 2188 2321 QString xxx_name = QString( "XXX_%1" ) . arg( pInstr->get_name() ); … … 2225 2358 pos = pos % nPatternGroups; 2226 2359 } else { 2227 _WARNINGLOG( QString( "patternPos > nPatternGroups. pos: %1, nPatternGroups: %2").arg( pos ).arg( nPatternGroups ) ); 2360 _WARNINGLOG( QString( "patternPos > nPatternGroups. pos:" 2361 " %1, nPatternGroups: %2") 2362 .arg( pos ) 2363 .arg( nPatternGroups ) ); 2228 2364 return -1; 2229 2365 } … … 2236 2372 for ( int i = 0; i < pos; ++i ) { 2237 2373 PatternList *pColumn = ( *pColumns )[ i ]; 2238 pPattern = pColumn->get( 0 ); // prendo solo il primo. I pattern nel gruppo devono avere la stessa lunghezza 2374 // prendo solo il primo. I pattern nel gruppo devono avere la 2375 // stessa lunghezza 2376 pPattern = pColumn->get( 0 ); 2239 2377 if ( pPattern ) { 2240 2378 nPatternSize = pPattern->get_lenght(); … … 2262 2400 // find pattern immediately when not playing 2263 2401 // int dummy; 2264 // m_nSongPos = findPatternInTick( totalTick, m_pSong->is_loop_enabled(), &dummy ); 2402 // m_nSongPos = findPatternInTick( totalTick, 2403 // m_pSong->is_loop_enabled(), 2404 // &dummy ); 2265 2405 m_nSongPos = pos; 2266 2406 m_nPatternTickPosition = 0; 2267 2407 } 2268 m_pAudioDriver->locate( ( int ) ( totalTick * m_pAudioDriver->m_transport.m_nTickSize ) ); 2408 m_pAudioDriver->locate( 2409 ( int ) ( totalTick * m_pAudioDriver->m_transport.m_nTickSize ) 2410 ); 2269 2411 2270 2412 AudioEngine::get_instance()->unlock(); … … 2305 2447 gettimeofday(&now, NULL); 2306 2448 2307 float fInterval = (now.tv_sec - oldTimeVal.tv_sec) * 1000.0 + (now.tv_usec - oldTimeVal.tv_usec) / 1000.0; 2449 float fInterval = 2450 (now.tv_sec - oldTimeVal.tv_sec) * 1000.0 2451 + (now.tv_usec - oldTimeVal.tv_usec) / 1000.0; 2308 2452 2309 2453 oldTimeVal = now; … … 2352 2496 } 2353 2497 2354 fBPM = ( fBPM + fOldBpm1 + fOldBpm2 + fOldBpm3 + fOldBpm4 + fOldBpm5 + fOldBpm6 + fOldBpm7 + fOldBpm8 ) / 9.0; 2498 fBPM = ( fBPM + fOldBpm1 + fOldBpm2 + fOldBpm3 + fOldBpm4 + fOldBpm5 2499 + fOldBpm6 + fOldBpm7 + fOldBpm8 ) / 9.0; 2355 2500 2356 2501 … … 2416 2561 2417 2562 if ( Preferences::getInstance()->patternModePlaysSelected() ) { 2418 AudioEngine::get_instance()->lock( "Hydrogen::setSelectedPatternNumber" ); 2563 AudioEngine::get_instance() 2564 ->lock( "Hydrogen::setSelectedPatternNumber" ); 2419 2565 2420 2566 m_nSelectedPatternNumber = nPat; … … 2502 2648 void Hydrogen::handleBeatCounter() 2503 2649 { 2504 2505 2650 // Get first time value: 2506 2651 if (beatCount == 1) … … 2510 2655 2511 2656 // Set wlastTime to wcurrentTime to remind the time: 2512 lastTime = currentTime;2657 lastTime = currentTime; 2513 2658 2514 2659 // Get new time: 2515 gettimeofday(¤tTime,NULL);2660 gettimeofday(¤tTime,NULL); 2516 2661 2517 2662 2518 2663 // Build doubled time difference: 2519 lastBeatTime = (double)(lastTime.tv_sec + (double)(lastTime.tv_usec * US_DIVIDER) + (int)m_nCoutOffset * .0001 ); 2520 currentBeatTime = (double)(currentTime.tv_sec + (double)(currentTime.tv_usec * US_DIVIDER) ); 2521 beatDiff = beatCount == 1 ? 0 : currentBeatTime - lastBeatTime; 2664 lastBeatTime = (double)( 2665 lastTime.tv_sec 2666 + (double)(lastTime.tv_usec * US_DIVIDER) 2667 + (int)m_nCoutOffset * .0001 2668 ); 2669 currentBeatTime = (double)( 2670 currentTime.tv_sec 2671 + (double)(currentTime.tv_usec * US_DIVIDER) 2672 ); 2673 beatDiff = beatCount == 1 ? 0 : currentBeatTime - lastBeatTime; 2522 2674 2523 2675 //if differences are to big reset the beatconter … … 2537 2689 for(int i = 0; i < (m_nbeatsToCount - 1); i++) 2538 2690 beatTotalDiffs += beatDiffs[i]; 2539 double beatDiffAverage = beatTotalDiffs / (beatCount - 1) * m_ntaktoMeterCompute ; 2540 beatCountBpm = (float) ((int) (60 / beatDiffAverage * 100)) / 100; 2541 AudioEngine::get_instance()->lock( "Hydrogen::handleBeatCounter"); 2691 double beatDiffAverage = 2692 beatTotalDiffs 2693 / (beatCount - 1) 2694 * m_ntaktoMeterCompute ; 2695 beatCountBpm = 2696 (float) ((int) (60 / beatDiffAverage * 100)) 2697 / 100; 2698 AudioEngine::get_instance() 2699 ->lock( "Hydrogen::handleBeatCounter"); 2542 2700 if ( beatCountBpm > 500) 2543 2701 beatCountBpm = 500; 2544 2702 setBPM( beatCountBpm ); 2545 2703 AudioEngine::get_instance()->unlock(); 2546 if (Preferences::getInstance()->m_mmcsetplay == Preferences::SET_PLAY_OFF) { 2704 if (Preferences::getInstance()->m_mmcsetplay 2705 == Preferences::SET_PLAY_OFF) { 2547 2706 beatCount = 1; 2548 2707 eventCount = 1; 2549 2708 }else{ 2550 2709 if ( m_audioEngineState != STATE_PLAYING ){ 2551 unsigned bcsamplerate = m_pAudioDriver->getSampleRate(); 2710 unsigned bcsamplerate = 2711 m_pAudioDriver->getSampleRate(); 2552 2712 unsigned long rtstartframe = 0; 2553 2713 if ( m_ntaktoMeterCompute <= 1){ 2554 rtstartframe = bcsamplerate * beatDiffAverage * ( 1/ m_ntaktoMeterCompute ) ; 2714 rtstartframe = 2715 bcsamplerate 2716 * beatDiffAverage 2717 * ( 1/ m_ntaktoMeterCompute ); 2555 2718 }else 2556 2719 { 2557 rtstartframe = bcsamplerate * beatDiffAverage / m_ntaktoMeterCompute ; 2720 rtstartframe = 2721 bcsamplerate 2722 * beatDiffAverage 2723 / m_ntaktoMeterCompute ; 2558 2724 } 2559 2725 2560 int sleeptime = (float) rtstartframe / (float) bcsamplerate * (int) 1000 + (int)m_nCoutOffset + (int) m_nStartOffset; 2726 int sleeptime = 2727 ( (float) rtstartframe 2728 / (float) bcsamplerate 2729 * (int) 1000 ) 2730 + (int)m_nCoutOffset 2731 + (int) m_nStartOffset; 2561 2732 #ifdef WIN32 2562 2733 Sleep( sleeptime ); … … 2613 2784 int oldtick = getTickPosition(); 2614 2785 for (int i = 0; i <= getPatternPos(); i++){ 2615 float framesforposition = (long)getTickForHumanPosition(i) * (float)m_pAudioDriver->m_transport.m_nTickSize; 2786 float framesforposition = 2787 (long)getTickForHumanPosition(i) 2788 * (float)m_pAudioDriver->m_transport.m_nTickSize; 2616 2789 allframes = framesforposition + allframes; 2617 2790 } 2618 unsigned long framesfortimemaster = (unsigned int)(allframes + oldtick * (float)m_pAudioDriver->m_transport.m_nTickSize); 2791 unsigned long framesfortimemaster = (unsigned int)( 2792 allframes 2793 + oldtick * (float)m_pAudioDriver->m_transport.m_nTickSize 2794 ); 2619 2795 m_nHumantimeFrames = framesfortimemaster; 2620 2796 return framesfortimemaster; … … 2639 2815 } 2640 2816 2641 // std::vector<PatternList*> *pColumns = m_pSong->get_pattern_group_vector()[ humanpos - 1 ].get( 0 )->get_lenght(); 2817 // std::vector<PatternList*> *pColumns = 2818 // m_pSong->get_pattern_group_vector()[ humanpos - 1 ] 2819 // .get( 0 )->get_lenght(); 2642 2820 2643 2821 // ERRORLOG( "Kick me!" ); … … 2710 2888 { 2711 2889 m_pPlayingPatterns->clear(); 2712 Pattern * pSelectedPattern = m_pSong->get_pattern_list()->get(m_nSelectedPatternNumber); 2890 Pattern * pSelectedPattern = 2891 m_pSong 2892 ->get_pattern_list() 2893 ->get(m_nSelectedPatternNumber); 2713 2894 m_pPlayingPatterns->add( pSelectedPattern ); 2714 2895 } … … 2724 2905 int c = 0; 2725 2906 Instrument * pInstr = NULL; 2726 while ( __instrument_death_row.size() && __instrument_death_row.front()->is_queued() == 0 ) { 2907 while ( __instrument_death_row.size() 2908 && __instrument_death_row.front()->is_queued() == 0 ) { 2727 2909 pInstr = __instrument_death_row.front(); 2728 2910 __instrument_death_row.pop_front(); 2729 INFOLOG( QString( "Deleting unused instrument (%1). %2 unused remain." ) \ 2730 . arg( pInstr->get_name() ) \ 2911 INFOLOG( QString( "Deleting unused instrument (%1). " 2912 "%2 unused remain." ) 2913 . arg( pInstr->get_name() ) 2731 2914 . arg( __instrument_death_row.size() ) ); 2732 2915 delete pInstr; … … 2735 2918 if ( __instrument_death_row.size() ) { 2736 2919 pInstr = __instrument_death_row.front(); 2737 INFOLOG( QString( "Instrument %1 still has %2 active notes. Delaying 'delete instrument' operation." ) \ 2738 . arg( pInstr->get_name() ) \ 2920 INFOLOG( QString( "Instrument %1 still has %2 active notes. " 2921 "Delaying 'delete instrument' operation." ) 2922 . arg( pInstr->get_name() ) 2739 2923 . arg( pInstr->is_queued() ) ); 2740 2924 }