514 auto pSong = pHydrogen->getSong();
515 if ( pSong ==
nullptr ) {
521 if ( pInstr ==
nullptr ) {
527 auto pAudioDriver = pHydrogen->getAudioOutput();
528 if ( pAudioDriver ==
nullptr ) {
529 ERRORLOG(
"AudioDriver is not ready!" );
533 auto pAudioEngine = pHydrogen->getAudioEngine();
536 nFrame = pAudioEngine->getTransportPosition()->getFrame();
540 nFrame = pAudioEngine->getRealtimeFrame();
547 long long nInitialBufferPos = 0;
559 if ( nNoteStartInFrames > nFrame ) {
562 nInitialBufferPos = nNoteStartInFrames - nFrame;
564 if ( nBufferSize < nInitialBufferPos ) {
566 ERRORLOG( QString(
"Note pos in the future?? nFrame: %1, note start: %2, nInitialBufferPos: %3, nBufferSize: %4" )
568 .arg( nInitialBufferPos ).arg( nBufferSize ) );
595 float fPan = pInstr->getPan() + pNote->
getPan() * ( 1 - fabs( pInstr->getPan() ) );
598 float fPan_L =
panLaw( fPan, pSong );
599 float fPan_R =
panLaw( -fPan, pSong );
604 float fNotePan_L = 0;
605 float fNotePan_R = 0;
606 if ( pHydrogen->hasJackAudioDriver() &&
614 auto pComponents = pInstr->get_components();
615 bool nReturnValues[ pComponents->size() ];
617 for(
int i = 0; i < pComponents->size(); i++ ){
618 nReturnValues[i] =
false;
621 int nReturnValueIndex = 0;
622 int nAlreadySelectedLayer = -1;
623 bool bComponentFound =
false;
625 for (
const auto& pCompo : *pComponents ) {
626 std::shared_ptr<DrumkitComponent> pMainCompo =
nullptr;
633 bComponentFound =
true;
635 if ( pInstr->is_preview_instrument() ||
636 pInstr->is_metronome_instrument() ){
637 pMainCompo = pSong->getComponents()->front();
639 int nComponentID = pCompo->get_drumkit_componentID();
640 if ( nComponentID >= 0 ) {
641 pMainCompo = pSong->getComponent( nComponentID );
644 pMainCompo = pSong->getComponents()->front();
650 auto pSample = pNote->
getSample( pCompo->get_drumkit_componentID(),
651 nAlreadySelectedLayer );
652 if ( pSample ==
nullptr ) {
653 nReturnValues[nReturnValueIndex] =
true;
658 auto pSelectedLayer =
663 if ( nAlreadySelectedLayer != -1 &&
665 nAlreadySelectedLayer = pSelectedLayer->nSelectedLayer;
668 if( pSelectedLayer->nSelectedLayer == -1 ) {
669 ERRORLOG(
"Sample selection did not work." );
670 nReturnValues[nReturnValueIndex] =
true;
674 auto pLayer = pCompo->get_layer( pSelectedLayer->nSelectedLayer );
675 float fLayerGain = pLayer->get_gain();
676 float fLayerPitch = pLayer->get_pitch();
678 if ( pSelectedLayer->fSamplePosition >= pSample->get_frames() ) {
684 if ( pSelectedLayer->fSamplePosition >= pSample->get_frames() + 3 ) {
685 WARNINGLOG( QString(
"sample position [%1] out of bounds [0,%2]. The layer has been resized during note play?" )
686 .arg( pSelectedLayer->fSamplePosition )
687 .arg( pSample->get_frames() ) );
689 nReturnValues[nReturnValueIndex] =
true;
694 float fCost_L = 1.0f;
695 float fCost_R = 1.0f;
696 float fCostTrack_L = 1.0f;
697 float fCostTrack_R = 1.0f;
699 bool bIsMutedForExport = ( pHydrogen->getIsExportSessionActive() &&
700 ! pInstr->is_currently_exported() );
701 bool bAnyInstrumentIsSoloed = pSong->getInstrumentList()->isAnyInstrumentSoloed();
702 bool bIsMutedBecauseOfSolo = ( bAnyInstrumentIsSoloed &&
703 ! pInstr->is_soloed() );
714 if ( bIsMutedForExport || pInstr->is_muted() || pSong->getIsMuted() ||
715 pMainCompo->is_muted() || bIsMutedBecauseOfSolo) {
725 float fMonoGain = 1.0;
726 if ( pInstr->get_apply_velocity() ) {
730 fMonoGain *= fLayerGain;
731 fMonoGain *= pInstr->get_gain();
732 fMonoGain *= pCompo->get_gain();
733 fMonoGain *= pMainCompo->get_volume();
734 fMonoGain *= pInstr->get_volume();
735 fMonoGain *= pSong->getVolume();
737 fCost_L = fMonoGain * fPan_L;
738 fCost_R = fMonoGain * fPan_R;
741 fCostTrack_R = fCost_R * 2;
742 fCostTrack_L = fCost_L * 2;
749 if ( pInstr->get_apply_velocity() ) {
752 fCostTrack_L *= fLayerGain;
754 fCostTrack_R = fCostTrack_L;
756 fCostTrack_L *= fNotePan_L;
757 fCostTrack_R *= fNotePan_R;
769 if ( (
int) pSelectedLayer->fSamplePosition == 0 && ! pInstr->is_muted() ) {
770 if ( pHydrogen->getMidiOutput() !=
nullptr ){
771 pHydrogen->getMidiOutput()->handleQueueNote( pNote );
776 nReturnValues[nReturnValueIndex] =
renderNoteResample( pSample, pNote, pSelectedLayer, pCompo, pMainCompo, nBufferSize, nInitialBufferPos, fCost_L, fCost_R, fCostTrack_L, fCostTrack_R, fLayerPitch );
782 if ( ! bComponentFound ) {
783 ERRORLOG( QString(
"Specific note component [%1] not found in instrument associated with note: [%2]" )
789 for (
const auto& bReturnValue : nReturnValues ) {
790 if ( ! bReturnValue ) {
802 std::shared_ptr<Song> pSong = pHydrogen->
getSong();
804 if ( pSong ==
nullptr ) {
809 if ( pAudioDriver ==
nullptr ) {
810 ERRORLOG(
"AudioDriver is not ready!" );
822 if ( pCompo ==
nullptr ) {
823 ERRORLOG(
"Invalid component of playback instrument" );
827 auto pSample = pCompo->get_layer(0)->get_sample();
828 if ( pSample ==
nullptr ) {
829 ERRORLOG(
"Unable to process playback track" );
841 auto pSample_data_L = pSample->get_data_l();
842 auto pSample_data_R = pSample->get_data_r();
847 int nAvail_bytes = 0;
848 int nInitialBufferPos = 0;
850 const long long nFrame = pAudioEngine->getTransportPosition()->getFrame();
851 const long long nFrameOffset =
852 pAudioEngine->getTransportPosition()->getFrameOffsetTempo();
854 if ( pSample->get_sample_rate() == pAudioDriver->getSampleRate() ) {
860 if ( nAvail_bytes > nBufferSize ) {
861 nAvail_bytes = nBufferSize;
865 int nSamplePos = nInitialSamplePos;
867 int nFinalBufferPos = nInitialBufferPos + nAvail_bytes;
874 for (
int nBufferPos = nInitialBufferPos; nBufferPos < nFinalBufferPos; ++nBufferPos ) {
875 fVal_L = pSample_data_L[ nSamplePos ];
876 fVal_R = pSample_data_R[ nSamplePos ];
878 fVal_L = fVal_L * 1.0f * pSong->getPlaybackTrackVolume();
879 fVal_R = fVal_R * 1.0f * pSong->getPlaybackTrackVolume();
884 if ( fVal_L > fInstrPeak_L ) {
885 fInstrPeak_L = fVal_L;
887 if ( fVal_R > fInstrPeak_R ) {
888 fInstrPeak_R = fVal_R;
898 double fSamplePos = 0;
899 int nSampleFrames = pSample->get_frames();
901 fStep *= ( float )pSample->get_sample_rate() / pAudioDriver->getSampleRate();
907 fSamplePos = ( nFrame - nFrameOffset ) * fStep;
910 nAvail_bytes = ( int )( (
float )( pSample->get_frames() - fSamplePos ) / fStep );
912 if ( nAvail_bytes > nBufferSize ) {
913 nAvail_bytes = nBufferSize;
916 int nFinalBufferPos = nInitialBufferPos + nAvail_bytes;
918 for (
int nBufferPos = nInitialBufferPos; nBufferPos < nFinalBufferPos; ++nBufferPos ) {
919 int nSamplePos = ( int ) fSamplePos;
920 double fDiff = fSamplePos - nSamplePos;
921 if ( ( nSamplePos + 1 ) >= nSampleFrames ) {
930 if ( ( nSamplePos + 2 ) >= nSampleFrames ) {
934 last_l = pSample_data_L[nSamplePos + 2];
935 last_r = pSample_data_R[nSamplePos + 2];
940 case Interpolation::InterpolateMode::Linear:
941 fVal_L = pSample_data_L[nSamplePos] * (1 - fDiff ) + pSample_data_L[nSamplePos + 1] * fDiff;
942 fVal_R = pSample_data_R[nSamplePos] * (1 - fDiff ) + pSample_data_R[nSamplePos + 1] * fDiff;
944 case Interpolation::InterpolateMode::Cosine:
948 case Interpolation::InterpolateMode::Third:
952 case Interpolation::InterpolateMode::Cubic:
956 case Interpolation::InterpolateMode::Hermite:
963 fVal_L *= pSong->getPlaybackTrackVolume();
964 fVal_R *= pSong->getPlaybackTrackVolume();
966 if ( fVal_L > fInstrPeak_L ) {
967 fInstrPeak_L = fVal_L;
969 if ( fVal_R > fInstrPeak_R ) {
970 fInstrPeak_R = fVal_R;
988 std::shared_ptr<Sample> pSample,
990 std::shared_ptr<SelectedLayerInfo> pSelectedLayerInfo,
991 std::shared_ptr<InstrumentComponent> pCompo,
992 std::shared_ptr<DrumkitComponent> pDrumCompo,
994 int nInitialBufferPos,
1003 auto pAudioDriver = pHydrogen->getAudioOutput();
1004 auto pSong = pHydrogen->getSong();
1006 if ( pSong ==
nullptr ) {
1011 if ( pNote ==
nullptr ) {
1016 if ( pAudioDriver ==
nullptr ) {
1017 ERRORLOG(
"AudioDriver is not ready!" );
1022 if ( pInstrument ==
nullptr ) {
1023 ERRORLOG(
"Invalid note instrument" );
1028 const bool bResample = fNotePitch != 0 ||
1029 pSample->get_sample_rate() != pAudioDriver->getSampleRate();
1036 fStep *=
static_cast<float>(pSample->get_sample_rate()) /
1037 static_cast<float>(pAudioDriver->getSampleRate());
1043 auto pSample_data_L = pSample->get_data_l();
1044 auto pSample_data_R = pSample->get_data_r();
1045 const int nSampleFrames = pSample->get_frames();
1047 const int nRemainingFrames =
static_cast<int>(
1048 (
static_cast<float>(nSampleFrames) - pSelectedLayerInfo->fSamplePosition) /
1051 bool bRetValue =
true;
1053 if ( nRemainingFrames > nBufferSize - nInitialBufferPos ) {
1057 nAvail_bytes = nBufferSize - nInitialBufferPos;
1061 else if ( pInstrument->is_filter_active() && pNote->
filter_sustain() ) {
1063 nAvail_bytes = nBufferSize - nInitialBufferPos;
1066 nAvail_bytes = nRemainingFrames;
1069 double fSamplePos = pSelectedLayerInfo->fSamplePosition;
1070 const int nFinalBufferPos = nInitialBufferPos + nAvail_bytes;
1078 if ( pSelectedLayerInfo->nNoteLength == -1 ) {
1082 double fTickMismatch;
1084 pSelectedLayerInfo->nNoteLength =
1087 &fTickMismatch, pSample->get_sample_rate() ) -
1090 pSample->get_sample_rate() );
1093 nNoteEnd = std::min(nFinalBufferPos + 1,
static_cast<int>(
1094 (
static_cast<float>(pSelectedLayerInfo->nNoteLength) -
1095 pSelectedLayerInfo->fSamplePosition) / fStep ));
1097 if ( nNoteEnd < 0 ) {
1098 if ( ! pInstrument->is_filter_active() ) {
1102 ERRORLOG( QString(
"Note end located within the previous processing cycle. nNoteEnd: %1, nNoteLength: %2, fSamplePosition: %3, nFinalBufferPos: %4, fStep: %5")
1103 .arg( nNoteEnd ).arg( pSelectedLayerInfo->nNoteLength )
1104 .arg( pSelectedLayerInfo->fSamplePosition )
1105 .arg( nFinalBufferPos ).arg( fStep ) );
1114 nNoteEnd = nFinalBufferPos + 1;
1117 float fInstrPeak_L = pInstrument->get_peak_l();
1118 float fInstrPeak_R = pInstrument->get_peak_r();
1124#ifdef H2CORE_HAVE_JACK
1125 float* pTrackOutL =
nullptr;
1126 float* pTrackOutR =
nullptr;
1129 auto pJackAudioDriver =
dynamic_cast<JackAudioDriver*
>( pAudioDriver );
1130 if ( pJackAudioDriver !=
nullptr ) {
1131 pTrackOutL = pJackAudioDriver->getTrackOut_L( pInstrument, pCompo );
1132 pTrackOutR = pJackAudioDriver->getTrackOut_R( pInstrument, pCompo );
1146 for (
int nBufferPos = nInitialBufferPos; nBufferPos < nFinalBufferPos;
1149 int nSamplePos =
static_cast<int>(fSamplePos);
1150 double fDiff = fSamplePos - nSamplePos;
1151 if ( ( nSamplePos - 1 ) >= nSampleFrames ) {
1157 if ( ! bResample ) {
1158 if ( nSamplePos < nSampleFrames ) {
1159 fVal_L = pSample_data_L[ nSamplePos ];
1160 fVal_R = pSample_data_R[ nSamplePos ];
1168 float l0, l1, l2, l3, r0, r1, r2, r3;
1170 if ( nSamplePos >= 1 && nSamplePos + 2 < nSampleFrames ) {
1171 l0= pSample_data_L[ nSamplePos-1 ];
1172 l1 = pSample_data_L[ nSamplePos ];
1173 l2 = pSample_data_L[ nSamplePos+1 ];
1174 l3 = pSample_data_L[ nSamplePos+2 ];
1175 r0 = pSample_data_R[ nSamplePos-1 ];
1176 r1 = pSample_data_R[ nSamplePos ];
1177 r2 = pSample_data_R[ nSamplePos+1 ];
1178 r3 = pSample_data_R[ nSamplePos+2 ];
1180 l0 = l1 = l2 = l3 = r0 = r1 = r2 = r3 = 0.0;
1182 if ( nSamplePos >= 1 && nSamplePos < nSampleFrames + 1 ) {
1183 l0 = pSample_data_L[ nSamplePos-1 ];
1184 r0 = pSample_data_R[ nSamplePos-1 ];
1187 if ( nSamplePos < nSampleFrames ) {
1188 l1 = pSample_data_L[ nSamplePos ];
1189 r1 = pSample_data_R[ nSamplePos ];
1190 if ( nSamplePos+1 < nSampleFrames ) {
1191 l2 = pSample_data_L[ nSamplePos+1 ];
1192 r2 = pSample_data_R[ nSamplePos+1 ];
1193 if ( nSamplePos+2 < nSampleFrames ) {
1194 l3 = pSample_data_L[ nSamplePos+2 ];
1195 r3 = pSample_data_R[ nSamplePos+2 ];
1203 case Interpolation::InterpolateMode::Linear:
1204 fVal_L = l1 * (1 - fDiff ) + l2 * fDiff;
1205 fVal_R = r1 * (1 - fDiff ) + r2 * fDiff;
1207 case Interpolation::InterpolateMode::Cosine:
1211 case Interpolation::InterpolateMode::Third:
1215 case Interpolation::InterpolateMode::Cubic:
1219 case Interpolation::InterpolateMode::Hermite:
1227 buffer_L[nBufferPos] = fVal_L;
1228 buffer_R[nBufferPos] = fVal_R;
1230 fSamplePos += fStep;
1233 if ( pADSR->applyADSR( buffer_L, buffer_R, nFinalBufferPos, nNoteEnd,
1239 if ( pInstrument->is_filter_active() ) {
1240 for (
int nBufferPos = nInitialBufferPos; nBufferPos < nFinalBufferPos;
1243 fVal_L = buffer_L[ nBufferPos ];
1244 fVal_R = buffer_R[ nBufferPos ];
1248 buffer_L[ nBufferPos ] = fVal_L;
1249 buffer_R[ nBufferPos ] = fVal_R;
1255 for (
int nBufferPos = nInitialBufferPos; nBufferPos < nFinalBufferPos;
1258 fVal_L = buffer_L[ nBufferPos ];
1259 fVal_R = buffer_R[ nBufferPos ];
1261#ifdef H2CORE_HAVE_JACK
1263 pTrackOutL[nBufferPos] += fVal_L * fCostTrack_L;
1266 pTrackOutR[nBufferPos] += fVal_R * fCostTrack_R;
1274 if ( fVal_L > fInstrPeak_L ) {
1275 fInstrPeak_L = fVal_L;
1277 if ( fVal_R > fInstrPeak_R ) {
1278 fInstrPeak_R = fVal_R;
1281 pDrumCompo->set_outs( nBufferPos, fVal_L, fVal_R );
1289 if ( pInstrument->is_filter_active() && pNote->
filter_sustain() ) {
1294 pSelectedLayerInfo->fSamplePosition += nAvail_bytes * fStep;
1295 pInstrument->set_peak_l( fInstrPeak_L );
1296 pInstrument->set_peak_r( fInstrPeak_R );
1299#ifdef H2CORE_HAVE_LADSPA
1302 if ( pInstrument->is_muted() || pSong->getIsMuted() ) {
1305 float masterVol = pSong->getVolume();
1306 for (
unsigned nFX = 0; nFX <
MAX_FX; ++nFX ) {
1308 float fLevel = pInstrument->get_fx_level( nFX );
1309 if ( pFX !=
nullptr && fLevel != 0.0 ) {
1315 float fFXCost_L = fLevel * masterVol;
1316 float fFXCost_R = fLevel * masterVol;
1318 int nBufferPos = nInitialBufferPos;
1319 for (
int i = 0; i < nAvail_bytes; ++i ) {
1321 fVal_L = buffer_L[ nBufferPos ];
1322 fVal_R = buffer_R[ nBufferPos ];
1324 pBuf_L[ nBufferPos ] += fVal_L * fFXCost_L;
1325 pBuf_R[ nBufferPos ] += fVal_R * fFXCost_R;