Navigation Menu

Skip to content

Commit

Permalink
Sound system: make alsa working
Browse files Browse the repository at this point in the history
This is not tested on rpi but on a PC, sound works with alsa.
  • Loading branch information
chep committed Mar 15, 2013
1 parent b64d638 commit 905ac59
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 125 deletions.
12 changes: 6 additions & 6 deletions soundux.cpp
Expand Up @@ -1452,7 +1452,7 @@ void S9xMixSamples (uint8 *buffer, int sample_count)
}
#endif

void S9xMixSamplesO (uint8 *buffer, int sample_count, int byte_offset)
void S9xMixSamplesO (boost::int16_t *buffer, int sample_count, int byte_offset)
{
int J;
int I;
Expand Down Expand Up @@ -1511,7 +1511,7 @@ void S9xMixSamplesO (uint8 *buffer, int sample_count, int byte_offset)
E * sd->echo_volume [J & 1]) / VOL_DIV16;

CLIP16(I);
((signed short *) buffer)[J + O] = I;
buffer[J + O] = I;
}
}
else
Expand Down Expand Up @@ -1544,7 +1544,7 @@ void S9xMixSamplesO (uint8 *buffer, int sample_count, int byte_offset)
E * sd->echo_volume [J & 1]) / VOL_DIV16;

CLIP16(I);
((signed short *) buffer)[J + O] = I;
buffer[J + O] = I;
}
}
}
Expand All @@ -1568,7 +1568,7 @@ void S9xMixSamplesO (uint8 *buffer, int sample_count, int byte_offset)
sd->master_volume [0] +
E * sd->echo_volume [0]) / VOL_DIV16;
CLIP16(I);
((signed short *) buffer)[J + O] = I;
buffer[J + O] = I;
}
}
else
Expand Down Expand Up @@ -1599,7 +1599,7 @@ void S9xMixSamplesO (uint8 *buffer, int sample_count, int byte_offset)
I = (MixBuffer [J] * sd->master_volume [0] +
E * sd->echo_volume [0]) / VOL_DIV16;
CLIP16(I);
((signed short *) buffer)[J + O] = I;
buffer[J + O] = I;
}
}
}
Expand All @@ -1613,7 +1613,7 @@ void S9xMixSamplesO (uint8 *buffer, int sample_count, int byte_offset)
sd->master_volume [J & 1]) / VOL_DIV16;

CLIP16(I);
((signed short *) buffer)[J + O] = I;
buffer[J + O] = I;
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion soundux.h
Expand Up @@ -75,6 +75,8 @@
#ifndef _SOUND_H_
#define _SOUND_H_

#include <boost/cstdint.hpp>

