520 auto pSong = pHydrogen->getSong();
521 if ( pSong ==
nullptr ) {
527 if ( pInstr ==
nullptr ) {
533 auto pAudioDriver = pHydrogen->getAudioOutput();
534 if ( pAudioDriver ==
nullptr ) {
535 ERRORLOG(
"AudioDriver is not ready!" );
539 auto pAudioEngine = pHydrogen->getAudioEngine();
542 nFrame = pAudioEngine->getTransportPosition()->getFrame();
546 nFrame = pAudioEngine->getRealtimeFrame();
553 long long nInitialBufferPos = 0;
565 if ( nNoteStartInFrames > nFrame ) {
568 nInitialBufferPos = nNoteStartInFrames - nFrame;
570 if ( nBufferSize < nInitialBufferPos ) {
572 ERRORLOG( QString(
"Note pos in the future?? nFrame: %1, note start: %2, nInitialBufferPos: %3, nBufferSize: %4" )
574 .arg( nInitialBufferPos ).arg( nBufferSize ) );
601 float fPan = pInstr->getPan() + pNote->
getPan() * ( 1 - fabs( pInstr->getPan() ) );
604 float fPan_L =
panLaw( fPan, pSong );
605 float fPan_R =
panLaw( -fPan, pSong );
610 float fNotePan_L = 0;
611 float fNotePan_R = 0;
612 if ( pHydrogen->hasJackAudioDriver() &&
620 auto pComponents = pInstr->get_components();
621 bool nReturnValues[ pComponents->size() ];
623 for(
int i = 0; i < pComponents->size(); i++ ){
624 nReturnValues[i] =
false;
627 int nReturnValueIndex = 0;
628 int nAlreadySelectedLayer = -1;
629 bool bComponentFound =
false;
631 for (
const auto& pCompo : *pComponents ) {
632 std::shared_ptr<DrumkitComponent> pMainCompo =
nullptr;
639 bComponentFound =
true;
641 if ( pInstr->is_preview_instrument() ||
642 pInstr->is_metronome_instrument() ){
643 pMainCompo = pSong->getComponents()->front();
645 int nComponentID = pCompo->get_drumkit_componentID();
646 if ( nComponentID >= 0 ) {
647 pMainCompo = pSong->getComponent( nComponentID );
650 pMainCompo = pSong->getComponents()->front();
656 auto pSample = pNote->
getSample( pCompo->get_drumkit_componentID(),
657 nAlreadySelectedLayer );
658 if ( pSample ==
nullptr ) {
659 nReturnValues[nReturnValueIndex] =
true;
664 auto pSelectedLayer =
669 if ( nAlreadySelectedLayer != -1 &&
671 nAlreadySelectedLayer = pSelectedLayer->nSelectedLayer;
674 if( pSelectedLayer->nSelectedLayer == -1 ) {
675 ERRORLOG(
"Sample selection did not work." );
676 nReturnValues[nReturnValueIndex] =
true;
680 auto pLayer = pCompo->get_layer( pSelectedLayer->nSelectedLayer );
681 float fLayerGain = pLayer->get_gain();
682 float fLayerPitch = pLayer->get_pitch();
684 if ( pSelectedLayer->fSamplePosition >= pSample->get_frames() ) {
690 if ( pSelectedLayer->fSamplePosition >= pSample->get_frames() + 3 ) {
691 WARNINGLOG( QString(
"sample position [%1] out of bounds [0,%2]. The layer has been resized during note play?" )
692 .arg( pSelectedLayer->fSamplePosition )
693 .arg( pSample->get_frames() ) );
695 nReturnValues[nReturnValueIndex] =
true;
700 float fCost_L = 1.0f;
701 float fCost_R = 1.0f;
702 float fCostTrack_L = 1.0f;
703 float fCostTrack_R = 1.0f;
705 bool bIsMutedForExport = ( pHydrogen->getIsExportSessionActive() &&
706 ! pInstr->is_currently_exported() );
707 bool bAnyInstrumentIsSoloed = pSong->getInstrumentList()->isAnyInstrumentSoloed();
708 bool bIsMutedBecauseOfSolo = ( bAnyInstrumentIsSoloed &&
709 ! pInstr->is_soloed() );
720 if ( bIsMutedForExport || pInstr->is_muted() || pSong->getIsMuted() ||
721 pMainCompo->is_muted() || bIsMutedBecauseOfSolo) {
731 float fMonoGain = 1.0;
732 if ( pInstr->get_apply_velocity() ) {
736 fMonoGain *= fLayerGain;
737 fMonoGain *= pInstr->get_gain();
738 fMonoGain *= pCompo->get_gain();
739 fMonoGain *= pMainCompo->get_volume();
740 fMonoGain *= pInstr->get_volume();
741 fMonoGain *= pSong->getVolume();
743 fCost_L = fMonoGain * fPan_L;
744 fCost_R = fMonoGain * fPan_R;
747 fCostTrack_R = fCost_R * 2;
748 fCostTrack_L = fCost_L * 2;
755 if ( pInstr->get_apply_velocity() ) {
758 fCostTrack_L *= fLayerGain;
760 fCostTrack_R = fCostTrack_L;
762 fCostTrack_L *= fNotePan_L;
763 fCostTrack_R *= fNotePan_R;
775 if ( (
int) pSelectedLayer->fSamplePosition == 0 && ! pInstr->is_muted() ) {
776 if ( pHydrogen->getMidiOutput() != nullptr ){
777 pHydrogen->getMidiOutput()->handleQueueNote( pNote );
782 nReturnValues[nReturnValueIndex] =
renderNoteResample( pSample, pNote, pSelectedLayer, pCompo, pMainCompo, nBufferSize, nInitialBufferPos, fCost_L, fCost_R, fCostTrack_L, fCostTrack_R, fLayerPitch );
788 if ( ! bComponentFound ) {
789 ERRORLOG( QString(
"Specific note component [%1] not found in instrument associated with note: [%2]" )
795 for (
const auto& bReturnValue : nReturnValues ) {
796 if ( ! bReturnValue ) {
808 std::shared_ptr<Song> pSong = pHydrogen->
getSong();
810 if ( pSong ==
nullptr ) {
815 if ( pAudioDriver ==
nullptr ) {
816 ERRORLOG(
"AudioDriver is not ready!" );
828 if ( pCompo ==
nullptr ) {
829 ERRORLOG(
"Invalid component of playback instrument" );
833 auto pSample = pCompo->get_layer(0)->get_sample();
834 if ( pSample ==
nullptr ) {
835 ERRORLOG(
"Unable to process playback track" );
847 auto pSample_data_L = pSample->get_data_l();
848 auto pSample_data_R = pSample->get_data_r();
853 int nAvail_bytes = 0;
854 int nInitialBufferPos = 0;
856 const long long nFrame = pAudioEngine->getTransportPosition()->getFrame();
857 const long long nFrameOffset =
858 pAudioEngine->getTransportPosition()->getFrameOffsetTempo();
860 if ( pSample->get_sample_rate() == pAudioDriver->getSampleRate() ) {
866 if ( nAvail_bytes > nBufferSize ) {
867 nAvail_bytes = nBufferSize;
871 int nSamplePos = nInitialSamplePos;
873 int nFinalBufferPos = nInitialBufferPos + nAvail_bytes;
880 for (
int nBufferPos = nInitialBufferPos; nBufferPos < nFinalBufferPos; ++nBufferPos ) {
881 fVal_L = pSample_data_L[ nSamplePos ];
882 fVal_R = pSample_data_R[ nSamplePos ];
884 fVal_L = fVal_L * 1.0f * pSong->getPlaybackTrackVolume();
885 fVal_R = fVal_R * 1.0f * pSong->getPlaybackTrackVolume();
890 if ( fVal_L > fInstrPeak_L ) {
891 fInstrPeak_L = fVal_L;
893 if ( fVal_R > fInstrPeak_R ) {
894 fInstrPeak_R = fVal_R;
904 double fSamplePos = 0;
905 int nSampleFrames = pSample->get_frames();
907 fStep *= ( float )pSample->get_sample_rate() / pAudioDriver->getSampleRate();
913 fSamplePos = ( nFrame - nFrameOffset ) * fStep;
916 nAvail_bytes = ( int )( (
float )( pSample->get_frames() - fSamplePos ) / fStep );
918 if ( nAvail_bytes > nBufferSize ) {
919 nAvail_bytes = nBufferSize;
922 int nFinalBufferPos = nInitialBufferPos + nAvail_bytes;
924 for (
int nBufferPos = nInitialBufferPos; nBufferPos < nFinalBufferPos; ++nBufferPos ) {
925 int nSamplePos = ( int ) fSamplePos;
926 double fDiff = fSamplePos - nSamplePos;
927 if ( ( nSamplePos + 1 ) >= nSampleFrames ) {
936 if ( ( nSamplePos + 2 ) >= nSampleFrames ) {
940 last_l = pSample_data_L[nSamplePos + 2];
941 last_r = pSample_data_R[nSamplePos + 2];
946 case Interpolation::InterpolateMode::Linear:
947 fVal_L = pSample_data_L[nSamplePos] * (1 - fDiff ) + pSample_data_L[nSamplePos + 1] * fDiff;
948 fVal_R = pSample_data_R[nSamplePos] * (1 - fDiff ) + pSample_data_R[nSamplePos + 1] * fDiff;
950 case Interpolation::InterpolateMode::Cosine:
954 case Interpolation::InterpolateMode::Third:
958 case Interpolation::InterpolateMode::Cubic:
962 case Interpolation::InterpolateMode::Hermite:
969 fVal_L *= pSong->getPlaybackTrackVolume();
970 fVal_R *= pSong->getPlaybackTrackVolume();
972 if ( fVal_L > fInstrPeak_L ) {
973 fInstrPeak_L = fVal_L;
975 if ( fVal_R > fInstrPeak_R ) {
976 fInstrPeak_R = fVal_R;
994 std::shared_ptr<Sample> pSample,
996 std::shared_ptr<SelectedLayerInfo> pSelectedLayerInfo,
997 std::shared_ptr<InstrumentComponent> pCompo,
998 std::shared_ptr<DrumkitComponent> pDrumCompo,
1000 int nInitialBufferPos,
1009 auto pAudioDriver = pHydrogen->getAudioOutput();
1010 auto pSong = pHydrogen->getSong();
1012 if ( pSong ==
nullptr ) {
1017 if ( pNote ==
nullptr ) {
1022 if ( pAudioDriver ==
nullptr ) {
1023 ERRORLOG(
"AudioDriver is not ready!" );
1028 if ( pInstrument ==
nullptr ) {
1029 ERRORLOG(
"Invalid note instrument" );
1034 const bool bResample = fNotePitch != 0 ||
1035 pSample->get_sample_rate() != pAudioDriver->getSampleRate();
1042 fStep *=
static_cast<float>(pSample->get_sample_rate()) /
1043 static_cast<float>(pAudioDriver->getSampleRate());
1049 auto pSample_data_L = pSample->get_data_l();
1050 auto pSample_data_R = pSample->get_data_r();
1051 const int nSampleFrames = pSample->get_frames();
1053 const int nRemainingFrames =
static_cast<int>(
1054 (
static_cast<float>(nSampleFrames) - pSelectedLayerInfo->fSamplePosition) /
1057 bool bRetValue =
true;
1059 if ( nRemainingFrames > nBufferSize - nInitialBufferPos ) {
1063 nAvail_bytes = nBufferSize - nInitialBufferPos;
1067 else if ( pInstrument->is_filter_active() && pNote->
filter_sustain() ) {
1069 nAvail_bytes = nBufferSize - nInitialBufferPos;
1072 nAvail_bytes = nRemainingFrames;
1075 double fSamplePos = pSelectedLayerInfo->fSamplePosition;
1076 const int nFinalBufferPos = nInitialBufferPos + nAvail_bytes;
1084 if ( pSelectedLayerInfo->nNoteLength == -1 ) {
1088 double fTickMismatch;
1090 pSelectedLayerInfo->nNoteLength =
1093 &fTickMismatch, pSample->get_sample_rate() ) -
1096 pSample->get_sample_rate() );
1099 nNoteEnd = std::min(nFinalBufferPos + 1,
static_cast<int>(
1100 (
static_cast<float>(pSelectedLayerInfo->nNoteLength) -
1101 pSelectedLayerInfo->fSamplePosition) / fStep ));
1103 if ( nNoteEnd < 0 ) {
1104 if ( ! pInstrument->is_filter_active() ) {
1108 ERRORLOG( QString(
"Note end located within the previous processing cycle. nNoteEnd: %1, nNoteLength: %2, fSamplePosition: %3, nFinalBufferPos: %4, fStep: %5")
1109 .arg( nNoteEnd ).arg( pSelectedLayerInfo->nNoteLength )
1110 .arg( pSelectedLayerInfo->fSamplePosition )
1111 .arg( nFinalBufferPos ).arg( fStep ) );
1120 nNoteEnd = nFinalBufferPos + 1;
1123 float fInstrPeak_L = pInstrument->get_peak_l();
1124 float fInstrPeak_R = pInstrument->get_peak_r();
1130#ifdef H2CORE_HAVE_JACK
1131 float* pTrackOutL =
nullptr;
1132 float* pTrackOutR =
nullptr;
1135 auto pJackAudioDriver =
dynamic_cast<JackAudioDriver*
>( pAudioDriver );
1136 if ( pJackAudioDriver !=
nullptr ) {
1137 pTrackOutL = pJackAudioDriver->
getTrackOut_L( pInstrument, pCompo );
1138 pTrackOutR = pJackAudioDriver->getTrackOut_R( pInstrument, pCompo );
1152 for (
int nBufferPos = nInitialBufferPos; nBufferPos < nFinalBufferPos;
1155 int nSamplePos =
static_cast<int>(fSamplePos);
1156 double fDiff = fSamplePos - nSamplePos;
1157 if ( ( nSamplePos - 1 ) >= nSampleFrames ) {
1163 if ( ! bResample ) {
1164 if ( nSamplePos < nSampleFrames ) {
1165 fVal_L = pSample_data_L[ nSamplePos ];
1166 fVal_R = pSample_data_R[ nSamplePos ];
1174 float l0, l1, l2, l3, r0, r1, r2, r3;
1176 if ( nSamplePos >= 1 && nSamplePos + 2 < nSampleFrames ) {
1177 l0= pSample_data_L[ nSamplePos-1 ];
1178 l1 = pSample_data_L[ nSamplePos ];
1179 l2 = pSample_data_L[ nSamplePos+1 ];
1180 l3 = pSample_data_L[ nSamplePos+2 ];
1181 r0 = pSample_data_R[ nSamplePos-1 ];
1182 r1 = pSample_data_R[ nSamplePos ];
1183 r2 = pSample_data_R[ nSamplePos+1 ];
1184 r3 = pSample_data_R[ nSamplePos+2 ];
1186 l0 = l1 = l2 = l3 = r0 = r1 = r2 = r3 = 0.0;
1188 if ( nSamplePos >= 1 && nSamplePos < nSampleFrames + 1 ) {
1189 l0 = pSample_data_L[ nSamplePos-1 ];
1190 r0 = pSample_data_R[ nSamplePos-1 ];
1193 if ( nSamplePos < nSampleFrames ) {
1194 l1 = pSample_data_L[ nSamplePos ];
1195 r1 = pSample_data_R[ nSamplePos ];
1196 if ( nSamplePos+1 < nSampleFrames ) {
1197 l2 = pSample_data_L[ nSamplePos+1 ];
1198 r2 = pSample_data_R[ nSamplePos+1 ];
1199 if ( nSamplePos+2 < nSampleFrames ) {
1200 l3 = pSample_data_L[ nSamplePos+2 ];
1201 r3 = pSample_data_R[ nSamplePos+2 ];
1209 case Interpolation::InterpolateMode::Linear:
1210 fVal_L = l1 * (1 - fDiff ) + l2 * fDiff;
1211 fVal_R = r1 * (1 - fDiff ) + r2 * fDiff;
1213 case Interpolation::InterpolateMode::Cosine:
1217 case Interpolation::InterpolateMode::Third:
1221 case Interpolation::InterpolateMode::Cubic:
1225 case Interpolation::InterpolateMode::Hermite:
1233 buffer_L[nBufferPos] = fVal_L;
1234 buffer_R[nBufferPos] = fVal_R;
1236 fSamplePos += fStep;
1239 if ( pADSR->applyADSR( buffer_L, buffer_R, nFinalBufferPos, nNoteEnd,
1245 if ( pInstrument->is_filter_active() ) {
1246 for (
int nBufferPos = nInitialBufferPos; nBufferPos < nFinalBufferPos;
1249 fVal_L = buffer_L[ nBufferPos ];
1250 fVal_R = buffer_R[ nBufferPos ];
1254 buffer_L[ nBufferPos ] = fVal_L;
1255 buffer_R[ nBufferPos ] = fVal_R;
1261 for (
int nBufferPos = nInitialBufferPos; nBufferPos < nFinalBufferPos;
1264 fVal_L = buffer_L[ nBufferPos ];
1265 fVal_R = buffer_R[ nBufferPos ];
1267#ifdef H2CORE_HAVE_JACK
1269 pTrackOutL[nBufferPos] += fVal_L * fCostTrack_L;
1272 pTrackOutR[nBufferPos] += fVal_R * fCostTrack_R;
1280 if ( fVal_L > fInstrPeak_L ) {
1281 fInstrPeak_L = fVal_L;
1283 if ( fVal_R > fInstrPeak_R ) {
1284 fInstrPeak_R = fVal_R;
1287 pDrumCompo->set_outs( nBufferPos, fVal_L, fVal_R );
1295 if ( pInstrument->is_filter_active() && pNote->
filter_sustain() ) {
1300 pSelectedLayerInfo->fSamplePosition += nAvail_bytes * fStep;
1301 pInstrument->set_peak_l( fInstrPeak_L );
1302 pInstrument->set_peak_r( fInstrPeak_R );
1305#ifdef H2CORE_HAVE_LADSPA
1308 if ( pInstrument->is_muted() || pSong->getIsMuted() ) {
1311 float masterVol = pSong->getVolume();
1312 for (
unsigned nFX = 0; nFX <
MAX_FX; ++nFX ) {
1314 float fLevel = pInstrument->get_fx_level( nFX );
1315 if ( pFX !=
nullptr && fLevel != 0.0 ) {
1321 float fFXCost_L = fLevel * masterVol;
1322 float fFXCost_R = fLevel * masterVol;
1324 int nBufferPos = nInitialBufferPos;
1325 for (
int i = 0; i < nAvail_bytes; ++i ) {
1327 fVal_L = buffer_L[ nBufferPos ];
1328 fVal_R = buffer_R[ nBufferPos ];
1330 pBuf_L[ nBufferPos ] += fVal_L * fFXCost_L;
1331 pBuf_R[ nBufferPos ] += fVal_R * fFXCost_R;