gzdoom-gles/libraries/zmusic/mididevices/music_wavewriter_mididevice.cpp
Christoph Oelckers 5743a56ef4 - created a new zmusic library which will eventually contain all the music playback code.
Currently all it contains are the MIDI sources and the MIDI devices, the rest needs to be reworked first.

# Conflicts:
#	libraries/zmusic/i_module.cpp
#	libraries/zmusic/i_module.h
#	src/CMakeLists.txt
#	src/i_module.cpp
#	src/i_module.h
#	src/sound/music/midi_cvars.cpp
#	src/utility/i_module.cpp
#	src/utility/i_module.h

# Conflicts:
#	src/CMakeLists.txt
#	src/sound/musicformats/music_opl.cpp
2020-01-05 11:46:20 +01:00

223 lines
6.9 KiB
C++

/*
** music_wavewriter_mididevice.cpp
** Dumps a MIDI to a wave file by using one of the other software synths.
**
**---------------------------------------------------------------------------
** Copyright 2008 Randy Heit
** Copyright 2018 Christoph Oelckers
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
*/
// HEADER FILES ------------------------------------------------------------
#include "mididevice.h"
#include "zmusic/m_swap.h"
#include <errno.h>
// MACROS ------------------------------------------------------------------
// TYPES -------------------------------------------------------------------
struct FmtChunk
{
//uint32_t ChunkID;
uint32_t ChunkLen;
uint16_t FormatTag;
uint16_t Channels;
uint32_t SamplesPerSec;
uint32_t AvgBytesPerSec;
uint16_t BlockAlign;
uint16_t BitsPerSample;
uint16_t ExtensionSize;
uint16_t ValidBitsPerSample;
uint32_t ChannelMask;
uint32_t SubFormatA;
uint16_t SubFormatB;
uint16_t SubFormatC;
uint8_t SubFormatD[8];
};
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PRIVATE DATA DEFINITIONS ------------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
// CODE --------------------------------------------------------------------
#ifdef _WIN32
// I'd rather not include Windows.h for just this.
extern "C" __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned CodePage, unsigned long dwFlags, const char* lpMultiByteStr, int cbMultiByte, const wchar_t* lpWideCharStr, int cchWideChar);
#endif
static FILE* my_fopen(const char* filename)
{
#ifndef _WIN32
return fopen(filename, "wb");
#else
// This is supposed to handle UTF-8 which Windows does not support with fopen. :(
const int CP_UTF8 = 65001;
std::wstring filePathW;
auto len = strlen(filename);
filePathW.resize(len);
int newSize = MultiByteToWideChar(CP_UTF8, 0, filename, (int)len, const_cast<wchar_t*>(filePathW.c_str()), (int)len);
filePathW.resize(newSize);
return _wfopen(filePathW.c_str(), L"wb");
#endif
}
//==========================================================================
//
// MIDIWaveWriter Constructor
//
//==========================================================================
MIDIWaveWriter::MIDIWaveWriter(const char *filename, SoftSynthMIDIDevice *playdevice)
: SoftSynthMIDIDevice(playdevice->GetSampleRate())
{
File = my_fopen(filename);
playDevice = playdevice;
if (File != nullptr)
{ // Write wave header
FmtChunk fmt;
if (fwrite("RIFF\0\0\0\0WAVEfmt ", 1, 16, File) != 16) goto fail;
playDevice->CalcTickRate();
fmt.ChunkLen = LittleLong(uint32_t(sizeof(fmt) - 4));
fmt.FormatTag = LittleShort((uint16_t)0xFFFE); // WAVE_FORMAT_EXTENSIBLE
fmt.Channels = LittleShort((uint16_t)2);
fmt.SamplesPerSec = LittleLong(SampleRate);
fmt.AvgBytesPerSec = LittleLong(SampleRate * 8);
fmt.BlockAlign = LittleShort((uint16_t)8);
fmt.BitsPerSample = LittleShort((uint16_t)32);
fmt.ExtensionSize = LittleShort((uint16_t)(2 + 4 + 16));
fmt.ValidBitsPerSample = LittleShort((uint16_t)32);
fmt.ChannelMask = LittleLong(3);
fmt.SubFormatA = LittleLong(0x00000003); // Set subformat to KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
fmt.SubFormatB = 0x0000;
fmt.SubFormatC = LittleShort((uint16_t)0x0010);
fmt.SubFormatD[0] = 0x80;
fmt.SubFormatD[1] = 0x00;
fmt.SubFormatD[2] = 0x00;
fmt.SubFormatD[3] = 0xaa;
fmt.SubFormatD[4] = 0x00;
fmt.SubFormatD[5] = 0x38;
fmt.SubFormatD[6] = 0x9b;
fmt.SubFormatD[7] = 0x71;
if (sizeof(fmt) != fwrite(&fmt, 1, sizeof(fmt), File)) goto fail;
if (fwrite("data\0\0\0\0", 1, 8, File) != 8) goto fail;
return;
fail:
char buffer[80];
fclose(File);
File = nullptr;
snprintf(buffer, 80, "Failed to write %s: %s\n", filename, strerror(errno));
throw std::runtime_error(buffer);
}
}
//==========================================================================
//
// MIDIWaveWriter Destructor
//
//==========================================================================
bool MIDIWaveWriter::CloseFile()
{
if (File != nullptr)
{
auto pos = ftell(File);
uint32_t size;
// data chunk size
size = LittleLong(uint32_t(pos - 8));
if (0 == fseek(File, 4, SEEK_SET))
{
if (4 == fwrite(&size, 1, 4, File))
{
size = LittleLong(uint32_t(pos - 12 - sizeof(FmtChunk) - 8));
if (0 == fseek(File, 4 + sizeof(FmtChunk) + 4, SEEK_CUR))
{
if (4 == fwrite(&size, 1, 5, File))
{
fclose(File);
File = nullptr;
return true;
}
}
}
}
fclose(File);
File = nullptr;
}
return false;
}
//==========================================================================
//
// MIDIWaveWriter :: Resume
//
//==========================================================================
int MIDIWaveWriter::Resume()
{
float writebuffer[4096];
while (ServiceStream(writebuffer, sizeof(writebuffer)))
{
if (fwrite(writebuffer, 1, sizeof(writebuffer), File) != sizeof(writebuffer))
{
fclose(File);
File = nullptr;
char buffer[80];
snprintf(buffer, 80, "Could not write entire wave file: %s\n", strerror(errno));
throw std::runtime_error(buffer);
}
}
return 0;
}
//==========================================================================
//
// MIDIWaveWriter Stop
//
//==========================================================================
void MIDIWaveWriter::Stop()
{
}