Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
*WINPLR 0.1.15*
Browse files Browse the repository at this point in the history
Added XAudio Player
Re-factored code
Auto detect format
  • Loading branch information
vertver committed Aug 9, 2018
1 parent e34e708 commit 7b0f63e
Show file tree
Hide file tree
Showing 13 changed files with 496 additions and 238 deletions.
48 changes: 23 additions & 25 deletions WinPlr/WinAudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <dsound.h> // for DirectSound
#include <mmreg.h>
#include <mmeapi.h>
#include <VersionHelpers.h>
#include <mmdeviceapi.h> // minimal MME API
#include <ks.h>
#include <ksmedia.h>
Expand All @@ -23,7 +24,9 @@
#include <strsafe.h>
#include <d2d1_3.h>
#include <dxgi.h>
#include <audioclient.h>
#include <xaudio2.h>
#include <stdio.h>
#include <process.h>

#define DLL_EXPORTS
#ifdef DLL_EXPORTS
Expand All @@ -32,6 +35,9 @@
#define PLAYER_API decltype(dllimport)
#endif

template<typename T> T freadNum(FILE* f);
std::string freadStr(FILE* f, size_t len);

VOID CreateErrorText(LPCSTR lpMsgText);
VOID CreateErrorText(LPCSTR lpMsgText, HRESULT hr);
VOID CreateInfoText(LPCSTR lpMsgText);
Expand All @@ -42,8 +48,9 @@ LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
#define _RELEASE(x) if (x) { x->Release(); } x = NULL; // safety release pointers
#define R_ASSERT2(x, y) if (!SUCCEEDED(x)) { CreateErrorText(y, x); }
#define R_ASSERT(x) if (!SUCCEEDED(x)) { CreateErrorText("R_ASSERT"); }
#define DO_EXIT(x, y) if (!x) { CreateErrorText(y); }
#define PLAYER_VERSION "#PLAYER_VERSION: 0.1.13#"
#define ASSERT(x, y) if (!x) { OutputDebugStringA(y); OutputDebugStringA("\n");}
#define DO_EXIT(x, y) if (!(x)) { CreateErrorText(y); }
#define PLAYER_VERSION "#PLAYER_VERSION: 0.1.15#"

typedef enum
{
Expand Down Expand Up @@ -79,7 +86,7 @@ typedef struct
LPVOID lpData; // pointer to allocated memory
WORD wBitrate; // bit rate of sample
WORD wChannels; // count of channels
WORD wBits; // count of bits
BOOL isFloat; // IEEE or PCM
BOOL bReorderChannels; // if we need to reorder channels - get this
DWORD dwSamplerate; // sample rate of sample
DWORD dwTime; // durability of sample
Expand Down Expand Up @@ -115,10 +122,10 @@ typedef struct
typedef struct
{
PCM_DATA dPCM; // all PCM data for DirectSound
LPDIRECTSOUND lpDirectSound = NULL; // DirectSound main object
LPDIRECTSOUNDBUFFER lpPrimaryDirectBuffer = NULL; // DirectSound buffer
LPDIRECTSOUNDBUFFER lpSecondaryDirectBuffer = NULL; // DirectSound buffer
LPDIRECTSOUNDNOTIFY lpDirectNotify = NULL; // DirectSound notify
LPDIRECTSOUND lpDirectSound; // DirectSound main object
LPDIRECTSOUNDBUFFER lpPrimaryDirectBuffer; // DirectSound buffer
LPDIRECTSOUNDBUFFER lpSecondaryDirectBuffer; // DirectSound buffer
LPDIRECTSOUNDNOTIFY lpDirectNotify; // DirectSound notify
BOOL bPlaying; // display if audio now is playing
} STREAM_DATA, *STREAM_DATA_P;

Expand All @@ -129,6 +136,13 @@ typedef struct
HINSTANCE hInstance; // hInstance
} HWND_DATA, *HWND_DATA_P;

typedef struct
{
FILE_DATA dData; // file data struct
PCM_DATA dPCM; // PCM data struct
} AUDIO_FILE;


