-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExportSoundThread.cpp
More file actions
140 lines (125 loc) · 4.27 KB
/
ExportSoundThread.cpp
File metadata and controls
140 lines (125 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "pch.h"
#include "ExportSoundThread.h"
#include "fmod.hpp"
#pragma comment(lib, "fmod_vc.lib")
#pragma comment(lib, "fmodL_vc.lib")
IMPLEMENT_DYNCREATE(CExportSoundThread, CWinThread)
using namespace FMOD;
CExportSoundThread::CExportSoundThread() {
}
CExportSoundThread::CExportSoundThread(CWnd* pParent, CString selectedPath, CString newPath, int styleIndex, int sampleRate)
: m_pParent(pParent)
, m_selectedPath(selectedPath)
, m_newPath(newPath)
, m_styleIndex(styleIndex)
, m_sampleRate(sampleRate)
{
}
CExportSoundThread::~CExportSoundThread()
{
}
char* CStrW2CStrA(const CStringW& cstrSrcW)
{
int len = WideCharToMultiByte(CP_ACP, 0, LPCWSTR(cstrSrcW), -1, NULL, 0, NULL, NULL);
char* str = new char[len];
memset(str, 0, len);
WideCharToMultiByte(CP_ACP, 0, LPCWSTR(cstrSrcW), -1, str, len, NULL, NULL);
return str;
}
bool CExportSoundThread::ExportSound(const char* instr, const char* outstr, int type, int sampleRate) {
System* system;
Sound* sound;
Channel* channel;
DSP* dsp = nullptr;
FMOD_RESULT result;
bool isPlay = true;
float frequency;
result = System_Create(&system);
if (result != FMOD_OK) return false;
result = system->setSoftwareFormat(sampleRate, FMOD_SPEAKERMODE_MONO, 0);
if (result != FMOD_OK) return false;
result = system->setOutput(FMOD_OUTPUTTYPE_WAVWRITER);
if (result != FMOD_OK) return false;
result = system->init(32, FMOD_INIT_NORMAL | FMOD_INIT_PROFILE_ENABLE, const_cast<char*>(outstr));
if (result != FMOD_OK) return false;
try {
result = system->createSound(instr, FMOD_DEFAULT, nullptr, &sound);
if (result != FMOD_OK) return false;
result = system->playSound(sound, nullptr, false, &channel);
if (result != FMOD_OK) return false;
switch (type) {
case 0: // lolita
system->createDSPByType(FMOD_DSP_TYPE_NORMALIZE, &dsp);
channel->getFrequency(&frequency);
frequency *= 1.6f;
channel->setFrequency(frequency);
break;
case 1: // uncle
system->createDSPByType(FMOD_DSP_TYPE_PITCHSHIFT, &dsp);
dsp->setParameterFloat(FMOD_DSP_PITCHSHIFT_PITCH, 0.89f);
channel->addDSP(0, dsp);
break;
case 2: // thriller
system->createDSPByType(FMOD_DSP_TYPE_PITCHSHIFT, &dsp);
dsp->setParameterFloat(FMOD_DSP_PITCHSHIFT_PITCH, 1.8f);
channel->addDSP(0, dsp);
break;
case 3: // funny
system->createDSPByType(FMOD_DSP_TYPE_ECHO, &dsp);
dsp->setParameterFloat(FMOD_DSP_ECHO_DELAY, 50);
dsp->setParameterFloat(FMOD_DSP_ECHO_FEEDBACK, 60);
channel->addDSP(0, dsp);
break;
case 4: // ethereal
system->createDSPByType(FMOD_DSP_TYPE_ECHO, &dsp);
dsp->setParameterFloat(FMOD_DSP_ECHO_DELAY, 300);
dsp->setParameterFloat(FMOD_DSP_ECHO_FEEDBACK, 20);
channel->addDSP(0, dsp);
break;
case 5: // chorus
system->createDSPByType(FMOD_DSP_TYPE_ECHO, &dsp);
dsp->setParameterFloat(FMOD_DSP_ECHO_DELAY, 100);
dsp->setParameterFloat(FMOD_DSP_ECHO_FEEDBACK, 50);
channel->addDSP(0, dsp);
break;
case 6: // tremolo
system->createDSPByType(FMOD_DSP_TYPE_TREMOLO, &dsp);
dsp->setParameterFloat(FMOD_DSP_TREMOLO_SKEW, 0.8f);
channel->addDSP(0, dsp);
break;
default:
return false;
}
}
catch (...) {
sound->release();
system->close();
system->release();
return false;
}
system->update();
while (isPlay) {
Sleep(500);
channel->isPlaying(&isPlay);
}
sound->release();
system->close();
system->release();
ExitInstance();
return true;
}
BOOL CExportSoundThread::InitInstance()
{
ExportSound(CStrW2CStrA(m_selectedPath), CStrW2CStrA(m_newPath), m_styleIndex, m_sampleRate);
return TRUE;
}
int CExportSoundThread::ExitInstance()
{
if (m_pParent->GetSafeHwnd() && m_pParent->IsWindowVisible())
{
m_pParent->PostMessage(WM_EXPORT_SOUND_THREAD_FINISHED);
}
return CWinThread::ExitInstance();
}
BEGIN_MESSAGE_MAP(CExportSoundThread, CWinThread)
END_MESSAGE_MAP()