allow to play OR synth to file
This commit is contained in:
1 parent
d0cd0181a4
commit
f50dd86e75
2 files changed
+57
-43
No files matched your search
@@ -38,10 +38,11 @@ void WriteWAVHeader(std::ofstream &file, DWORD length) {
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
std::cout << "Program start\n";
|
||||
if (argc != 3) {
|
||||
if (argc != 4) {
|
||||
std::cerr << "Wrong arguments!\n";
|
||||
return -1;
|
||||
}
|
||||
bool synthToFile = std::strcmp(argv[3], "true") == 0;
|
||||
|
||||
// 1) Initialize BASS: default device, 44100 Hz, no flags
|
||||
if (!BASS_Init(-1, 44100, 0, NULL, NULL)) {
|
||||
@@ -57,9 +58,12 @@ int main(int argc, char** argv) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Create decoding stream (no playback)
|
||||
HSTREAM midiStream = BASS_MIDI_StreamCreateFile(FALSE, argv[2], 0, 0,
|
||||
BASS_STREAM_DECODE | BASS_SAMPLE_FLOAT | BASS_MIDI_NOFX, 44100);
|
||||
// Create decoding stream
|
||||
DWORD flags = BASS_SAMPLE_FLOAT | BASS_MIDI_NOFX;
|
||||
if (synthToFile)
|
||||
flags |= BASS_STREAM_DECODE;
|
||||
|
||||
HSTREAM midiStream = BASS_MIDI_StreamCreateFile(FALSE, argv[2], 0, 0, flags, 44100);
|
||||
if (!midiStream) {
|
||||
std::cerr << "BASS_MIDI_StreamCreateFile error: " << BASS_ErrorGetCode() << std::endl;
|
||||
BASS_MIDI_FontFree(sfont);
|
||||
@@ -77,50 +81,60 @@ int main(int argc, char** argv) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::ofstream wav("output.wav", std::ios::binary);
|
||||
if (!wav) {
|
||||
std::cerr << "Can't open output.wav for writing" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
WriteWAVHeader(wav, 0);
|
||||
|
||||
DWORD totalBytes = 0;
|
||||
const int bufferSize = 44100 * 2 * sizeof(float); // 1 second float stereo
|
||||
float buffer[44100 * 2];
|
||||
|
||||
std::cout << "Synthesis start\n";
|
||||
|
||||
QWORD totalRead = 0;
|
||||
QWORD totalStreamLength = BASS_ChannelGetLength(midiStream, BASS_POS_BYTE);
|
||||
auto lastUpdate = std::chrono::steady_clock::now();
|
||||
|
||||
while (true) {
|
||||
int bytes = BASS_ChannelGetData(midiStream, buffer, bufferSize);
|
||||
if (bytes <= 0) break;
|
||||
|
||||
short pcm[44100 * 2];
|
||||
for (int i = 0; i < bytes / 4; ++i) {
|
||||
float sample = buffer[i] * 32767.0f;
|
||||
if (sample > 32767.0f) sample = 32767.0f;
|
||||
if (sample < -32768.0f) sample = -32768.0f;
|
||||
pcm[i] = static_cast<short>(sample);
|
||||
if (!synthToFile) {
|
||||
std::cout << "Playing live...\n";
|
||||
if (!BASS_ChannelPlay(midiStream, FALSE)) {
|
||||
std::cerr << "BASS_ChannelPlay error: " << BASS_ErrorGetCode() << std::endl;
|
||||
BASS_StreamFree(midiStream);
|
||||
BASS_MIDI_FontFree(sfont);
|
||||
BASS_Free();
|
||||
return 1;
|
||||
}
|
||||
|
||||
wav.write(reinterpret_cast<const char*>(pcm), bytes / 2);
|
||||
totalRead += bytes;
|
||||
std::cout << "Press Enter to stop...\n";
|
||||
std::cin.get(); // wait for user input
|
||||
} else {
|
||||
std::ofstream wav("output.wav", std::ios::binary);
|
||||
if (!wav) {
|
||||
std::cerr << "Can't open output.wav for writing" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
float percent = (totalStreamLength > 0) ? (100.0f * totalRead / totalStreamLength) : 0.0f;
|
||||
printf("\rProgress: %.2f%%", percent);
|
||||
WriteWAVHeader(wav, 0);
|
||||
DWORD totalBytes = 0;
|
||||
const int bufferSize = 44100 * 2 * sizeof(float);
|
||||
float buffer[44100 * 2];
|
||||
QWORD totalRead = 0;
|
||||
QWORD totalStreamLength = BASS_ChannelGetLength(midiStream, BASS_POS_BYTE);
|
||||
|
||||
std::cout << "Synthesis start\n";
|
||||
while (true) {
|
||||
int bytes = BASS_ChannelGetData(midiStream, buffer, bufferSize);
|
||||
if (bytes <= 0) break;
|
||||
|
||||
short pcm[44100 * 2];
|
||||
for (int i = 0; i < bytes / 4; ++i) {
|
||||
float sample = buffer[i] * 32767.0f;
|
||||
if (sample > 32767.0f) sample = 32767.0f;
|
||||
if (sample < -32768.0f) sample = -32768.0f;
|
||||
pcm[i] = static_cast<short>(sample);
|
||||
}
|
||||
|
||||
wav.write(reinterpret_cast<const char*>(pcm), bytes / 2);
|
||||
totalBytes += bytes / 2;
|
||||
totalRead += bytes;
|
||||
|
||||
float percent = (totalStreamLength > 0) ? (100.0f * totalRead / totalStreamLength) : 0.0f;
|
||||
printf("\rProgress: %.2f%%", percent);
|
||||
}
|
||||
|
||||
wav.seekp(0);
|
||||
WriteWAVHeader(wav, totalBytes);
|
||||
wav.close();
|
||||
std::cout << "\nSynthesis end\n";
|
||||
std::cout << "Exported to output.wav" << std::endl;
|
||||
}
|
||||
|
||||
wav.seekp(0);
|
||||
WriteWAVHeader(wav, totalBytes);
|
||||
wav.close();
|
||||
|
||||
std::cout << "\nSynthesis end\n";
|
||||
std::cout << "Exported to output.wav" << std::endl;
|
||||
|
||||
// 6) Clean up
|
||||
BASS_StreamFree(midiStream); // free stream
|
||||
BASS_MIDI_FontFree(sfont); // unload soundfont
|
||||
|
||||
Reference in new issue
Block a user