Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
Seems like a rare crash that may occur after a track(s) was loaded ha…
Browse files Browse the repository at this point in the history
…s been fixed.
  • Loading branch information
Flone-dnb committed Nov 1, 2019
1 parent 361e0c4 commit 9a844ae
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 38 deletions.
4 changes: 1 addition & 3 deletions TODO.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
- Speed up the process of adding the tracks. Add only paths and when play() called create FMOD object.
- Speed up the process of adding the tracks.
- When adding music, we need to add it in the same order in which it was in the folder.
- Change random to random_device?
- There is a rare bug that causes a crash right after you open one track:
vector subscript out of range exception
- Do something with color banding.
- Seems like with 'speed (by pitch)' effect we also need to increase mid-high frequencies on like 3-4 dB and maybe do the same thing with the low and the mid-low.
- Add bounds in which track will repeat by clicking RMB on the oscillogram. Track's volume in the transition from end bound to start bound will change?
Expand Down
2 changes: 1 addition & 1 deletion ide/BloodyPlayer.pro.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.10.1, 2019-11-01T14:51:44. -->
<!-- Written by QtCreator 4.10.1, 2019-11-02T00:13:12. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down
Binary file modified screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 35 additions & 21 deletions src/Model/AudioService/audioservice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,17 @@ void AudioService::FMODinit()

void AudioService::addTrack(const wchar_t *pFilePath)
{
Track* pNewTrack = new Track(pMainWindow, pSystem);
if ( !pNewTrack->setTrack(pFilePath) )
Track* pNewTrack = new Track(pFilePath, pMainWindow, pSystem);
if ( !pNewTrack->setupTrack() )
{
delete pNewTrack;
return;
}

std::wstring wPathStr(pFilePath);



// Get track name
size_t iNameStartIndex = 0;
for (size_t i = wPathStr.size() - 1; i >= 1; i--)
Expand All @@ -179,13 +181,16 @@ void AudioService::addTrack(const wchar_t *pFilePath)
break;
}
}

std::wstring trackName = L"";
for (size_t i = iNameStartIndex; i < wPathStr.size() - 4; i++)
{
trackName += wPathStr[i];
}
if (trackName.back() == L'.') trackName.pop_back();



// Get track info
std::wstring trackInfo = L"";
for (size_t i = wPathStr.size() - 4; i < wPathStr.size(); i++)
Expand Down Expand Up @@ -224,12 +229,14 @@ void AudioService::addTrack(const wchar_t *pFilePath)
trackInfo += stream.str();
trackInfo += L" MB";



// Get track time
std::string trackTime = "";
unsigned int iMS = pNewTrack->getLengthInMS();
unsigned int iSeconds = iMS / 1000;
unsigned int iMinutes = iSeconds / 60;
iSeconds -= (iMinutes * 60);
std::string trackTime = "";
trackTime += std::to_string(iMinutes);
trackTime += ":";
if (iSeconds < 10) trackTime += "0";
Expand Down Expand Up @@ -278,12 +285,14 @@ void AudioService::addTracks(std::vector<wchar_t*> paths)
paths.pop_back();
}

pMainWindow->showMessageBox(true, std::to_string(removeCount) + std::string(" tracks were rejected because with them we will exceed maximum"
" amount of tracks in the tracklist which is " + std::to_string(MAX_CHANNELS)));
pMainWindow->showMessageBox(true, "Maximum tracks exceeded." + std::to_string(removeCount) + " tracks have not been added.");
}


pMainWindow->showWaitWindow("Please wait.\nAdding your music.");

pMainWindow->showWaitWindow("Please wait...\n"
"Adding your music.");


// Get amount of CPU threads
// In every CPU thread we will add tracks
Expand All @@ -295,35 +304,38 @@ void AudioService::addTracks(std::vector<wchar_t*> paths)
int* allCount = new int(0);


if (paths.size() >= threads*2)
if (paths.size() >= threads * 2)
{
size_t iPathsOnOneThread = paths.size() / threads;
size_t iCurrentPos = 0;

std::vector<wchar_t*> onThread;
size_t iStart, iStop;
for (size_t i = 0; i < paths.size(); i++)
{
if (iCurrentPos < iPathsOnOneThread)
{
onThread.push_back(paths[i]);
if (iCurrentPos == 0) iStart = i;
iCurrentPos++;
}

if (iCurrentPos == iPathsOnOneThread)
{
iStop = i;

threadsDoneFlags.push_back(new bool(false));
std::thread t (&AudioService::threadAddTracks, this, onThread, threadsDoneFlags.back(), allCount, static_cast<int>(paths.size()));
std::thread t (&AudioService::threadAddTracks, this, &paths, iStart, iStop, threadsDoneFlags.back(), allCount, static_cast<int>(paths.size()));
t.detach();

onThread.clear();
iCurrentPos = 0;
}
}

if (onThread.size() > 0)
if (iCurrentPos > 0)
{
iStop = paths.size() - 1;

threadsDoneFlags.push_back(new bool(false));
std::thread t (&AudioService::threadAddTracks, this, onThread, threadsDoneFlags.back(), allCount, static_cast<int>(paths.size()));
std::thread t (&AudioService::threadAddTracks, this, &paths, iStart, iStop, threadsDoneFlags.back(), allCount, static_cast<int>(paths.size()));
t.detach();
}
}
Expand All @@ -334,6 +346,8 @@ void AudioService::addTracks(std::vector<wchar_t*> paths)
for (size_t i = 0; i < paths.size(); i++)
{
addTrack(paths[i]);

pMainWindow->setProgress( static_cast<int>(100.0f * ( i + 1 ) / paths.size()) );
}

