Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Kielan committed Jun 3, 2016
1 parent 50f044a commit 25d71e1
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 90 deletions.
56 changes: 36 additions & 20 deletions AudioBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,6 @@ AudioBase::AudioBase(const char* _name) : name{_name}
}


snd_pcm_hw_params_t* AudioBase::getParamStructure() const
{
return hwParams;
}


snd_pcm_t* AudioBase::getDeviceStructure() const
{
return soundDevice;
}


void AudioBase::abort(int err)
{
log_write( "Error: ");
Expand Down Expand Up @@ -92,7 +80,7 @@ void AudioBase::setRateNear(unsigned int val, int dir)
void AudioBase::setParams(snd_pcm_format_t format, snd_pcm_access_t access, Channels channels,
unsigned int rate, int soft_resample, unsigned int latency)
{
int err = snd_pcm_set_params(getDeviceStructure(), format, access,
int err = snd_pcm_set_params(soundDevice, format, access,
static_cast<unsigned int>(channels), rate, soft_resample, latency);
if(err < 0)
abort(err);
Expand All @@ -101,7 +89,7 @@ void AudioBase::setParams(snd_pcm_format_t format, snd_pcm_access_t access, Chan

snd_pcm_sframes_t AudioBase::writeInterleaved(const void* buffer, snd_pcm_uframes_t size)
{
int frames = snd_pcm_writei(getDeviceStructure(), buffer, size);
int frames = snd_pcm_writei(soundDevice, buffer, size);

if(frames < 0)
recoverStream(frames, 0);
Expand All @@ -112,21 +100,45 @@ snd_pcm_sframes_t AudioBase::writeInterleaved(const void* buffer, snd_pcm_uframe

snd_pcm_sframes_t AudioBase::writeNonInterleaved(void** buffer, snd_pcm_uframes_t size)
{
return snd_pcm_writen(getDeviceStructure(), buffer, size);
int frames = snd_pcm_writen(soundDevice, buffer, size);

if(frames < 0)
recoverStream(frames, 0);

return frames;
}

snd_pcm_sframes_t AudioBase::write(void* buffer, snd_pcm_uframes_t size)
{
auto access = getAccess();
snd_pcm_sframes_t frames {};

if( (access == SND_PCM_ACCESS_MMAP_INTERLEAVED) ||
(access == SND_PCM_ACCESS_RW_INTERLEAVED) )
frames =writeInterleaved(buffer, size);

else if ( (access == SND_PCM_ACCESS_RW_NONINTERLEAVED) ||
(access == SND_PCM_ACCESS_MMAP_NONINTERLEAVED) )
frames = writeNonInterleaved(&buffer, size);

else
abort();

return frames;
}


void AudioBase::recoverStream(int err, int silent)
{
int _err = snd_pcm_recover(getDeviceStructure(), err, silent);
int _err = snd_pcm_recover(soundDevice, err, silent);
if(_err < 0)
abort();
}


void AudioBase::stopPresentingFrames()
{
int err = snd_pcm_drain(getDeviceStructure());
int err = snd_pcm_drain(soundDevice);
if(err < 0)
abort();
}
Expand Down Expand Up @@ -168,10 +180,14 @@ unsigned int AudioBase::getRate() const
}


snd_pcm_access_t AudioBase::getAccess() const
snd_pcm_access_t AudioBase::getAccess()
{
snd_pcm_access_t access;
snd_pcm_access_t access {};
snd_pcm_hw_params_get_access(hwParams, &access);

if(!((access >= 0) && (access <= SND_PCM_ACCESS_LAST)))
abort(static_cast<int>(access));

return access;
}

Expand All @@ -184,4 +200,4 @@ AudioBase::~AudioBase()

}

} /* namespace Sound */
} /* namespace Audio */
21 changes: 11 additions & 10 deletions AudioBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <alsa/asoundlib.h>
#include "Logger.h"
#include <iostream>

#include <memory>

namespace Audio {

Expand All @@ -26,8 +26,6 @@ enum class Channels : unsigned int
*/
class AudioBase
{
// std::shared_ptr<snd_pcm_t *> soundDevice;
// std::shared_ptr<snd_pcm_hw_params_t *> hwParams;
snd_pcm_t* soundDevice { nullptr };
snd_pcm_hw_params_t* hwParams { nullptr };
const char* name { nullptr };
Expand All @@ -36,27 +34,25 @@ class AudioBase

AudioBase(const char* _name);

snd_pcm_hw_params_t* getParamStructure() const;


snd_pcm_t* getDeviceStructure() const;


/**
* \ Abort function, make extension if error code is existing
*/
void abort(int err = -1);


/**
* \ Open device
*/
void opendev(snd_pcm_stream_t stream, int mode = 0);


/**
* \ Allocate hardware param memory and set the default values
*/
void allocateDefault();


/**
* \
*/
Expand Down Expand Up @@ -99,6 +95,11 @@ class AudioBase
snd_pcm_sframes_t writeNonInterleaved(void** buffer, snd_pcm_uframes_t size);


/**
* \ Write
*/
snd_pcm_sframes_t write(void* buffer, snd_pcm_uframes_t size);

/**
* \
*/
Expand Down Expand Up @@ -150,14 +151,14 @@ class AudioBase
/**
* \
*/
snd_pcm_access_t getAccess() const;
snd_pcm_access_t getAccess();

/*
*
*/
virtual ~AudioBase();
};

} /* namespace Sound */
} /* namespace Audio */

#endif /* AUDIOBASE_H_ */
2 changes: 1 addition & 1 deletion Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ class Player {
virtual ~Player();
};

} /* namespace Sound */
} /* namespace Audio */

