From: Eli Cohen <echoline@gmail.com>
To: 9front@9front.org
Subject: Re: [9front] beep.c
Date: Mon, 5 Apr 2021 08:51:04 -0700 [thread overview]
Message-ID: <CAHwi9bz0+N6smaqFQk7dV25vx1wQFGeCe2G9trDWvsfYtYJjHg@mail.gmail.com> (raw)
In-Reply-To: <FEF9713B-2AAE-4F69-A4D2-CCCA8ECE01DD@quintile.net>
[-- 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);
}
prev parent reply other threads:[~2021-04-05 17:34 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-04-05 9:03 Steve Simon
2021-04-05 15:51 ` Eli Cohen [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAHwi9bz0+N6smaqFQk7dV25vx1wQFGeCe2G9trDWvsfYtYJjHg@mail.gmail.com \
--to=echoline@gmail.com \
--cc=9front@9front.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).