@@ -22,6 +22,7 @@ AudioService::AudioService(MainWindow* pMainWindow)
2222 this ->pMainWindow = pMainWindow;
2323 pSystem = nullptr ;
2424 pRndGen = new std::mt19937_64 (time (nullptr ));
25+ iCurrentlyDrawingTrackIndex = new size_t (0 );
2526
2627 bMonitorTracks = false ;
2728 bIsSomeTrackPlaying = false ;
@@ -377,8 +378,17 @@ void AudioService::playTrack(size_t iTrackIndex, bool bDontLockMutex)
377378 {
378379 if ( ((bCurrentTrackPaused == false ) && (bIsSomeTrackPlaying == false )) || (iTrackIndex != iCurrentlyPlayingTrackIndex) )
379380 {
381+ if (bDrawing)
382+ {
383+ // Some thread is drawing, stop it
384+ bDrawing = false ;
385+ mtxDrawGraph.lock ();
386+ mtxDrawGraph.unlock ();
387+ }
388+
380389 // Add new graph
381- std::thread drawGraphThread (&AudioService::drawGraph, this , iTrackIndex);
390+ *iCurrentlyDrawingTrackIndex = iTrackIndex;
391+ std::thread drawGraphThread (&AudioService::drawGraph, this , iCurrentlyDrawingTrackIndex);
382392 drawGraphThread.detach ();
383393 }
384394
@@ -562,15 +572,7 @@ void AudioService::removeTrack(size_t iTrackIndex)
562572
563573 if ( iTrackIndex < tracks.size () )
564574 {
565- if ((bIsSomeTrackPlaying || bCurrentTrackPaused) && (iCurrentlyPlayingTrackIndex == iTrackIndex))
566- {
567- bDrawing = false ;
568- mtxDrawGraph.lock ();
569- mtxDrawGraph.unlock ();
570- }
571-
572- delete tracks[iTrackIndex];
573- tracks.erase ( tracks.begin () + iTrackIndex );
575+ mtxGetCurrentDrawingIndex.lock ();
574576
575577 if ( tracks.size () == 0 )
576578 {
@@ -584,6 +586,12 @@ void AudioService::removeTrack(size_t iTrackIndex)
584586 {
585587 if (iTrackIndex == iCurrentlyPlayingTrackIndex)
586588 {
589+ mtxGetCurrentDrawingIndex.unlock ();
590+ bDrawing = false ;
591+ mtxDrawGraph.lock ();
592+ mtxDrawGraph.unlock ();
593+ mtxGetCurrentDrawingIndex.lock ();
594+
587595 bIsSomeTrackPlaying = false ;
588596 bCurrentTrackPaused = false ;
589597 iCurrentlyPlayingTrackIndex = 0 ;
@@ -597,8 +605,14 @@ void AudioService::removeTrack(size_t iTrackIndex)
597605 {
598606 // Move 'iCurrentlyPlayingTrackIndex' to the left in the 'tracks' vector because of the delete.
599607 iCurrentlyPlayingTrackIndex--;
608+ *iCurrentlyDrawingTrackIndex -= 1 ;
600609 }
601610 }
611+
612+ delete tracks[iTrackIndex];
613+ tracks.erase ( tracks.begin () + iTrackIndex );
614+
615+ mtxGetCurrentDrawingIndex.unlock ();
602616 }
603617 else
604618 {
@@ -890,6 +904,8 @@ void AudioService::moveDown(size_t iTrackIndex)
890904{
891905 mtxTracksVec.lock ();
892906
907+ mtxGetCurrentDrawingIndex.lock ();
908+
893909 if (iTrackIndex == tracks.size () - 1 )
894910 {
895911 Track* pTemp = tracks[iTrackIndex];
@@ -901,10 +917,12 @@ void AudioService::moveDown(size_t iTrackIndex)
901917 if (iTrackIndex == iCurrentlyPlayingTrackIndex)
902918 {
903919 iCurrentlyPlayingTrackIndex = 0 ;
920+ *iCurrentlyDrawingTrackIndex = 0 ;
904921 }
905922 else
906923 {
907924 iCurrentlyPlayingTrackIndex++;
925+ *iCurrentlyDrawingTrackIndex += 1 ;
908926 }
909927 }
910928 }
@@ -919,24 +937,30 @@ void AudioService::moveDown(size_t iTrackIndex)
919937 if (iTrackIndex == iCurrentlyPlayingTrackIndex)
920938 {
921939 iCurrentlyPlayingTrackIndex++;
940+ *iCurrentlyDrawingTrackIndex += 1 ;
922941 }
923942 else if (iCurrentlyPlayingTrackIndex != 0 )
924943 {
925944 if (iTrackIndex == iCurrentlyPlayingTrackIndex - 1 )
926945 {
927946 iCurrentlyPlayingTrackIndex--;
947+ *iCurrentlyDrawingTrackIndex -= 1 ;
928948 }
929949 }
930950 }
931951 }
932952
953+ mtxGetCurrentDrawingIndex.unlock ();
954+
933955 mtxTracksVec.unlock ();
934956}
935957
936958void AudioService::moveUp (size_t iTrackIndex)
937959{
938960 mtxTracksVec.lock ();
939961
962+ mtxGetCurrentDrawingIndex.lock ();
963+
940964 if (iTrackIndex == 0 )
941965 {
942966 Track* pTemp = tracks[iTrackIndex];
@@ -948,10 +972,12 @@ void AudioService::moveUp(size_t iTrackIndex)
948972 if (iTrackIndex == iCurrentlyPlayingTrackIndex)
949973 {
950974 iCurrentlyPlayingTrackIndex = tracks.size () - 1 ;
975+ *iCurrentlyDrawingTrackIndex = tracks.size () - 1 ;
951976 }
952977 else
953978 {
954979 iCurrentlyPlayingTrackIndex--;
980+ *iCurrentlyDrawingTrackIndex -= 1 ;
955981 }
956982 }
957983 }
@@ -966,14 +992,18 @@ void AudioService::moveUp(size_t iTrackIndex)
966992 if (iTrackIndex == iCurrentlyPlayingTrackIndex)
967993 {
968994 iCurrentlyPlayingTrackIndex--;
995+ *iCurrentlyDrawingTrackIndex -= 1 ;
969996 }
970997 else if (iTrackIndex == iCurrentlyPlayingTrackIndex + 1 )
971998 {
972999 iCurrentlyPlayingTrackIndex++;
1000+ *iCurrentlyDrawingTrackIndex += 1 ;
9731001 }
9741002 }
9751003 }
9761004
1005+ mtxGetCurrentDrawingIndex.unlock ();
1006+
9771007 mtxTracksVec.unlock ();
9781008}
9791009
@@ -1073,14 +1103,8 @@ void AudioService::monitorTrack()
10731103 }
10741104}
10751105
1076- void AudioService::drawGraph (size_t iTrackIndex)
1106+ void AudioService::drawGraph (size_t * iTrackIndex)
10771107{
1078- if (bDrawing)
1079- {
1080- // Some thread is drawing, stop it
1081- bDrawing = false ;
1082- }
1083-
10841108 mtxDrawGraph.lock ();
10851109 bDrawing = true ;
10861110
@@ -1097,14 +1121,16 @@ void AudioService::drawGraph(size_t iTrackIndex)
10971121 // x ('iSamplesInOne') - track length (in sec.)
10981122
10991123 // here we do: 3000 * (tracks[iTrackIndex]->getLengthInMS() / 1000) / 6000, but we can replace this with just:
1100- iSamplesInOne = tracks[iTrackIndex]->getLengthInMS () * 3 / 6000 ;
1124+ mtxGetCurrentDrawingIndex.lock ();
1125+ iSamplesInOne = tracks[*iTrackIndex]->getLengthInMS () * 3 / 6000 ;
11011126 if (iSamplesInOne == 0 ) iSamplesInOne = 1 ;
11021127
1103- unsigned int iTempMax = tracks[iTrackIndex]->getLengthInPCMbytes () / 4 / iSamplesInOne;
1128+ unsigned int iTempMax = tracks[* iTrackIndex]->getLengthInPCMbytes () / 4 / iSamplesInOne;
11041129 pMainWindow->setXMaxToGraph (iTempMax);
1105- tracks[iTrackIndex]->setMaxPosInGraph (iTempMax);
1130+ tracks[* iTrackIndex]->setMaxPosInGraph (iTempMax);
11061131
1107- tracks[iTrackIndex]->createDummySound ();
1132+ tracks[*iTrackIndex]->createDummySound ();
1133+ mtxGetCurrentDrawingIndex.unlock ();
11081134
11091135 // 2 MB because you need to multiply this value by 2 (because of short int type of buffer).
11101136 unsigned int iBufferSize = 1048576 ;
@@ -1115,7 +1141,10 @@ void AudioService::drawGraph(size_t iTrackIndex)
11151141 {
11161142 short int * pSamples = new short int [iBufferSize];
11171143 unsigned int iActuallyRead;
1118- result = tracks[iTrackIndex]->getPCMSamples (pSamples, iBufferSize * 2 , &iActuallyRead);
1144+
1145+ mtxGetCurrentDrawingIndex.lock ();
1146+ result = tracks[*iTrackIndex]->getPCMSamples (pSamples, iBufferSize * 2 , &iActuallyRead);
1147+ mtxGetCurrentDrawingIndex.unlock ();
11191148
11201149 if (result == 0 )
11211150 {
@@ -1131,14 +1160,17 @@ void AudioService::drawGraph(size_t iTrackIndex)
11311160
11321161 }while ( (result == 1 ) && (bDrawing) );
11331162
1163+ mtxGetCurrentDrawingIndex.lock ();
1164+
11341165 if (bDrawing)
11351166 {
11361167 pMainWindow->setXMaxToGraph (iGraphMax);
1137- tracks[iTrackIndex]->setMaxPosInGraph (iGraphMax);
1168+ tracks[* iTrackIndex]->setMaxPosInGraph (iGraphMax);
11381169 }
11391170
1140- tracks[iTrackIndex]->releaseDummySound ();
1171+ tracks[* iTrackIndex]->releaseDummySound ();
11411172
1173+ mtxGetCurrentDrawingIndex.unlock ();
11421174
11431175 bDrawing = false ;
11441176 mtxDrawGraph.unlock ();
@@ -1166,6 +1198,8 @@ AudioService::~AudioService()
11661198 mtxDrawGraph.lock ();
11671199 mtxDrawGraph.unlock ();
11681200
1201+ delete iCurrentlyDrawingTrackIndex;
1202+
11691203 delete pRndGen;
11701204
11711205 bMonitorTracks = false ;
0 commit comments