Skip to content

Instantly share code, notes, and snippets.

@jblang

jblang/music.c Secret

Last active Jul 3, 2019
Embed
What would you like to do?
SN76489 music player
/*
SN76489 music player
Uses GW-BASIC PLAY macro syntax:
https://hwiegman.home.xs4all.nl/gw-man/PLAY.html
Runs on PCjr, Tandy, and CP/M using my SN76489 sound board for RC2014.
Supported Compilers:
- Turbo C (DOS)
- Open Watcom (DOS) https://github.com/open-watcom/open-watcom-v2
- z88dk (CP/M) https://github.com/z88dk/z88dk
Currently only supports one voice, but 3-voice support is planned.
Copyright 2019 J.B. Langston
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
/* update these values for other systems */
#define muxport 0x61 /* port to control audio mux on PCjr */
#define psgport 0xc0 /* port to control SN76489 PSG on PCjr */
/*#define psgport 0xfe*/ /* port to control SN76489 PSG on RC2014 */
#define refclk 3579000 /* input clock rate for PSG on PCjr */
#define tickhz 60
typedef unsigned char uint8_t;
typedef unsigned int uint16_t;
#ifdef __TURBOC__
#define outp outportb
#define inp inportb
#endif
void setmux()
{
outp(muxport, inp(muxport) | 0x60);
}
/* set volume attenuation for channel */
uint8_t vol(uint8_t channel, uint8_t value)
{
return 0x90 | ((channel & 0x3) << 5) | (value & 0xf);
}
void setvol(uint8_t channel, uint8_t value)
{
outp(psgport, vol(channel, value));
}
void mute()
{
int i;
for (i = 0; i < 4; i++)
setvol(i, 0xf);
}
/* set tone half period for channel */
uint8_t tonelo(uint8_t channel, uint16_t value)
{
return 0x80 | ((channel & 0x3) << 5) | (value & 0xf);
}
uint8_t tonehi(uint16_t value)
{
return (value >> 4) & 0x3f;
}
void settone(uint8_t channel, uint16_t value)
{
outp(psgport, tonelo(channel, value));
if (channel < 3)
outp(psgport, tonehi(value));
}
/* calculate tone half period from frequency */
uint16_t fromfreq(float value)
{
int tone = refclk / (32 * value);
if (tone < 1)
tone = 1;
else if (tone > 1023)
tone = 1023;
return tone;
}
uint16_t scale[84];
/* build an even-tempered scale from C2 to B8
See: https://en.wikipedia.org/wiki/Twelfth_root_of_two */
void buildscale()
{
float next = 65.40639133; /* start at C2 */
int i;
for (i = 0; i < 84; i++) {
scale[i] = fromfreq(next);
next *= 1.059463094359; /* increase by 12th root of 2 */
}
}
/* parse a number from the macro string */
int parsenum(char *input, int *output)
{
int len = 0;
int num = 0;
while (*input >= '0' && *input <= '9') {
num = 10 * num + *input - '0';
len++;
input++;
}
if (len > 0)
*output = num;
return len;
}
struct command {
uint8_t value;
uint8_t delay;
struct command *next;
};
/* compile a music macro string using GW-BASIC syntax */
struct command *compile(char *macro, uint8_t channel)
{
struct command *start = NULL;
struct command *temp = NULL;
int pitch = 0;
int fraction = 4;
int tempfraction = 4;
float articulation = 7.0 / 8.0;
int octave = 4;
int tempo = 120;
int volume = 8;
int parselen = 0;
float duration = 0;
char c;
/* Start list with a dummy command to make traversal logic easier */
start = temp = malloc(sizeof(struct command));
/* compile music expression into array of pokes and delays */
while (*macro) {
c = toupper(*macro++);
parselen = 0;
if ((c >= 'A' && c <= 'G') || c == 'N') {
if (c == 'N') {
/* note specified as number */
if (!(parselen = parsenum(macro, &pitch)))
printf("Error parsing pitch.\n");
macro += parselen;
} else {
/* note specified by name */
switch (c) {
case 'C':
pitch = 1;
break;
case 'D':
pitch = 3;
break;
case 'E':
pitch = 5;
break;
case 'F':
pitch = 6;
break;
case 'G':
pitch = 8;
break;
case 'A':
pitch = 10;
break;
case 'B':
pitch = 12;
break;
}
if (*macro == '-') {
/* flat */
pitch--;
macro++;
} else if (*macro == '+' || *macro == '#') {
/* sharp */
pitch++;
macro++;
}
pitch += octave * 12;
}
/* set length */
if (!(parselen = parsenum(macro, &tempfraction)))
tempfraction = fraction;
duration = (60.0 / tempo) * (4.0 / tempfraction) * tickhz;
macro += parselen;
while (*macro == '.') {
duration *= 3.0 / 2.0;
macro++;
}
if (pitch > 0)
{
temp = temp->next = malloc(sizeof(struct command));
temp->value = tonelo(channel, scale[pitch]);
temp->delay = 0;
temp = temp->next = malloc(sizeof(struct command));
temp->value = tonehi(scale[pitch]);
temp->delay = 0;
temp = temp->next = malloc(sizeof(struct command));
temp->value = vol(channel, 15 - volume);
temp->delay = duration * articulation;
temp = temp->next = malloc(sizeof(struct command));
temp->value = vol(channel, 15);
temp->delay = duration * (1.0 - articulation);
}
} else {
switch(c) {
case 'L':
/* set note length */
if (!(parselen = parsenum(macro, &fraction)))
printf("Error parsing fraction.\n");
macro += parselen;
break;
case 'M':
/* set articulation style */
switch (toupper(*macro)) {
case 'L':
/* legato */
articulation = 1;
break;
case 'S':
/* staccato */
articulation = 3.0 / 4.0;
break;
case 'N':
/* normal */
articulation = 7.0 / 8.0;
break;
default:
printf("Error parsing articulation.\n");
break;
}
macro++;
break;
case 'O':
/* set octave */
if (!(parselen = parsenum(macro, &octave)))
printf("Error parsing octave.\n");
macro += parselen;
break;
case 'P':
/* rest */
if (!(parselen = parsenum(macro, &tempfraction)))
tempfraction = fraction;
macro += parselen;
duration = (60.0 / tempo) * (4.0 / tempfraction) * tickhz;
temp = temp->next = malloc(sizeof(struct command));
temp->value = vol(channel, 15);
temp->delay = duration;
break;
case 'T':
/* set tempo */
if (!(parselen = parsenum(macro, &tempo)))
printf("Error parsing tempo.\n");
macro += parselen;
break;
case 'V':
/* set volume */
if (!(parselen = parsenum(macro, &volume)))
printf("Error parsing volume.\n");
macro += parselen;
break;
case '>':
/* increase octave */
if (octave < 6)
octave++;
break;
case '<':
/* decrease octave */
if (octave > 0)
octave--;
break;
default:
printf("Encountered unexpected character %x %c\n", c, c);
break;
}
}
}
/* Free dummy command at start of list */
temp->next = NULL;
temp = start;
start = start->next;
free(temp);
return start;
}
void freelist(struct command *list)
{
struct command *temp;
while (list != NULL) {
temp = list->next;
free(list);
list = temp;
}
}
/* play compiled song */
void play(struct command *voices[])
{
uint8_t ticks[] = {0, 0, 0, 0};
uint8_t v;
uint8_t done = 0;
delay(100); /* calibrate delay */
while(!done)
{
done = 1;
for (v = 0; v < 3; v++)
{
while (voices[v] != NULL && ticks[v] == 0) {
outp(psgport, voices[v]->value);
if (voices[v]->delay > 0)
ticks[v] = voices[v]->delay;
voices[v] = voices[v]->next;
done = 0;
}
if (ticks[v] > 0) {
ticks[v]--;
done = 0;
}
}
delay(1000 / tickhz);
}
mute();
}
int main (int argc, char *argv[])
{
struct command *voices[3];
int i;
buildscale();
setmux(); /* comment out on RC2014 */
mute();
if (argc == 1) {
printf("usage: %s <voice> [<voice>...]\n", argv[0]);
return -1;
}
for (i = 0; i < 3; i++)
if (i < argc - 1)
voices[i] = compile(argv[i+1], i);
else
voices[i] = NULL;
play(voices);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.