Re: Sample-rate conversion
Re: Sample-rate conversion
- Subject: Re: Sample-rate conversion
- From: Franck Stauffer <email@hidden>
- Date: Wed, 4 Feb 2004 07:35:50 +0100
Hey jens,
I don't think averaging things the way you do it is safe. If you'd like
downsampling things properly i suggest you use something like cubic
spline interpolation. I guess the music-dsp
source code archive has a code that does this...
Le 4 fivr. 04, ` 06:31, Jens Bauer a icrit :
Hi all,
I just wrote a program, which needs to convert a sample-rate from
approx. 3.5MHz resolution to 44100Hz resolution.
The signal that is sampled, is in the hearable range, but I ran into a
problem that some of you most likely recognize.
When I have a "pure tone", and it's converted, I can hear another
tone, which is of a lower frequency, and the amplitude is somewhat
lower.
Is there an easy/simple way of getting rid of these extra tones ?
I pull around 79 or 80 samples from the 3.5MHz signal add them up,
averaging them by dividing them by # of samples read (say 80), then I
multiply them by the playback volume, and store them in the left+right
channel (mono).
The signal I am converting, is a square signal, which can be either hi
or lo.
Here's the most important part of my 'playback' code:
OSStatus playbackCallback(AudioDeviceID device, const AudioTimeStamp
*now, const AudioBufferList *input_data, const AudioTimeStamp
*input_time, AudioBufferList *output_data, const AudioTimeStamp
*output_time, void *client_data)
{
register int channels;
register int frames;
register float left;
register float right;
register float *new_data;
register float volume;
register MyUserInfo *userInfo;
register float samples;
register float ear;
register float countDown;
userInfo = (MyUserInfo *)client_data;
new_data = (float *)(output_data->mBuffers[0].mData);
channels = output_data->mBuffers[0].mNumberChannels;
frames = (output_data->mBuffers[0].mDataByteSize / sizeof(float)) /
channels;
volume = userInfo->volume;
countDown = userInfo->lastCountDown; // restore saved countDown value
while(frames--)
{
ear = 0.0;
samples = 80.0; // (never mind this default-value!)
if(userInfo->sampleRate > 0.0)
{
samples = (3500000.0 / userInfo->sampleRate);
}
countDown += samples;
samples = 0.0;
while(countDown-- >= 1.0)
{
samples++;
ear =+ ear_state(userInfo->tape) ? volume : -volume;
}
ear /= samples;
left = ear;
right = left;
*new_data++ = left;
*new_data++ = right;
}
userInfo->lastCountDown = countDown; // save fractional part of
countDown for next time
return(kAudioHardwareNoError);
}
Earlier, I used integers for the counters. I hoped to get rid of some
of the tones, by using floating point counters (to get the division
more precise), but I don't hear the big difference. :/ (Note:
countDown is saved for next time the callback is invoked)
I tried looking at the output it generates (by dumping it to a file),
and this looks nice (I haven't analyzed the frequencies, as I'm not
experienced enough for this yet).
After that, I tried to connect the audio out to the audio in jack
(speaker->microphone), and when I used an oscilloscope program on the
playback program, I got a minor shock. I didn't imagine that my data
could be *that* much distorted. A bunch of high frequencies are living
on the 'lo' and 'hi' edges.
I do not see these in the file I dumped.
There is a strange thing. I tried using Audio Hijack on the output
(before it gets into the oscilloscope), and using lowpass filters, I
got rid of very much noise. (This confuses me quite a bit, because
when I output pure data, how can Audio Hijack make them cleaner??? -Is
this because I output square waves rather than sine waves?)
Love,
Jens
_______________________________________________
coreaudio-api mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/coreaudio-api
Do not post admin requests to the list. They will be ignored.
_______________________________________________
coreaudio-api mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/coreaudio-api
Do not post admin requests to the list. They will be ignored.