namespace Player
{
class Buffer
Expand All @@ -141,7 +155,6 @@ namespace Player
class Stream
{
public:
STREAM_DATA CreateWASAPIStream(FILE_DATA fileData, PCM_DATA pcmData, HWND hwnd);
STREAM_DATA CreateMMIOStream(FILE_DATA dData, PCM_DATA dPCM, HWND hwnd);
STREAM_DATA CreateDirectSoundStream(FILE_DATA dData, PCM_DATA dPCM, HWND hwnd);
VOID PlayBufferSound(STREAM_DATA streamData);
Expand All @@ -151,26 +164,11 @@ namespace Player
class ThreadSystem
{
public:
HANDLE ThCreateNewMutex(LPCSTR lpName);
DWORD ThCreateNewThread(LPVOID lpFunc, HANDLE hMutex);
VOID ThSetNewThreadName(DWORD dwThreadID, LPCSTR lpName);
VOID ThBeginXAudioThread(AUDIO_FILE audioFile);
};
class Graphics
{
public:
};
class DirectGraphic
{
public:
VOID SetDirectWindow(HWND hwnd, int x, int y);
protected:
ID2D1Factory* pD2DFactory = NULL;
ID2D1Device* pD2DDevice = NULL;

ID2D1DeviceContext* pD2DContext = NULL;
ID2D1HwndRenderTarget* pRT = NULL;
ID2D1Bitmap* pD2DRenderTargets[2];
ID2D1SolidColorBrush *pBrush = NULL;
};
}

52 changes: 0 additions & 52 deletions WinPlr/WinDirect.cpp

This file was deleted.

18 changes: 12 additions & 6 deletions WinPlr/WinFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* File-system for WinPlr
*********************************************************/
#include "WinAudio.h"
#include "WinFileReader.h"

/*************************************************
* LoadFileToBuffer():
Expand Down Expand Up @@ -84,15 +85,20 @@ Player::Buffer::LoadFileToBuffer(
dFile.hFile = hData;
dFile.lpFile = lpData;

dPCM.bWindows = TRUE; // WINAPI methods play audio
dPCM.dwSamplerate = 44100;
dPCM.wBitrate = 16;
dPCM.wBits = 16;
dPCM.dwTime = 10000; // 10000 msecs - 10 secs
CloseHandle(hData);

//
FileReader fReader;
PCM_DATA fileType = fReader.GetFullFileInfo(oFN.lpstrFile);

dPCM.bWindows = TRUE; // WINAPI methods play audio
dPCM.dwSamplerate = fileType.dwSamplerate;
dPCM.wBitrate = fileType.wBitrate;
dPCM.dwTime = 10000; // 10000 msecs - 10 secs
dPCM.lpData = lpData;
dPCM.lpName = oFN.lpstrFileTitle;
dPCM.lpPath = oFN.lpstrFile;
dPCM.wChannels = 2;
dPCM.wChannels = fileType.wChannels;

dFFT.lpData = lpData;
dFFT.dwSizeBuffer = 1024;
Expand Down
191 changes: 191 additions & 0 deletions WinPlr/WinFileReader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
/*********************************************************
* Copyright (C) VERTVER, 2018. All rights reserved.
* WinPlr - open-source WINAPI audio player.
* MIT-License
**********************************************************
* Module Name: WinAudio file-reader
**********************************************************
* WinFileReader.cpp
* File type checking
*********************************************************/

#include "WinFileReader.h"
FILE* fileData;
PCM_DATA pcmData;

/***********************************************
* freadNum():
* Checking for bad number
***********************************************/
template
<typename T> T
freadNum(
FILE* f
)
{
T value;
DO_EXIT(fread(&value, sizeof(value), 1, f) == 1, "Can't read file");
return value; // no endian-swap for now... WAV is LE anyway...
}

/***********************************************
* freadStr():
* Checking for bad string
***********************************************/
std::string
freadStr(
FILE* f,
size_t len
)
{
std::string s(len, '\0');
DO_EXIT(fread(&s[0], 1, len, f) == len, "Can't read file");
return s;
}

/***********************************************
* ReadFmtChunk():
* Reading FMT chunk with information
***********************************************/
PCM_DATA
ReadFmtChunk(
UINT uChunkLen
)
{
WORD dwSampleBits = NULL;
DO_EXIT(uChunkLen >= 16, "Error type");

WORD fmttag = freadNum<WORD>(fileData);
DO_EXIT(fmttag == 1 || fmttag == 3, "Error"); // IEEE or PCM

// check for FMT tag
switch (fmttag)
{
case 1:
pcmData.isFloat = FALSE;
case 3:
pcmData.isFloat = TRUE;
default:
break;
}

// check channels
WORD wChannels = freadNum<WORD>(fileData);
DO_EXIT(wChannels > 0, "No channels");
pcmData.wChannels = wChannels;

// check sample rate
UINT dwSampleRate = freadNum<UINT>(fileData);
DO_EXIT(dwSampleRate > 0, "No sample rate");
pcmData.dwSamplerate = dwSampleRate;

// byte and bitrate
UINT dwByteRate = freadNum<UINT>(fileData);
WORD wBlockAlign = freadNum<WORD>(fileData);

// count of bits and bytes
WORD wBits = freadNum<WORD>(fileData);
WORD wBytes = wBits / 8;

// compare all data
DO_EXIT(dwByteRate == dwSampleRate * wChannels * wBytes, "Error types");
DO_EXIT(wBlockAlign == wChannels * wBytes, "Error block align");

// 1: PCM - int
// 3: IEEE - float
if (fmttag == 1)
{
switch (wBits)
{
case 8: dwSampleBits = 8; break;
case 16: dwSampleBits = 16; break;
case 24: dwSampleBits = 24; break;
case 32: dwSampleBits = 32; break;
}
}
else
{
DO_EXIT(fmttag == 3, "No type");
DO_EXIT(wBits == 32, "No type");
}
if (uChunkLen > 16)
{
// compare with extented size
WORD dwExtentedSize = freadNum<WORD>(fileData);
DO_EXIT(uChunkLen == 18 + dwExtentedSize, "No chunk len");
fseek(fileData, dwExtentedSize, SEEK_CUR);
}

// returnt this struct
pcmData.wBitrate = dwSampleBits;
return pcmData;
}

/***********************************************
* GetFullFileType():
* Return current type of file
***********************************************/
FILE_TYPE
FileReader::GetFullFileType(
LPCSTR lpString
)
{
// open file with "read-only" rools
fileData = fopen(lpString, "r");
DO_EXIT(fileData, "Can't open file");

// if file is WAVE PCM - return
if (freadStr(fileData, 4) == "RIFF")
{
UINT wavechunksize = freadNum<UINT>(fileData);
DO_EXIT((freadStr(fileData, 4) == "WAVE"), "No wave");
return WAV_FILE;
}
return UNKNOWN_FILE;
}

/***********************************************
* ReadChunks():
* Checking valid chunks
***********************************************/
VOID
FileReader::ReadChunks()
{
while (TRUE)
{
// set chunk name and chunk length
std::string chunkName = " ";
chunkName = freadStr(fileData, 4);
UINT chunkLen = freadNum<UINT>(fileData);

// if chunk include FMT data - read this
if (chunkName == "fmt ")
{
ReadFmtChunk(chunkLen);
}
else if (chunkName == "data") break; // else break cycle
else
{
DO_EXIT((fseek(fileData, chunkLen, SEEK_CUR) == NULL), "Can't seek file"); // skip chunk
}
}
}

/***********************************************
* GetFullFileInfo():
* returnt full info about audiofile
***********************************************/
PCM_DATA
FileReader::GetFullFileInfo(LPCSTR lpPath)
{
FILE_TYPE eType;
eType = GetFullFileType(lpPath);
if (eType == WAV_FILE)
{
ReadChunks();
free(fileData);
return pcmData;
}
DO_EXIT(eType, "Can't check format");
return pcmData;
}
Loading

0 comments on commit 7b0f63e

Please sign in to comment.