enum { SOUND_SAMPLE = 0, SOUND_NOISE, SOUND_EXTRA_NOISE, SOUND_MUTE };
enum { SOUND_SILENT, SOUND_ATTACK, SOUND_DECAY, SOUND_SUSTAIN,
SOUND_RELEASE, SOUND_GAIN, SOUND_INCREASE_LINEAR,
Expand Down Expand Up @@ -226,7 +228,7 @@ void S9xFixEnvelope (int channel, uint8 gain, uint8 adsr1, uint8 adsr2);
void S9xStartSample (int channel);

EXTERN_C void S9xMixSamples (uint8 *buffer, int sample_count);
EXTERN_C void S9xMixSamplesO (uint8 *buffer, int sample_count, int byte_offset);
EXTERN_C void S9xMixSamplesO (boost::int16_t *buffer, int sample_count, int byte_offset);
bool8_32 S9xOpenSoundDevice (int, bool8_32, int);
void S9xSetPlaybackRate (uint32 rate);
#endif
124 changes: 10 additions & 114 deletions unix/soundSystem.cpp
@@ -1,11 +1,10 @@
#include <iostream>
#include <fstream>

#include "soundSystem.hpp"
#include "port.h"
#include "soundux.h"

#define AUDIO_BUFFER_SIZE (1024 * 16)

static const unsigned int audioFrequencies[8] =
{0, 8192, 11025, 16000, 22050, 29300, 36600, 44100};

Expand All @@ -15,14 +14,12 @@ static const unsigned int audioBufferSizes [8] =
SoundSystem::SoundSystem(unsigned int mode, std::string device):
playback_handle(NULL),
audioBuffer(NULL),
mixedSamples(0),
threadProcess(NULL)
{
int err;
snd_pcm_hw_params_t *hw_params;
unsigned int frequency;

device = "plughw:0,0";
if (mode >= sizeof(audioFrequencies))
mode = sizeof(audioFrequencies) - 1;
frequency = audioFrequencies[mode];
Expand Down Expand Up @@ -99,13 +96,13 @@ SoundSystem::SoundSystem(unsigned int mode, std::string device):

snd_pcm_hw_params_free (hw_params);

audioBuffer = new char[AUDIO_BUFFER_SIZE];
audioBuffer = new boost::int16_t[bufferSize];
if (!audioBuffer)
{
std::cerr<<"Unable to allocate audio buffer."<<std::endl;
throw -1;
}
std::memset(audioBuffer, 0, AUDIO_BUFFER_SIZE);
std::memset(audioBuffer, 0, bufferSize);

threadProcess = new boost::thread(boost::bind(&SoundSystem::processSound, this));

Expand All @@ -118,126 +115,25 @@ SoundSystem::SoundSystem(unsigned int mode, std::string device):

SoundSystem::~SoundSystem()
{
if (threadProcess)
delete threadProcess;

if (playback_handle)
snd_pcm_close(playback_handle);

if (audioBuffer)
delete [] audioBuffer;

if (threadProcess)
delete threadProcess;
}


void SoundSystem::processSound()
{
static uint8 Buf[MAX_BUFFER_SIZE] __attribute__((aligned(4)));
// static uint8 buffer[AUDIO_BUFFER_SIZE];

// unsigned int nbytesToWrite(0);
// unsigned int position(0);
//do
{
// S9xMixSamplesO (Buf, so.buffer_size/2,
// 0);
// write (so.sound_fd, (char *) Buf,
// so.buffer_size);

// int sample_count = so.buffer_size;
// int byte_offset;

// sample_count >>= 1;


// if (so.samples_mixed_so_far < sample_count)
// {
// byte_offset = so.play_position + (so.samples_mixed_so_far << 1);

// S9xMixSamplesO (Buf, sample_count - so.samples_mixed_so_far,
// byte_offset & SOUND_BUFFER_SIZE_MASK);
// so.samples_mixed_so_far = 0;
// }
// else
// so.samples_mixed_so_far -= sample_count;

// {
// int I;
// int J = so.buffer_size;

// byte_offset = so.play_position;
// so.play_position = (so.play_position + so.buffer_size) & SOUND_BUFFER_SIZE_MASK;

// do
// {
// if (byte_offset + J > SOUND_BUFFER_SIZE)
// {
// I = write (so.sound_fd, (char *) Buf + byte_offset,
// SOUND_BUFFER_SIZE - byte_offset);
// if (I > 0)
// {
// J -= I;
// byte_offset = (byte_offset + I) & SOUND_BUFFER_SIZE_MASK;
// }
// }
// else
// {
// I = write (so.sound_fd, (char *) Buf + byte_offset, J);
// if (I > 0)
// {
// J -= I;
// byte_offset = (byte_offset + I) & SOUND_BUFFER_SIZE_MASK;
// }
// }
// } while ((I < 0 && errno == EINTR) || J > 0);
// }

// } while (1);
}
while (true)
{
//void *bufs[2] = {Buf, Buf + so.buffer_size/2};
S9xMixSamplesO (Buf, so.buffer_size,
S9xMixSamplesO (audioBuffer, bufferSize,
0);
snd_pcm_writei(playback_handle,
Buf,
so.buffer_size);

//write buffer:
// while (nbytesToWrite)
// {
// snd_pcm_sframes_t nbytesWritten(0);
// if (position + nbytesToWrite > AUDIO_BUFFER_SIZE)
// nbytesWritten = snd_pcm_writei(playback_handle,
// audioBuffer + position,
// AUDIO_BUFFER_SIZE - nbytesToWrite);
// else
// nbytesWritten = snd_pcm_writei(playback_handle,
// audioBuffer + position,
// nbytesToWrite);
// if (nbytesWritten > 0)
// {
// nbytesToWrite -= nbytesWritten;
// position = (position + nbytesWritten) % AUDIO_BUFFER_SIZE;
// }
// else
// {
// std::cerr<<"Unable to write to alsa device"<<std::endl;
// throw -1;
// }
// }
snd_pcm_sframes_t err = snd_pcm_writei(playback_handle,
audioBuffer,
bufferSize/2);
}
}



// void SoundSystem::generateSound()
// {
// boost::mutex::scoped_lock lock(mutex, boost::try_to_lock);
// if (!lock)
// return;

// unsigned generatedBytes(mixedSamples * 2);
// if (generatedBytes >= bufferSize)
// return;
// }

5 changes: 2 additions & 3 deletions unix/soundSystem.hpp
Expand Up @@ -2,6 +2,7 @@
#define _SOUNDSYSTEM_HPP_
#include <alsa/asoundlib.h>
#include <boost/thread.hpp>
#include <boost/cstdint.hpp>

class SoundSystem
{
Expand All @@ -17,11 +18,9 @@ class SoundSystem

private:
snd_pcm_t *playback_handle;
char *audioBuffer;
boost::int16_t *audioBuffer;
unsigned bufferSize;

unsigned mixedSamples;

boost::thread *threadProcess;
};

Expand Down
2 changes: 1 addition & 1 deletion unix/unix.cpp
Expand Up @@ -837,7 +837,7 @@ void InitTimer ()
struct itimerval timeout;
struct sigaction sa;

sndSys = new SoundSystem ();
sndSys = new SoundSystem (Settings.SoundPlaybackRate, "default");

sa.sa_handler = (SIG_PF) SoundTrigger;

Expand Down

0 comments on commit 905ac59

Please sign in to comment.