finally, at least input seems to work now, also did some cleanup
This commit is contained in:
parent
d15cce1c00
commit
07be35a465
1 changed files with 46 additions and 148 deletions
178
mac/sound.cpp
178
mac/sound.cpp
|
@ -162,6 +162,10 @@ void CSound::OpenCoreAudio()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// TODO check for required sample rate, if not available, throw error message
|
||||||
|
|
||||||
|
|
||||||
// TEST
|
// TEST
|
||||||
StoreInOutStreamProps ( audioUnit );
|
StoreInOutStreamProps ( audioUnit );
|
||||||
|
|
||||||
|
@ -185,9 +189,6 @@ void CSound::Start()
|
||||||
|
|
||||||
// call base class
|
// call base class
|
||||||
CSoundBase::Start();
|
CSoundBase::Start();
|
||||||
|
|
||||||
// TEST
|
|
||||||
printf ( "start\n" );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CSound::Stop()
|
void CSound::Stop()
|
||||||
|
@ -200,54 +201,21 @@ void CSound::Stop()
|
||||||
|
|
||||||
// call base class
|
// call base class
|
||||||
CSoundBase::Stop();
|
CSoundBase::Stop();
|
||||||
|
|
||||||
// TEST
|
|
||||||
printf ( "stop\n" );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int CSound::Init ( const int iNewPrefMonoBufferSize )
|
int CSound::Init ( const int iNewPrefMonoBufferSize )
|
||||||
{
|
{
|
||||||
// store buffer size
|
|
||||||
iCoreAudioBufferSizeMono = iNewPrefMonoBufferSize;
|
|
||||||
|
|
||||||
|
// TODO try to set the preferred buffer size to the audio unit
|
||||||
|
|
||||||
|
// get the audio unit buffer size
|
||||||
// TEST
|
UInt32 bufferSizeFrames;
|
||||||
//Get the size of the IO buffer(s)
|
UInt32 propertySize = sizeof(UInt32);
|
||||||
UInt32 bufferSizeFrames, bufferSizeBytes;
|
|
||||||
|
|
||||||
UInt32 propertySize = sizeof(bufferSizeFrames);
|
|
||||||
AudioUnitGetProperty ( audioUnit, kAudioDevicePropertyBufferFrameSize,
|
AudioUnitGetProperty ( audioUnit, kAudioDevicePropertyBufferFrameSize,
|
||||||
kAudioUnitScope_Global, 1, &bufferSizeFrames, &propertySize );
|
kAudioUnitScope_Global, 1, &bufferSizeFrames, &propertySize );
|
||||||
|
|
||||||
bufferSizeBytes = bufferSizeFrames * sizeof(Float32);
|
// store buffer size
|
||||||
|
iCoreAudioBufferSizeMono = bufferSizeFrames;
|
||||||
printf("Buffer_Size: %d", (int) bufferSizeFrames);
|
|
||||||
|
|
||||||
|
|
||||||
//malloc buffer lists
|
|
||||||
theBufferList = (AudioBufferList*) malloc(
|
|
||||||
offsetof(AudioBufferList, mBuffers[0]) + (sizeof(AudioBuffer) * 1));
|
|
||||||
|
|
||||||
theBufferList->mNumberBuffers = 1;
|
|
||||||
|
|
||||||
//pre-malloc buffers for AudioBufferLists
|
|
||||||
for ( UInt32 i = 0; i < theBufferList->mNumberBuffers; i++ )
|
|
||||||
{
|
|
||||||
theBufferList->mBuffers[i].mNumberChannels = 2;
|
|
||||||
theBufferList->mBuffers[i].mDataByteSize = bufferSizeBytes;
|
|
||||||
theBufferList->mBuffers[i].mData = malloc(bufferSizeBytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// (re-)initialize unit
|
|
||||||
if ( AudioUnitInitialize ( audioUnit ) )
|
|
||||||
{
|
|
||||||
throw CGenErr ( tr ( "Initialization of CoreAudio failed" ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
// init base class
|
// init base class
|
||||||
CSoundBase::Init ( iCoreAudioBufferSizeMono );
|
CSoundBase::Init ( iCoreAudioBufferSizeMono );
|
||||||
|
@ -258,7 +226,30 @@ for ( UInt32 i = 0; i < theBufferList->mNumberBuffers; i++ )
|
||||||
// create memory for intermediate audio buffer
|
// create memory for intermediate audio buffer
|
||||||
vecsTmpAudioSndCrdStereo.Init ( iCoreAudioBufferSizeStero );
|
vecsTmpAudioSndCrdStereo.Init ( iCoreAudioBufferSizeStero );
|
||||||
|
|
||||||
return iNewPrefMonoBufferSize;
|
|
||||||
|
// TEST
|
||||||
|
printf ( "Buffer_Size: %d", (int) bufferSizeFrames );
|
||||||
|
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
// fill audio unit buffer struct
|
||||||
|
theBufferList = (AudioBufferList*) malloc ( offsetof ( AudioBufferList,
|
||||||
|
mBuffers[0] ) + sizeof(AudioBuffer) );
|
||||||
|
|
||||||
|
|
||||||
|
theBufferList->mNumberBuffers = 1;
|
||||||
|
theBufferList->mBuffers[0].mNumberChannels = 2; // stereo
|
||||||
|
theBufferList->mBuffers[0].mDataByteSize = bufferSizeFrames * 4; // 2 bytes, 2 channels
|
||||||
|
theBufferList->mBuffers[0].mData = &vecsTmpAudioSndCrdStereo[0];
|
||||||
|
//theBufferList->mBuffers[0].mData = malloc(bufferSizeBytes);
|
||||||
|
|
||||||
|
// initialize unit
|
||||||
|
if ( AudioUnitInitialize ( audioUnit ) )
|
||||||
|
{
|
||||||
|
throw CGenErr ( tr ( "Initialization of CoreAudio failed" ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
return iCoreAudioBufferSizeMono;
|
||||||
}
|
}
|
||||||
|
|
||||||
OSStatus CSound::process ( void* inRefCon,
|
OSStatus CSound::process ( void* inRefCon,
|
||||||
|
@ -270,14 +261,6 @@ OSStatus CSound::process ( void* inRefCon,
|
||||||
{
|
{
|
||||||
CSound* pSound = reinterpret_cast<CSound*> ( inRefCon );
|
CSound* pSound = reinterpret_cast<CSound*> ( inRefCon );
|
||||||
|
|
||||||
// TEST
|
|
||||||
for ( UInt32 channel = 0; channel < theBufferList->mNumberBuffers; channel++)
|
|
||||||
{
|
|
||||||
memset ( theBufferList->mBuffers[channel].mData, 0,
|
|
||||||
theBufferList->mBuffers[channel].mDataByteSize );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// get the new audio data
|
// get the new audio data
|
||||||
ComponentResult err =
|
ComponentResult err =
|
||||||
AudioUnitRender ( pSound->audioUnit,
|
AudioUnitRender ( pSound->audioUnit,
|
||||||
|
@ -287,15 +270,6 @@ for ( UInt32 channel = 0; channel < theBufferList->mNumberBuffers; channel++)
|
||||||
inNumberFrames,
|
inNumberFrames,
|
||||||
theBufferList );
|
theBufferList );
|
||||||
|
|
||||||
// TEST
|
|
||||||
for ( int test = 0; test < theBufferList->mBuffers[0].mDataByteSize / 2; test++ )
|
|
||||||
{
|
|
||||||
fprintf ( pFile, "%d\n",
|
|
||||||
static_cast<short*>(theBufferList->mBuffers[0].mData)[test]);
|
|
||||||
}
|
|
||||||
fflush ( pFile );
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// TEST
|
// TEST
|
||||||
//static FILE* pFile = fopen ( "test.dat", "w" );
|
//static FILE* pFile = fopen ( "test.dat", "w" );
|
||||||
|
@ -308,29 +282,11 @@ fflush ( pFile );
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// TEST output seems to work!!!
|
// TEST output seems to work!!!
|
||||||
for ( UInt32 i = 1; i < ioData->mBuffers[0].mDataByteSize / 2; i++)
|
for ( UInt32 i = 1; i < theBufferList->mBuffers[0].mDataByteSize / 2; i++)
|
||||||
{
|
{
|
||||||
static_cast<short*> ( ioData->mBuffers[0].mData)[i] = (short) ( (double) rand() / RAND_MAX * 10000 );
|
static_cast<short*> ( theBufferList->mBuffers[0].mData)[i] = (short) ( (double) rand() / RAND_MAX * 10000 );
|
||||||
}
|
}
|
||||||
*/
|
ioData = theBufferList;
|
||||||
|
|
||||||
/*
|
|
||||||
// TEST
|
|
||||||
static FILE* pFile = fopen ( "test.dat", "w" );
|
|
||||||
fprintf ( pFile, "%d %d %d\n", ioActionFlags, inNumberFrames, err);
|
|
||||||
fflush ( pFile );
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
// TEST
|
|
||||||
static FILE* pFile = fopen ( "test.dat", "w" );
|
|
||||||
for ( int test = 0; test < theBufferList->mBuffers[0].mDataByteSize / 4; test++ )
|
|
||||||
{
|
|
||||||
fprintf ( pFile, "%e %d %d\n", static_cast<float*>(theBufferList->mBuffers[0].mData)[test],
|
|
||||||
static_cast<short*>(theBufferList->mBuffers[0].mData)[test],
|
|
||||||
static_cast<short*> ( ioData->mBuffers[0].mData)[test]);
|
|
||||||
}
|
|
||||||
fflush ( pFile );
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// TEST
|
// TEST
|
||||||
|
@ -338,71 +294,13 @@ printf ( "buffersize: %d, inBus: %d, ioActionFlags: %d, err: %d\n",
|
||||||
(int) inNumberFrames, (int) inBusNumber, (int) ioActionFlags, (int) err );
|
(int) inNumberFrames, (int) inBusNumber, (int) ioActionFlags, (int) err );
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
for ( UInt32 channel = 1; channel < ioData->mNumberBuffers; channel++)
|
|
||||||
{
|
|
||||||
memcpy ( ioData->mBuffers[channel].mData,
|
|
||||||
ioData->mBuffers[0].mData,
|
|
||||||
ioData->mBuffers[0].mDataByteSize );
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
// get input data pointer
|
|
||||||
jack_default_audio_sample_t* in_left =
|
|
||||||
(jack_default_audio_sample_t*) jack_port_get_buffer (
|
|
||||||
pSound->input_port_left, nframes );
|
|
||||||
|
|
||||||
jack_default_audio_sample_t* in_right =
|
|
||||||
(jack_default_audio_sample_t*) jack_port_get_buffer (
|
|
||||||
pSound->input_port_right, nframes );
|
|
||||||
|
|
||||||
// copy input data
|
|
||||||
for ( i = 0; i < pSound->iJACKBufferSizeMono; i++ )
|
|
||||||
{
|
|
||||||
pSound->vecsTmpAudioSndCrdStereo[2 * i] = (short) ( in_left[i] * _MAXSHORT );
|
|
||||||
pSound->vecsTmpAudioSndCrdStereo[2 * i + 1] = (short) ( in_right[i] * _MAXSHORT );
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
// TEST
|
|
||||||
for ( int i = 0; i < inNumberFrames; i++ )
|
|
||||||
{
|
|
||||||
pSound->vecsTmpAudioSndCrdStereo[2 * i] = static_cast<SInt16*>(ioData->mBuffers[0].mData)[i];
|
|
||||||
pSound->vecsTmpAudioSndCrdStereo[2 * i + 1] = static_cast<SInt16*>(ioData->mBuffers[0].mData)[i];
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
// call processing callback function
|
// call processing callback function
|
||||||
pSound->ProcessCallback ( pSound->vecsTmpAudioSndCrdStereo );
|
pSound->ProcessCallback ( pSound->vecsTmpAudioSndCrdStereo );
|
||||||
|
|
||||||
/*
|
|
||||||
// get output data pointer
|
|
||||||
jack_default_audio_sample_t* out_left =
|
|
||||||
(jack_default_audio_sample_t*) jack_port_get_buffer (
|
|
||||||
pSound->output_port_left, nframes );
|
|
||||||
|
|
||||||
jack_default_audio_sample_t* out_right =
|
|
||||||
(jack_default_audio_sample_t*) jack_port_get_buffer (
|
|
||||||
pSound->output_port_right, nframes );
|
|
||||||
|
|
||||||
// copy output data
|
// TODO take care of outputting processed data
|
||||||
for ( i = 0; i < pSound->iJACKBufferSizeMono; i++ )
|
|
||||||
{
|
|
||||||
out_left[i] = (jack_default_audio_sample_t)
|
|
||||||
pSound->vecsTmpAudioSndCrdStereo[2 * i] / _MAXSHORT;
|
|
||||||
|
|
||||||
out_right[i] = (jack_default_audio_sample_t)
|
|
||||||
pSound->vecsTmpAudioSndCrdStereo[2 * i + 1] / _MAXSHORT;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
/*
|
|
||||||
ioData->mBuffers[0].mDataByteSize = 2048;
|
|
||||||
ioData->mBuffers[0].mData = lbuf;
|
|
||||||
ioData->mBuffers[0].mNumberChannels = 1;
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
return noErr;
|
return noErr;
|
||||||
|
|
Loading…
Add table
Reference in a new issue