#endif /* PLAYER_H_ */
30 changes: 16 additions & 14 deletions WavPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ void WavPlayer::load(const char* _filename)

fs.open(filename, std::fstream::in);

if(!fs.is_open())
abort();

if(!isWavFile(fs))
abort();

Expand All @@ -59,11 +62,9 @@ void WavPlayer::load(const char* _filename)

if(id == "fmt ")
{
WavFmtParam fmt;

if(fs.read(reinterpret_cast<char *>(&fmt), sizeof(WavFmtParam)))
if(fs.read(reinterpret_cast<char *>(&fmt), sizeof(WavFmtHeader)))
{
if(fmt.wFormatTag != 1)
if(fmt.audioFormat != 1)
{
log_writeln("Compressed WAVE not supported yet");
abort();
Expand All @@ -72,10 +73,6 @@ void WavPlayer::load(const char* _filename)

else
break;

waveBits = static_cast<unsigned char>(fmt.wBitsPerSample);
waveRate = static_cast<unsigned short>(fmt.dwSamplesPerSec);
waveChannels = static_cast<Channels>(fmt.wChannels);
}

else if (id == "data")
Expand All @@ -92,7 +89,7 @@ void WavPlayer::load(const char* _filename)
fs.read(buf.data.get(), buf.len - offsite); /// TODO - wtf with len

if(fs)
waveSize = (buf.len * 8) / ( static_cast<unsigned int>(waveBits) * static_cast<unsigned int>(waveChannels) );
waveSize = (buf.len * 8) / ( static_cast<unsigned int>(fmt.bitsPerSample) * static_cast<unsigned int>(fmt.channels) );

else
{
Expand All @@ -102,7 +99,8 @@ void WavPlayer::load(const char* _filename)
}
}

setParams(convertBitsToPcmFormat(waveBits), SND_PCM_ACCESS_RW_INTERLEAVED, waveChannels, waveRate, 1, 500000);
setParams(convertBitsToPcmFormat(fmt.bitsPerSample), SND_PCM_ACCESS_RW_INTERLEAVED,
static_cast<Channels>(fmt.channels), static_cast<unsigned short>(fmt.byteRate), 1, 500000);
}


Expand Down Expand Up @@ -140,27 +138,31 @@ void WavPlayer::play(const char* _filename)
{
snd_pcm_uframes_t count {}, frames {};

load( _filename);
load(_filename);

if(buf.data.get() != nullptr)
{
do
{
frames = writeInterleaved(buf.data.get() + count, waveSize - count);
frames = write(buf.data.get() + count, waveSize - count);
count += frames;

} while (count < waveSize);

// Wait for playback to completely finish
if (count == waveSize)
if (count >= waveSize)
stopPresentingFrames();
}

else
log_write("File not loaded correctly");

// clear data
buf.data.reset();
buf.data = nullptr;
memset(&fmt,0,sizeof(WavFmtHeader));
}



} /* namespace Sound */
} /* namespace Audio */
22 changes: 10 additions & 12 deletions WavPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ typedef struct WavChunkHeader


// WAVE fmt chunk
typedef struct WavFmtParam {
short wFormatTag;
unsigned short wChannels;
unsigned int dwSamplesPerSec;
unsigned int dwAvgBytesPerSec;
unsigned short wBlockAlign;
unsigned short wBitsPerSample;
} WavFmtParam;
typedef struct WavFmtHeader {
short audioFormat;
unsigned short channels;
unsigned int sampleRate;
unsigned int byteRate;
unsigned short blockAlign;
unsigned short bitsPerSample;
} WavFmtHeader;

#pragma pack()

Expand Down Expand Up @@ -116,10 +116,8 @@ typedef struct Buffer

class WavPlayer : public Player, public AudioBase
{
unsigned char waveBits {};
unsigned short waveRate {};
Channels waveChannels {};
snd_pcm_uframes_t waveSize {};
WavFmtHeader fmt {};

const char *filename { nullptr };
Buffer buf {nullptr, 0};
Expand Down Expand Up @@ -159,6 +157,6 @@ class WavPlayer : public Player, public AudioBase
void play(const char* _filename) override;
};

} /* namespace Sound */
} /* namespace Audio */

#endif /* WAVPLAYER_H_ */
Loading

0 comments on commit 25d71e1

Please sign in to comment.