@@ -2938,99 +2938,29 @@ void CCtrlSamples::OnSustainPointsChanged()
29382938}
29392939
29402940
2941- #define SMPLOOP_ACCURACY 7 // 5%
2942- #define BIDILOOP_ACCURACY 2 // 5%
2943-
2944-
2945- bool MPT_LoopCheck (int sstart0, int sstart1, int send0, int send1)
2946- {
2947- int dse0 = send0 - sstart0;
2948- if ((dse0 < -SMPLOOP_ACCURACY) || (dse0 > SMPLOOP_ACCURACY)) return false ;
2949- int dse1 = send1 - sstart1;
2950- if ((dse1 < -SMPLOOP_ACCURACY) || (dse1 > SMPLOOP_ACCURACY)) return false ;
2951- int dstart = sstart1 - sstart0;
2952- int dend = send1 - send0;
2953- if (!dstart) dstart = dend >> 7 ;
2954- if (!dend) dend = dstart >> 7 ;
2955- if ((dstart ^ dend) < 0 ) return false ;
2956- int delta = dend - dstart;
2957- return ((delta > -SMPLOOP_ACCURACY) && (delta < SMPLOOP_ACCURACY));
2958- }
2959-
2960-
2961- bool MPT_BidiEndCheck (int spos0, int spos1, int spos2)
2962- {
2963- int delta0 = spos1 - spos0;
2964- int delta1 = spos2 - spos1;
2965- int delta2 = spos2 - spos0;
2966- if (!delta0) delta0 = delta1 >> 7 ;
2967- if (!delta1) delta1 = delta0 >> 7 ;
2968- if ((delta1 ^ delta0) < 0 ) return false ;
2969- return ((delta0 >= -1 ) && (delta0 <= 0 ) && (delta1 >= -1 ) && (delta1 <= 0 ) && (delta2 >= -1 ) && (delta2 <= 0 ));
2970- }
2971-
2972-
2973- bool MPT_BidiStartCheck (int spos0, int spos1, int spos2)
2974- {
2975- int delta1 = spos1 - spos0;
2976- int delta0 = spos2 - spos1;
2977- int delta2 = spos2 - spos0;
2978- if (!delta0) delta0 = delta1 >> 7 ;
2979- if (!delta1) delta1 = delta0 >> 7 ;
2980- if ((delta1 ^ delta0) < 0 ) return false ;
2981- return ((delta0 >= -1 ) && (delta0 <= 0 ) && (delta1 > -1 ) && (delta1 <= 0 ) && (delta2 >= -1 ) && (delta2 <= 0 ));
2982- }
2983-
2984-
2985-
29862941void CCtrlSamples::OnVScroll (UINT nCode, UINT, CScrollBar *scrollBar)
29872942{
2988- TCHAR s[256 ];
2943+ TCHAR s[32 ];
29892944 if (IsLocked ()) return ;
29902945 ModSample &sample = m_sndFile.GetSample (m_nSample);
2991- const uint8 *pSample = mpt::byte_cast<const uint8 *>(sample.sampleb ());
2992- const uint32 inc = sample.GetBytesPerSample ();
2993- SmpLength i;
2994- int pos;
2946+ const bool moveLoop = CMainFrame::GetInputHandler ()->CtrlPressed ();
29952947 bool redraw = false ;
29962948 static CScrollBar *lastScrollbar = nullptr ;
29972949
29982950 LockControls ();
2999- if ((!sample.nLength ) || (!pSample)) goto NoSample;
3000- if (sample.uFlags [CHN_16BIT])
3001- {
3002- pSample++;
3003- }
30042951 // Loop Start
3005- if (( pos = m_SpinLoopStart.GetPos32 ()) != 0 && sample.nLoopEnd > 0 )
2952+ if ( int pos = m_SpinLoopStart.GetPos32 (); pos != 0 && sample.nLoopEnd > 0 && sample. HasSampleData () )
30062953 {
3007- bool bOk = false ;
3008- const uint8 *p = pSample + sample.nLoopStart * inc;
3009- int find0 = (int )pSample[sample.nLoopEnd *inc-inc];
3010- int find1 = (int )pSample[sample.nLoopEnd *inc];
3011- // Find Next LoopStart Point
3012- if (pos > 0 )
3013- {
3014- for (i = sample.nLoopStart + 1 ; i + 16 < sample.nLoopEnd ; i++)
3015- {
3016- p += inc;
3017- bOk = sample.uFlags [CHN_PINGPONGLOOP] ? MPT_BidiStartCheck (p[0 ], p[inc], p[inc*2 ]) : MPT_LoopCheck (find0, find1, p[0 ], p[inc]);
3018- if (bOk) break ;
3019- }
3020- } else
3021- // Find Prev LoopStart Point
2954+ if (SmpLength i = SampleEdit::FindLoopStart (sample, false , pos > 0 , moveLoop); i < sample.nLength )
30222955 {
3023- for (i = sample.nLoopStart ; i; )
2956+ if (!m_startedEdit && lastScrollbar != scrollBar)
2957+ PrepareUndo (moveLoop ? " Move Loop" : " Set Loop Start" );
2958+ if (moveLoop)
30242959 {
3025- i--;
3026- p -= inc;
3027- bOk = sample.uFlags [CHN_PINGPONGLOOP] ? MPT_BidiStartCheck (p[0 ], p[inc], p[inc*2 ]) : MPT_LoopCheck (find0, find1, p[0 ], p[inc]);
3028- if (bOk) break ;
2960+ sample.nLoopEnd = i + (sample.nLoopEnd - sample.nLoopStart );
2961+ wsprintf (s, _T (" %u" ), sample.nLoopEnd );
2962+ m_EditLoopEnd.SetWindowText (s);
30292963 }
3030- }
3031- if (bOk)
3032- {
3033- if (!m_startedEdit && lastScrollbar != scrollBar) PrepareUndo (" Set Loop Start" );
30342964 sample.nLoopStart = i;
30352965 wsprintf (s, _T (" %u" ), sample.nLoopStart );
30362966 m_EditLoopStart.SetWindowText (s);
@@ -3040,34 +2970,18 @@ void CCtrlSamples::OnVScroll(UINT nCode, UINT, CScrollBar *scrollBar)
30402970 m_SpinLoopStart.SetPos (0 );
30412971 }
30422972 // Loop End
3043- if (( pos = m_SpinLoopEnd.GetPos32 ()) != 0 )
2973+ if ( int pos = m_SpinLoopEnd.GetPos32 (); pos != 0 && sample. HasSampleData () )
30442974 {
3045- bool bOk = false ;
3046- const uint8 *p = pSample + sample.nLoopEnd * inc;
3047- int find0 = (int )pSample[sample.nLoopStart *inc];
3048- int find1 = (int )pSample[sample.nLoopStart *inc+inc];
3049- // Find Next LoopEnd Point
3050- if (pos > 0 )
3051- {
3052- for (i = sample.nLoopEnd + 1 ; i <= sample.nLength ; i++, p += inc)
3053- {
3054- bOk = sample.uFlags [CHN_PINGPONGLOOP] ? MPT_BidiEndCheck (p[0 ], p[inc], p[inc*2 ]) : MPT_LoopCheck (find0, find1, p[0 ], p[inc]);
3055- if (bOk) break ;
3056- }
3057- } else
3058- // Find Prev LoopEnd Point
2975+ if (SmpLength i = SampleEdit::FindLoopEnd (sample, false , pos > 0 , moveLoop); i > 0 )
30592976 {
3060- for (i = sample.nLoopEnd ; i > sample.nLoopStart + 16 ; )
2977+ if (!m_startedEdit && lastScrollbar != scrollBar)
2978+ PrepareUndo (moveLoop ? " Move Loop" : " Set Loop End" );
2979+ if (moveLoop)
30612980 {
3062- i--;
3063- p -= inc;
3064- bOk = sample.uFlags [CHN_PINGPONGLOOP] ? MPT_BidiEndCheck (p[0 ], p[inc], p[inc*2 ]) : MPT_LoopCheck (find0, find1, p[0 ], p[inc]);
3065- if (bOk) break ;
2981+ sample.nLoopStart = i - (sample.nLoopEnd - sample.nLoopStart );
2982+ wsprintf (s, _T (" %u" ), sample.nLoopStart );
2983+ m_EditLoopStart.SetWindowText (s);
30662984 }
3067- }
3068- if (bOk)
3069- {
3070- if (!m_startedEdit && lastScrollbar != scrollBar) PrepareUndo (" Set Loop End" );
30712985 sample.nLoopEnd = i;
30722986 wsprintf (s, _T (" %u" ), sample.nLoopEnd );
30732987 m_EditLoopEnd.SetWindowText (s);
@@ -3077,35 +2991,18 @@ void CCtrlSamples::OnVScroll(UINT nCode, UINT, CScrollBar *scrollBar)
30772991 m_SpinLoopEnd.SetPos (0 );
30782992 }
30792993 // Sustain Loop Start
3080- if (( pos = m_SpinSustainStart.GetPos32 ()) != 0 && sample.nSustainEnd > 0 )
2994+ if ( int pos = m_SpinSustainStart.GetPos32 (); pos != 0 && sample.nSustainEnd > 0 && sample. HasSampleData () )
30812995 {
3082- bool bOk = false ;
3083- const uint8 *p = pSample + sample.nSustainStart * inc;
3084- int find0 = (int )pSample[sample.nSustainEnd *inc-inc];
3085- int find1 = (int )pSample[sample.nSustainEnd *inc];
3086- // Find Next Sustain LoopStart Point
3087- if (pos > 0 )
3088- {
3089- for (i = sample.nSustainStart + 1 ; i + 16 < sample.nSustainEnd ; i++)
3090- {
3091- p += inc;
3092- bOk = sample.uFlags [CHN_PINGPONGSUSTAIN] ? MPT_BidiStartCheck (p[0 ], p[inc], p[inc*2 ]) : MPT_LoopCheck (find0, find1, p[0 ], p[inc]);
3093- if (bOk) break ;
3094- }
3095- } else
3096- // Find Prev Sustain LoopStart Point
2996+ if (SmpLength i = SampleEdit::FindLoopStart (sample, true , pos > 0 , moveLoop); i < sample.nLength )
30972997 {
3098- for (i = sample.nSustainStart ; i; )
2998+ if (!m_startedEdit && lastScrollbar != scrollBar)
2999+ PrepareUndo (moveLoop ? " Move Sustain Loop" : " Set Sustain Loop Start" );
3000+ if (moveLoop)
30993001 {
3100- i--;
3101- p -= inc;
3102- bOk = sample.uFlags [CHN_PINGPONGSUSTAIN] ? MPT_BidiStartCheck (p[0 ], p[inc], p[inc*2 ]) : MPT_LoopCheck (find0, find1, p[0 ], p[inc]);
3103- if (bOk) break ;
3002+ sample.nSustainEnd = i + (sample.nSustainEnd - sample.nSustainStart );
3003+ wsprintf (s, _T (" %u" ), sample.nSustainEnd );
3004+ m_EditSustainEnd.SetWindowText (s);
31043005 }
3105- }
3106- if (bOk)
3107- {
3108- if (!m_startedEdit && lastScrollbar != scrollBar) PrepareUndo (" Set Sustain Loop Start" );
31093006 sample.nSustainStart = i;
31103007 wsprintf (s, _T (" %u" ), sample.nSustainStart );
31113008 m_EditSustainStart.SetWindowText (s);
@@ -3115,34 +3012,18 @@ void CCtrlSamples::OnVScroll(UINT nCode, UINT, CScrollBar *scrollBar)
31153012 m_SpinSustainStart.SetPos (0 );
31163013 }
31173014 // Sustain Loop End
3118- if (( pos = m_SpinSustainEnd.GetPos32 ()) != 0 )
3015+ if ( int pos = m_SpinSustainEnd.GetPos32 (); pos != 0 && sample. HasSampleData () )
31193016 {
3120- bool bOk = false ;
3121- const uint8 *p = pSample + sample.nSustainEnd * inc;
3122- int find0 = (int )pSample[sample.nSustainStart *inc];
3123- int find1 = (int )pSample[sample.nSustainStart *inc+inc];
3124- // Find Next LoopEnd Point
3125- if (pos > 0 )
3126- {
3127- for (i = sample.nSustainEnd + 1 ; i + 1 < sample.nLength ; i++, p += inc)
3128- {
3129- bOk = sample.uFlags [CHN_PINGPONGSUSTAIN] ? MPT_BidiEndCheck (p[0 ], p[inc], p[inc*2 ]) : MPT_LoopCheck (find0, find1, p[0 ], p[inc]);
3130- if (bOk) break ;
3131- }
3132- } else
3133- // Find Prev LoopEnd Point
3017+ if (SmpLength i = SampleEdit::FindLoopEnd (sample, true , pos > 0 , moveLoop); i > 0 )
31343018 {
3135- for (i = sample.nSustainEnd ; i > sample.nSustainStart + 16 ; )
3019+ if (!m_startedEdit && lastScrollbar != scrollBar)
3020+ PrepareUndo (moveLoop ? " Move Sustain Loop" : " Set Sustain Loop End" );
3021+ if (moveLoop)
31363022 {
3137- i--;
3138- p -= inc;
3139- bOk = sample.uFlags [CHN_PINGPONGSUSTAIN] ? MPT_BidiEndCheck (p[0 ], p[inc], p[inc*2 ]) : MPT_LoopCheck (find0, find1, p[0 ], p[inc]);
3140- if (bOk) break ;
3023+ sample.nSustainStart = i - (sample.nSustainEnd - sample.nSustainStart );
3024+ wsprintf (s, _T (" %u" ), sample.nSustainStart );
3025+ m_EditSustainStart.SetWindowText (s);
31413026 }
3142- }
3143- if (bOk)
3144- {
3145- if (!m_startedEdit && lastScrollbar != scrollBar) PrepareUndo (" Set Sustain Loop End" );
31463027 sample.nSustainEnd = i;
31473028 wsprintf (s, _T (" %u" ), sample.nSustainEnd );
31483029 m_EditSustainEnd.SetWindowText (s);
@@ -3151,9 +3032,8 @@ void CCtrlSamples::OnVScroll(UINT nCode, UINT, CScrollBar *scrollBar)
31513032 }
31523033 m_SpinSustainEnd.SetPos (0 );
31533034 }
3154- NoSample:
31553035 // FineTune / C-5 Speed
3156- if (( pos = m_SpinFineTune.GetPos32 ()) != 0 )
3036+ if ( int pos = m_SpinFineTune.GetPos32 (); pos != 0 )
31573037 {
31583038 if (!m_startedEdit && lastScrollbar != scrollBar)
31593039 PrepareUndo (" Finetune" );
@@ -3366,8 +3246,7 @@ void CCtrlSamples::OnXFade()
33663246 CSampleXFadeDlg dlg (this , sample);
33673247 if (dlg.DoModal () == IDOK)
33683248 {
3369- const SmpLength loopStart = dlg.m_useSustainLoop ? sample.nSustainStart : sample.nLoopStart ;
3370- const SmpLength loopEnd = dlg.m_useSustainLoop ? sample.nSustainEnd : sample.nLoopEnd ;
3249+ const auto [loopStart, loopEnd] = dlg.m_useSustainLoop ? sample.GetSustainLoop () : sample.GetLoop ();
33713250 const SmpLength maxSamples = std::min ({ sample.nLength , loopStart, loopEnd / 2 });
33723251 SmpLength fadeSamples = dlg.PercentToSamples (dlg.m_fadeLength );
33733252 LimitMax (fadeSamples, maxSamples);
0 commit comments