delete allCount;
Expand Down Expand Up @@ -367,7 +381,6 @@ void AudioService::addTracks(std::vector<wchar_t*> paths)

}while(bDone == false);


delete allCount;

for (size_t i = 0; i < threadsDoneFlags.size(); i++)
Expand All @@ -377,11 +390,9 @@ void AudioService::addTracks(std::vector<wchar_t*> paths)

pMainWindow->hideWaitWindow();

// Wait a little for all track widgets to show
std::this_thread::sleep_for(std::chrono::milliseconds(100));
if (tracks.size() > 0) pMainWindow->setFocusOnTrack(tracks.size() - 1);

mtxTracksVec.unlock();

pMainWindow->showAllTracks();
}

void AudioService::playTrack(size_t iTrackIndex, bool bDontLockMutex)
Expand Down Expand Up @@ -1459,15 +1470,18 @@ int AudioService::interpret24bitAsInt32(char byte0, char byte1, char byte2)
return ( (byte0 << 24) | (byte1 << 16) | (byte2 << 8) ) >> 8;
}

void AudioService::threadAddTracks(std::vector<wchar_t*> paths, bool* done, int* allCount, int all)
void AudioService::threadAddTracks(std::vector<wchar_t*>* paths, size_t iStart, size_t iStop, bool* done, int* allCount, int all)
{
for (size_t i = 0; i < paths.size(); i++)
for (size_t i = iStart; i <= iStop; i++)
{
addTrack(paths[i]);
addTrack(paths->operator[](i));


mtxLoadThreadDone.lock();

*allCount += 1;
pMainWindow->setProgress( *allCount * 100 / all);

mtxLoadThreadDone.unlock();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Model/AudioService/audioservice.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class AudioService
void FMODinit ();

// Functions for execution in a separete thread
void threadAddTracks (std::vector<wchar_t*> paths, bool* done, int* allCount, int all);
void threadAddTracks (std::vector<wchar_t*>* paths, size_t iStart, size_t iStop, bool* done, int* allCount, int all);
void addTrack (const wchar_t* pFilePath);

// Will switch to next track if one's ended
Expand Down
8 changes: 3 additions & 5 deletions src/Model/Track/track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

#include <windows.h>

Track::Track(MainWindow *pMainWindow, FMOD::System* pSystem)
Track::Track(const wchar_t* pFilePath, MainWindow *pMainWindow, FMOD::System* pSystem)
{
pChannel = nullptr;
pSound = nullptr;
pFilePath = nullptr;
this->pFilePath = pFilePath;

this->pMainWindow = pMainWindow;
this->pSystem = pSystem;
Expand All @@ -32,7 +32,7 @@ Track::Track(MainWindow *pMainWindow, FMOD::System* pSystem)



bool Track::setTrack(const wchar_t* pFilePath)
bool Track::setupTrack()
{
// This function creates a track (pSound) in the FMOD system.

Expand All @@ -52,8 +52,6 @@ bool Track::setTrack(const wchar_t* pFilePath)
return false;
}

// On this point, we are created the sound and can save path to it.
this->pFilePath = pFilePath;

// Get audio format (mp3, wav, flac, ogg and etc.)
FMOD_SOUND_TYPE type;
Expand Down
4 changes: 2 additions & 2 deletions src/Model/Track/track.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ class Track

public:

Track(MainWindow* pMainWindow, FMOD::System* pSystem);
Track(const wchar_t* pFilePath, MainWindow* pMainWindow, FMOD::System* pSystem);





// Start/stop functions

bool setTrack (const wchar_t* pFilePath);
bool setupTrack ();
bool playTrack (float fVolume);
bool pauseTrack ();
bool stopTrack ();
Expand Down
29 changes: 28 additions & 1 deletion src/View/MainWindow/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

// STL
#include <cmath>
#include <thread>

// Custom
#include "../src/Controller/controller.h"
Expand Down Expand Up @@ -129,6 +130,7 @@ MainWindow::MainWindow(QWidget *parent) :
connect(pFXWindow, &FXWindow::signalChangeEchoVolume, this, &MainWindow::slotSetEchoVolume);
connect(pFXWindow, &FXWindow::signalOpenVST, this, &MainWindow::slotLoadVST);
connect(pFXWindow, &FXWindow::signalShowVST, this, &MainWindow::slotShowVST);
connect(this, &MainWindow::signalShowAllTracks, this, &MainWindow::slotShowAllTracks);
connect(this, &MainWindow::signalResetAll, pFXWindow, &FXWindow::slotResetAll);
connect(this, &MainWindow::signalSetVSTName, pFXWindow, &FXWindow::slotSetVSTName);

Expand Down Expand Up @@ -156,6 +158,11 @@ void MainWindow::addNewTrack(std::wstring trackName, std::wstring trackInfo, std
emit signalAddNewTrack(trackName, trackInfo, trackTime);
}

void MainWindow::showAllTracks()
{
emit signalShowAllTracks();
}

void MainWindow::removePlayingOnTrack(size_t iTrackIndex)
{
tracks[iTrackIndex]->disablePlaying();
Expand Down Expand Up @@ -246,6 +253,9 @@ void MainWindow::setCurrentPos(double x, std::string time)

void MainWindow::setFocusOnTrack(size_t index)
{
// Wait a little for all track widgets to show
std::this_thread::sleep_for(std::chrono::milliseconds(150));

ui->scrollArea->ensureWidgetVisible(tracks[index], 50, 50);
}

Expand Down Expand Up @@ -407,22 +417,39 @@ void MainWindow::slotSetNumber(size_t iNumber)

void MainWindow::slotAddNewTrack(std::wstring trackName, std::wstring trackInfo, std::string trackTime)
{
mtxAddTrackWidget.lock();

TrackWidget* pNewTrack = new TrackWidget( QString::fromStdWString(trackName), QString::fromStdWString(trackInfo), QString::fromStdString(trackTime) );
connect(pNewTrack, &TrackWidget::signalDoubleClick, this, &MainWindow::slotClickedOnTrack);
connect(pNewTrack, &TrackWidget::signalSelected, this, &MainWindow::slotTrackSelected);
connect(pNewTrack, &TrackWidget::signalDelete, this, &MainWindow::deleteSelectedTrack);
connect(pNewTrack, &TrackWidget::signalMoveUp, this, &MainWindow::slotMoveUp);
connect(pNewTrack, &TrackWidget::signalMoveDown, this, &MainWindow::slotMoveDown);

pNewTrack->setVisible(false);
ui->verticalLayout_Tracks->addWidget(pNewTrack);

tracks.push_back(pNewTrack);
pNewTrack->setNumber( tracks.size() );
pNewTrack->show();
//pNewTrack->show();

if (tracks.size() == 1)
{
ui->horizontalSlider->setEnabled(true);
}

mtxAddTrackWidget.unlock();
}

void MainWindow::slotShowAllTracks()
{
for (size_t i = 0; i < tracks.size(); i++)
{
tracks[i]->setVisible(true);
}

std::thread focus(&MainWindow::setFocusOnTrack, this, tracks.size() - 1);
focus.detach();
}

void MainWindow::slotShowWaitWindow(QString text)
Expand Down
7 changes: 7 additions & 0 deletions src/View/MainWindow/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

// STL
#include <string>
#include <mutex>
#include <vector>


Expand Down Expand Up @@ -66,6 +67,7 @@ class MainWindow : public QMainWindow
// Other

void signalAddNewTrack (std::wstring trackName, std::wstring trackInfo, std::string trackTime);
void signalShowAllTracks ();
void signalSetTrack (size_t iTrackIndex, bool bClear = false);
void signalShowMessageBox (bool errorBox, std::string text);
void signalSetNumber (size_t iNumber);
Expand Down Expand Up @@ -117,6 +119,7 @@ class MainWindow : public QMainWindow
// Other

void addNewTrack (std::wstring trackName, std::wstring trackInfo, std::string trackTime);
void showAllTracks ();
void showMessageBox (bool errorBox, std::string text);
void setPlayingOnTrack (size_t iTrackIndex, bool bClear = false);
void removePlayingOnTrack (size_t iTrackIndex);
Expand Down Expand Up @@ -228,6 +231,7 @@ private slots:
// Other

void slotAddNewTrack (std::wstring trackName, std::wstring trackInfo, std::string trackTime);
void slotShowAllTracks ();
void slotSetTrack (size_t iTrackIndex, bool bClear = false);
void slotShowMessageBox (bool errorBox, std::string text);
void slotSetNumber (size_t iNumber);
Expand All @@ -247,6 +251,9 @@ private slots:
QCPItemRect* backgnd;


std::mutex mtxAddTrackWidget;


int iSelectedTrackIndex;


Expand Down
Loading

0 comments on commit 9a844ae

Please sign in to comment.