Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //compile with:
- //"cl tiny_bytebeat.cpp kernel32.lib /GS- /Os /link /entry:main /SUBSYSTEM:WINDOWS /NODEFAULTLIB"
- //("/GS-" to disable buffer security check, since it seems to break when not using crt)
- //#define WIN32_LEAN_AND_MEAN
- #include <Windows.h>
- #include <mmsystem.h>
- #define _PI2 1.57079632679489661923f
- #define _PI 3.14159265358979323846f
- #define _2PI 6.28318530717958647692f
- #define lengthof(_thing, _type) (sizeof(_thing)/sizeof(_type))
- //doing "*((FARPROC*)&_name)" to work around not
- //being able to directly cast an lvalue
- #define LOAD_FUNC(_strname, _name, _dll) \
- *((FARPROC*)&_name) = GetProcAddress(_dll, _strname)
- typedef unsigned char u8;
- typedef unsigned short u16;
- typedef unsigned int u32;
- typedef unsigned long long u64;
- typedef signed char s8;
- typedef signed short s16;
- typedef signed int s32;
- typedef signed long long s64;
- typedef float f32;
- typedef double f64;
- //"tiny.obj : error LNK2001: unresolved external symbol _fltused"
- //(i think this is because i'm not using the crt)
- #ifdef __cplusplus
- extern "C" {
- #endif
- char _fltused = 0; //this works lmaooo
- #ifdef __cplusplus
- }
- #endif
- /*******************************************************************************/
- HMODULE _User32_dll;
- HMODULE _winmm_dll;
- //User32
- #define MessageBoxA MessageBoxA_
- int (WINAPI *MessageBoxA_)(HWND, LPCSTR, LPCSTR, UINT);
- //winmm
- #define waveOutGetNumDevs winmm.waveOutGetNumDevs_
- #define waveOutGetDevCapsA winmm.waveOutGetDevCapsA_
- #define waveOutGetVolume winmm.waveOutGetVolume_
- #define waveOutSetVolume winmm.waveOutSetVolume_
- #define waveOutGetErrorTextA winmm.waveOutGetErrorTextA_
- #define waveOutOpen winmm.waveOutOpen_
- #define waveOutClose winmm.waveOutClose_
- #define waveOutPrepareHeader winmm.waveOutPrepareHeader_
- #define waveOutUnprepareHeader winmm.waveOutUnprepareHeader_
- #define waveOutWrite winmm.waveOutWrite_
- #define waveOutPause winmm.waveOutPause_
- #define waveOutRestart winmm.waveOutRestart_
- #define waveOutReset winmm.waveOutReset_
- #define waveOutBreakLoop winmm.waveOutBreakLoop_
- #define waveOutGetPosition winmm.waveOutGetPosition_
- #define waveOutGetPitch winmm.waveOutGetPitch_
- #define waveOutSetPitch winmm.waveOutSetPitch_
- #define waveOutGetPlaybackRate winmm.waveOutGetPlaybackRate_
- #define waveOutSetPlaybackRate winmm.waveOutSetPlaybackRate_
- #define waveOutGetID winmm.waveOutGetID_
- #define waveOutMessage winmm.waveOutMessage_
- #define winmm_strlength 21
- #define winmm_names_len (lengthof(winmm_names, char)/winmm_strlength)
- char winmm_names[][winmm_strlength] = {
- //(not including null terminator)
- //"waveOutGetNumDevs", //length = [17]
- //"waveOutGetDevCapsA", //length = [18]
- //"waveOutGetVolume", //length = [16]
- "waveOutSetVolume", //length = [16]
- //"waveOutGetErrorTextA", //length = [20]
- "waveOutOpen", //length = [11]
- //"waveOutClose", //length = [12]
- "waveOutPrepareHeader", //length = [20]
- //"waveOutUnprepareHeader", //length = [22]
- "waveOutWrite", //length = [12]
- "waveOutPause", //length = [12]
- //"waveOutRestart", //length = [14]
- //"waveOutReset", //length = [12]
- //"waveOutBreakLoop", //length = [16]
- //"waveOutGetPosition", //length = [18]
- //"waveOutGetPitch", //length = [15]
- //"waveOutSetPitch", //length = [15]
- //"waveOutGetPlaybackRate", //length = [22]
- //"waveOutSetPlaybackRate", //length = [22]
- //"waveOutGetID", //length = [12]
- //"waveOutMessage", //length = [14]
- };
- union winmm_func {
- FARPROC _FUNCTIONS[winmm_names_len];
- struct {
- //UINT (WINAPI *waveOutGetNumDevs_)(void);
- //MMRESULT (WINAPI *waveOutGetDevCapsA_)(UINT_PTR, LPWAVEOUTCAPSA, UINT);
- //MMRESULT (WINAPI *waveOutGetVolume_)(HWAVEOUT, LPDWORD);
- MMRESULT (WINAPI *waveOutSetVolume_)(HWAVEOUT, DWORD);
- //MMRESULT (WINAPI *waveOutGetErrorTextA_)(MMRESULT, LPSTR, UINT);
- MMRESULT (WINAPI *waveOutOpen_)(LPHWAVEOUT, UINT, LPCWAVEFORMATEX, DWORD_PTR, DWORD_PTR, DWORD);
- //MMRESULT (WINAPI *waveOutClose_)(HWAVEOUT);
- MMRESULT (WINAPI *waveOutPrepareHeader_)(HWAVEOUT, LPWAVEHDR, UINT);
- //MMRESULT (WINAPI *waveOutUnprepareHeader_)(HWAVEOUT, LPWAVEHDR, UINT);
- MMRESULT (WINAPI *waveOutWrite_)(HWAVEOUT, LPWAVEHDR, UINT);
- MMRESULT (WINAPI *waveOutPause_)(HWAVEOUT);
- //MMRESULT (WINAPI *waveOutRestart_)(HWAVEOUT);
- //MMRESULT (WINAPI *waveOutReset_)(HWAVEOUT);
- //MMRESULT (WINAPI *waveOutBreakLoop_)(HWAVEOUT);
- //MMRESULT (WINAPI *waveOutGetPosition_)(HWAVEOUT, LPMMTIME, UINT);
- //MMRESULT (WINAPI *waveOutGetPitch_)(HWAVEOUT, LPDWORD);
- //MMRESULT (WINAPI *waveOutSetPitch_)(HWAVEOUT, DWORD);
- //MMRESULT (WINAPI *waveOutGetPlaybackRate_)(HWAVEOUT, LPDWORD);
- //MMRESULT (WINAPI *waveOutSetPlaybackRate_)(HWAVEOUT, DWORD);
- //MMRESULT (WINAPI *waveOutGetID_)(HWAVEOUT, LPUINT);
- //MMRESULT (WINAPI *waveOutMessage_)(HWAVEOUT, UINT, DWORD_PTR, DWORD_PTR);
- };
- };
- winmm_func winmm;
- static inline void LOAD_FUNC_ALL(){
- _User32_dll = LoadLibrary("User32.dll");
- _winmm_dll = LoadLibrary("winmm.dll");
- LOAD_FUNC("MessageBoxA", MessageBoxA_, _User32_dll);
- for(u32 i=0; i<winmm_names_len; ++i)
- winmm._FUNCTIONS[i] = GetProcAddress(_winmm_dll, winmm_names[i]);
- }
- /*******************************************************************************/
- #define SAMPLERATE 11025
- #define _chunk_count 3
- #define _chunk_samples 512
- s16 _chunk_data[_chunk_count][_chunk_samples];
- WAVEHDR _chunk_header[_chunk_count];
- int _chunk_which;
- HWAVEOUT wave_out;
- void audio_callback(s16* smp, u32 len);
- void CALLBACK WaveOutProc(HWAVEOUT hwo, UINT msg, DWORD_PTR inst,
- DWORD_PTR param1, DWORD_PTR param2)
- {
- if(msg == WOM_DONE){
- audio_callback(_chunk_data[_chunk_which], _chunk_samples);
- waveOutWrite(hwo, &_chunk_header[_chunk_which], sizeof(WAVEHDR));
- _chunk_which = (_chunk_which+1)%_chunk_count;
- }
- }
- static inline bool WaveOutInit(){
- _chunk_which = 0;
- WAVEFORMATEX fmt;
- fmt.wFormatTag = WAVE_FORMAT_PCM;
- fmt.nChannels = 1;
- fmt.nSamplesPerSec = SAMPLERATE;
- fmt.wBitsPerSample = 16;
- fmt.cbSize = 0;
- fmt.nBlockAlign = fmt.nChannels * fmt.wBitsPerSample / 8;
- fmt.nAvgBytesPerSec = fmt.nSamplesPerSec * fmt.nBlockAlign;
- if(waveOutOpen(&wave_out, WAVE_MAPPER, &fmt,
- (DWORD_PTR)WaveOutProc, 0, CALLBACK_FUNCTION))
- {
- return false;
- }
- #define WO_VOLUME(_left, _right) ( ((_right)<<16) | (_left) )
- if(waveOutSetVolume(wave_out, WO_VOLUME(0xffff, 0xffff) )) return false;
- //set header info and prime the audio buffers
- for(u32 i=0; i<_chunk_count; ++i){
- _chunk_header[i].lpData = (CHAR*)_chunk_data[i];
- _chunk_header[i].dwBufferLength = _chunk_samples*sizeof(s16)*fmt.nChannels;
- _chunk_header[i].dwBytesRecorded = 0;
- _chunk_header[i].dwUser = 0;
- _chunk_header[i].dwFlags = 0;
- _chunk_header[i].dwLoops = 0;
- _chunk_header[i].lpNext = nullptr;
- _chunk_header[i].reserved = 0;
- if(waveOutPrepareHeader(wave_out, &_chunk_header[i], sizeof(WAVEHDR)))
- return false;
- audio_callback(_chunk_data[i], _chunk_samples);
- if(waveOutWrite(wave_out, &_chunk_header[i], sizeof(WAVEHDR)))
- return false;
- }
- return true;
- }
- /*******************************************************************************/
- #define volume 0.05f
- #define u8_smp(_smp) ( (s16)( ((_smp)-128)<<8 ) ) //u8 to s16 sample
- void audio_callback(s16* smp, u32 len){
- static s64 t = 0;
- for(u32 i=0; i<len; ++i){
- //bytebeat shamelessly stolen from dollchan (go check out their bytebeat editor, seriously)
- smp[i] = u8_smp( (((t/10|0)^(t/10|0)-1280)%11*t/2&127)+(((t/640|0)^(t/640|0)-2)%13*t/2&127) );
- smp[i] *= volume;
- ++t;
- }
- }
- int main(){
- int returnCode = 0;
- LOAD_FUNC_ALL();
- if(!WaveOutInit()){
- MessageBoxA(nullptr, "failed to initialize audio device", "oh no!", MB_ICONERROR);
- --returnCode; goto _quit_;
- }
- MessageBoxA(nullptr, "cool bytebeat", "cool message box", MB_OK);
- //Sleep(2000);
- waveOutPause(wave_out);
- _quit_:
- //program hangs unless i explicitly exit the process for some reason
- ExitProcess((UINT)returnCode);
- return returnCode;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement