Re: [linux-audio-dev] weird sndlib problems

New Message Reply About this list Date view Thread view Subject view Author view Other groups

Subject: Re: [linux-audio-dev] weird sndlib problems
From: Conrad Parker (conrad_AT_vergenet.net)
Date: Thu Aug 08 2002 - 09:15:47 EEST


Hi,

I'm not familiar with sndlib but I've had a quick look at your code below.
You seem to be using different ways of working out how big the read buffer
is when allocating and when reading, and you don't seem to be taking the
number of channels into account.

eg. you create an input buffer (ibuf) of 'buffer_size' shorts:

> ibuf = (short *)calloc(buffer_size, sizeof(short));

but on each read, you read 'bytes_per_read' bytes into it:

> err = mus_audio_read(afd, (char *)ibuf, bytes_per_read);

where bytes_per_read has been defined earlier in terms of the audio format.

This will not work if (buffer_size * sizeof(short)) < bytes_per_read,
ie. if you're not allocating enough space for the reads. It's probably
better if you allocate exactly as much as you're going to read, ie:

        ibuf = (short *)malloc(bytes_per_read);

hope that helps,

Conrad.

On Wed, Aug 07, 2002 at 05:30:46PM +0530, Tapan S. Parikh wrote:
>
> Im having a weird problem w/ using sndlib. I am calling some
> functions to record audio to a file from the mic. But there is some
> kind of weird register spill happening that is causing some local
> pointer variables to be reset and screwed up.
>
> I have a feeling this maybe thread-related? See the following gdb
> session that shows some local vars getting screwed up.
> aihbox_record_audio has some code which records audio much like
> sndrecord.c. The code for that also follows. The type of the screwed
> up vars is AIHBox* for aihbox and gpointer for user_data.
>
> Any idea what could cause this? Im using gcc on RH Linux 7.2 on a
> Pentium 3.
>
> <gdbsession-snip>
> (gdb) print aihbox
> $1 = (AIHBox *) 0x81bbc80
> (gdb) print filename
> $2 = (gchar *) 0x81c2d18 "1028720319.wav"
> (gdb) print result
> $3 = {tv_sec = 1028720319, tv_usec = 83798}
> (gdb) print user_data
> $4 = 0x81bbc80
> (gdb) next
> 214 aihbox_record_audio (filename, 100, 50, 4096);
> (gdb) print user_data
> $5 = 0x3f800000
> (gdb) print result
> $6 = {tv_sec = 1028720319, tv_usec = 83798}
> (gdb) print aihbox
> $7 = (AIHBox *) 0x3f800000
> (gdb) print filename
> $8 = (gchar *) 0x81c2d18 "1028720319.wav"
> </gdbsession-snip>
>
> <code-snip>
> void
> aihbox_record_audio(gchar* filename, int buffers, int num_reads, int
> buffer_size)
> {
> int fd, afd, i, err, bytes_per_sample, bytes_per_read;
> float mic_gain[1];
> short *ibuf;
>
> afd = -1;
> mus_sound_initialize();
>
> // determine num reads per sec based on sampling rate
>
>
> /* make sure the microphone is on */
> mic_gain[0] = 1.0;
> mic_gain[1] = 1.0;
>
> mus_audio_mixer_write(MUS_AUDIO_MICROPHONE, MUS_AUDIO_AMP, 0,
> mic_gain);
> if (CHANNELS == 2) mus_audio_mixer_write(MUS_AUDIO_MICROPHONE,
> MUS_AUDIO_AMP, 1, mic_gain);
>
> /* open the output sound file */
> bytes_per_sample = mus_data_format_to_bytes_per_sample(DATA_TYPE);
> bytes_per_read = buffer_size * bytes_per_sample;
> fd = mus_sound_open_output(filename, SAMPLING_RATE, CHANNELS,
> DATA_TYPE, FILE_TYPE, "created by sndrecord");
>
> //fd = mus_sound_open_output(filename, 22050, 2, MUS_LSHORT,
> MUS_RIFF, "created by sndlib");
>
> if (fd != -1)
> {
> ibuf = (short *)calloc(buffer_size, sizeof(short));
> afd = mus_audio_open_input(MUS_AUDIO_MICROPHONE, SAMPLING_RATE,
> CHANNELS, DATA_TYPE, bytes_per_read);
> if (afd != -1)
> {
> for (i = 0; i < num_reads; i++)
> {
> err = mus_audio_read(afd, (char *)ibuf, bytes_per_read);
> if (err != MUS_NO_ERROR) break;
> write(fd, ibuf, bytes_per_read);
> }
> mus_audio_close(afd);
> }
> else
> {
> printf ("Error opening audio input device.\n");
> }
> mus_sound_close_output(fd, bytes_per_read * num_reads);
> free(ibuf);
> }
> else
> {
> printf ("Error opening audio input device.\n");
> }
> }
> </code-snip>
>


New Message Reply About this list Date view Thread view Subject view Author view Other groups

This archive was generated by hypermail 2b28 : Thu Aug 08 2002 - 09:21:20 EEST