This commit is contained in:
Maksymilian Wilczyński committed 2025-05-10 21:31:35 +02:00
1 parent 48dc9ec180
commit c44757430d
2 files changed
+22 -5

No files matched your search

Binary file not shown.
+22 -5
View File
@@ -3,6 +3,8 @@
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <cstring> #include <cstring>
#include <iomanip>
#include <chrono>
void WriteWAVHeader(std::ofstream &file, DWORD length) { void WriteWAVHeader(std::ofstream &file, DWORD length) {
DWORD dataChunkSize = length; DWORD dataChunkSize = length;
@@ -34,7 +36,13 @@ void WriteWAVHeader(std::ofstream &file, DWORD length) {
file.write(reinterpret_cast<const char*>(&dataChunkSize), 4); file.write(reinterpret_cast<const char*>(&dataChunkSize), 4);
} }
int main() { int main(int argc, char** argv) {
std::cout << "Program start\n";
if (argc != 2) {
std::cerr << "Wrong arguments!\n";
return -1;
}
// 1) Initialize BASS: default device, 44100 Hz, no flags // 1) Initialize BASS: default device, 44100 Hz, no flags
if (!BASS_Init(-1, 44100, 0, NULL, NULL)) { if (!BASS_Init(-1, 44100, 0, NULL, NULL)) {
std::cerr << "BASS_Init error: " << BASS_ErrorGetCode() << std::endl; std::cerr << "BASS_Init error: " << BASS_ErrorGetCode() << std::endl;
@@ -50,7 +58,7 @@ int main() {
} }
// Create decoding stream (no playback) // Create decoding stream (no playback)
HSTREAM midiStream = BASS_MIDI_StreamCreateFile(FALSE, "Rapapara_POL_07753.mid", 0, 0, HSTREAM midiStream = BASS_MIDI_StreamCreateFile(FALSE, argv[1], 0, 0,
BASS_STREAM_DECODE | BASS_SAMPLE_FLOAT, 44100); BASS_STREAM_DECODE | BASS_SAMPLE_FLOAT, 44100);
if (!midiStream) { if (!midiStream) {
std::cerr << "BASS_MIDI_StreamCreateFile error: " << BASS_ErrorGetCode() << std::endl; std::cerr << "BASS_MIDI_StreamCreateFile error: " << BASS_ErrorGetCode() << std::endl;
@@ -81,11 +89,16 @@ int main() {
const int bufferSize = 44100 * 2 * sizeof(float); // 1 second float stereo const int bufferSize = 44100 * 2 * sizeof(float); // 1 second float stereo
float buffer[44100 * 2]; 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) { while (true) {
int bytes = BASS_ChannelGetData(midiStream, buffer, bufferSize); int bytes = BASS_ChannelGetData(midiStream, buffer, bufferSize);
if (bytes <= 0) break; if (bytes <= 0) break;
// Convert float samples to 16-bit PCM
short pcm[44100 * 2]; short pcm[44100 * 2];
for (int i = 0; i < bytes / 4; ++i) { for (int i = 0; i < bytes / 4; ++i) {
float sample = buffer[i] * 32767.0f; float sample = buffer[i] * 32767.0f;
@@ -94,14 +107,18 @@ int main() {
pcm[i] = static_cast<short>(sample); pcm[i] = static_cast<short>(sample);
} }
wav.write(reinterpret_cast<const char*>(pcm), bytes / 2); // float->16bit halves size 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); wav.seekp(0);
WriteWAVHeader(wav, totalBytes); WriteWAVHeader(wav, totalBytes);
wav.close(); wav.close();
std::cout << "\nSynthesis end\n";
std::cout << "Exported to output.wav" << std::endl; std::cout << "Exported to output.wav" << std::endl;
// 6) Clean up // 6) Clean up