Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: simplest way to write code to make sounds?

4 views
Skip to first unread message
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

des...@verizon.net

unread,
Dec 24, 2009, 12:54:22 PM12/24/09
to
unruh <un...@wormhole.physics.ubc.ca> writes:

> On 2009-12-24, des...@verizon.net <des...@verizon.net> wrote:
>> The Natural Philosopher <t...@invalid.invalid> writes:
>>
>>> For a debian linux system.
>>>
>>> I don't want the syntesizer bit..can take care of all that..just how
>>> to hook C or indeed any other language into making a noise come out of
>>> the speakers.
>>
>> For C:
>>
>> system("aplay sound.wav");
>
> Which of course means that sound.wav has to already exist. This would be
> pretty useless for playing, in real time, a sound that you had
> synthesised. Synthesise the sound. Write it out to a .wav file, then run
> aplay on it.

Is that a requirement?

From the aplay man page:

If filename is not specified, the standard output or input is
used. The aplay utility accepts multiple filenames.

So it looks like aplay would be happy at the receiving end of a pipe.

Message has been deleted
Message has been deleted

The Natural Philosopher

unread,
Dec 25, 2009, 4:56:32 AM12/25/09
to
pk wrote:
> The Natural Philosopher wrote:
>
>> pk wrote:

>>> The Natural Philosopher wrote:
>>>
>>>> For a debian linux system.
>>>>
>>>> I don't want the syntesizer bit..can take care of all that..just how to
>>>> hook C or indeed any other language into making a noise come out of the
>>>> speakers.
>>>>
>>>> Ideally would be a fragment of code that simply makes any noise on my
>>>> ALSA based sound system.
>>>>
>>>> The only bit I found segfaults when it opens device 'default'
>>> How about
>>>
>>> anything_that_produces_some_output > /dev/dsp
>>>
>>> (the noise that comes out of it is not very pretty though)
>> OK, can you ( haha ) AMPLIFY on that a little?
>>
>>
>> If I feed a bunch of bytes to that, are they simply buffered and spat
>> out as a sequence of binary audio levels at 44Khz?
>>
>> If so, its just what I need.
>
> Not sure about the 44Khz part, but yeah, it just takes whatever you throw at
> it as if it were something to play.
>
> About the "device or resource busy" thing, not sure, but it might be that
> the device is being used exclusively by some process in the system
> (typically things like arts, or gstreamer I think). At least I seem to
> remember I had some similar problem in the past, although unfortunately I
> can't really help you more.


Ok. I downloaded two packages. Both use this to open a sound device in alsa.

if ((err = snd_pcm_open (&playback_handle,"default",
SND_PCM_STREAM_PLAYBACK, 0)) < 0)

Both compile and link correctly. One runs, the other segfaults at this
point.

I conclude they aren't linking the same libraries..


..ah. I found it. One had a later call to a library function that had
changed from accepting a value, to a pointer.

I have sounds.

The Natural Philosopher

unread,
Dec 25, 2009, 5:24:13 AM12/25/09
to
In case anyone has the same problem, here is some code that opens a
sound channel to ALSA. I didn't write it, I found it.


#define ALSA_PCM_NEW_HW_PARAMS_API
#include <alsa/asoundlib.h>

snd_pcm_t *init_sound()
{
int rc;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
unsigned int val;
int dir;
snd_pcm_uframes_t frames;

/* Open PCM device for playback. */
rc = snd_pcm_open(&handle, "default",
SND_PCM_STREAM_PLAYBACK, 0);
if (rc < 0) {
fprintf(stderr,
"unable to open pcm device: %s\n",
snd_strerror(rc));
exit(1);
}

/* Allocate a hardware parameters object. */
snd_pcm_hw_params_alloca(&params);

/* Fill it in with default values. */
snd_pcm_hw_params_any(handle, params);

/* Set the desired hardware parameters. */

/* Interleaved mode */
snd_pcm_hw_params_set_access(handle, params,
SND_PCM_ACCESS_RW_INTERLEAVED);

/* Signed 16-bit little-endian format */
snd_pcm_hw_params_set_format(handle, params,
SND_PCM_FORMAT_S16_LE);

/* Two channels (stereo) */
snd_pcm_hw_params_set_channels(handle, params, 2);

/* 44100 bits/second sampling rate (CD quality) */
val = (SAMPLE_FREQ);
snd_pcm_hw_params_set_rate_near(handle, params,
&val, &dir);

/* Set period size to 1 frames. */
frames = 100000;
snd_pcm_hw_params_set_period_size_near(handle,
params, &frames, &dir);

/* Write the parameters to the driver */
rc = snd_pcm_hw_params(handle, params);
if (rc < 0) {
fprintf(stderr,
"unable to set hw parameters: %s\n",
snd_strerror(rc));
exit(1);
}

/* Use a buffer large enough to hold one period */
snd_pcm_hw_params_get_period_size(params, &frames,&dir);
/* We want to loop for 5 seconds */
snd_pcm_hw_params_get_period_time(params,&val, &dir);
return(handle);
}

To write a stereo pair of identical short words to it

short value;
int stereo;

stereo=(value<<16) +value;
snd_pcm_writei(handle, &stereo,1);


This works in a loop. I.e. if value is set to e.g. the integer of sine
(loop counter) you get a tone out.

Perhaps I should have said I wanted a quick'n'dirty way to make noises
for a possible sound effects board using a cheap microcontroller: First
I need to develop the effect algorithms. PICS and the like do not have
enough RAM to hold massive samples or .wavs. So algorithmic generation
is what I needed.

Once the sound is good enough, its down to PIC or equivalent with
adequate processing power to generate it.

It's been a long time since I wrote 'C' in any seriousness. Boy I am rusty..

Message has been deleted
0 new messages