9front - general discussion about 9front
 help / color / mirror / Atom feed
* Re: [9front] beep.c
@ 2021-04-05  9:03 Steve Simon
  2021-04-05 15:51 ` Eli Cohen
  0 siblings, 1 reply; 2+ messages in thread
From: Steve Simon @ 2021-04-05  9:03 UTC (permalink / raw)
  To: 9front

hi,

i would suggest (on principal) creating a scaled sin() lookup table rather than calling the transcendental function each loop.

Go n-éirghidh an bóthar libh, a chairde

-Steve




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [9front] beep.c
  2021-04-05  9:03 [9front] beep.c Steve Simon
@ 2021-04-05 15:51 ` Eli Cohen
  0 siblings, 0 replies; 2+ messages in thread
From: Eli Cohen @ 2021-04-05 15:51 UTC (permalink / raw)
  To: 9front

[-- Attachment #1: Type: text/plain, Size: 291 bytes --]

:)

On Mon, Apr 5, 2021 at 2:08 AM Steve Simon <steve@quintile.net> wrote:
>
> hi,
>
> i would suggest (on principal) creating a scaled sin() lookup table rather than calling the transcendental function each loop.
>
> Go n-éirghidh an bóthar libh, a chairde
>
> -Steve
>
>
>

[-- Attachment #2: beep.c --]
[-- Type: text/plain, Size: 1161 bytes --]

// produce waveform for a "beep"
#include <u.h>
#include <libc.h>

void
usage(void)
{
	fprint(2, "usage: %s [-f hz] [-d decay] [-l samples] [-s slide] [-v volume]\n", argv0);
	exits("usage");
}

void
main(int argc, char **argv)
{
	char *buffer;
	short s;
	int i;
	double hz = 440;
	double decay = 0.9998;
	int samples = 11025;
	double slide = 1.0001;
	double volume = 0.33;
	double *table;

	ARGBEGIN{
	case 'f':
		hz = atof(EARGF(usage()));
		break;
	case 'd':
		decay = atof(EARGF(usage()));
		break;
	case 'l':
		samples = atoi(EARGF(usage()));
		break;
	case 's':
		slide = atof(EARGF(usage()));
		break;
	case 'v':
		volume = atof(EARGF(usage()))/100.0;
		break;
	default:
		usage();
	}ARGEND

	volume = volume < 0? 0: volume > 1? 1: volume;

	table = malloc(6283 * sizeof(double));
	for (i = 0; i < 6283; i++) {
		table[i] = cos(i/1000.0);
	}

	buffer = malloc(samples * 4);

	for (i = 0; i < samples; i++) {
		s = table[(int)(fmod(((double)i/44100*2*PI*(hz-pow(slide,i))),6.283)*1000.0)] * 32767 * volume * pow(decay,i);
		buffer[i*4+0] = buffer[i*4+2] = s & 0xFF;
		buffer[i*4+1] = buffer[i*4+3] = (s >> 8) & 0xFF;
	}

	write(1, buffer, samples * 4);
}

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-04-05 17:34 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-05  9:03 [9front] beep.c Steve Simon
2021-04-05 15:51 ` Eli Cohen

9front - general discussion about 9front

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/9front

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 9front 9front/ http://inbox.vuxu.org/9front \
		9front@9front.org
	public-inbox-index 9front

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.9front


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git