forked from flarn2006/MiscPrograms
/
playnotes.c
107 lines (92 loc) · 2.32 KB
/
playnotes.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// playnotes.c - Plays a string of notes given on the command line.
// Created by flarn2006 - http://flarn2006.blogspot.com/
// Licensed under Creative Commons Attribution-ShareAlike 3.0 Unported
// Requires libao (http://xiph.org/ao/) - Compile with "-lao -lm"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <string.h>
#include <ao/ao.h>
#define SAMPLE_RATE 44100
#define NOTE_LENGTH 0.180
double tofreq(char note);
void play_note(ao_device *device, double freq, double duration)
{
unsigned long i;
size_t length = SAMPLE_RATE * duration;
int16_t *audio = malloc(length*2);
for (i=0; i<length; i++)
{
double value = sin(i * 2 * M_PI * (freq / (double)SAMPLE_RATE));
audio[i] = (int16_t)(value * 0x7FFF);
}
ao_play(device, (char*)audio, length*2);
free(audio);
}
int main(int argc, char *argv[])
{
ao_device *device;
ao_sample_format fmt;
double duration = NOTE_LENGTH;
float octave_mult = 1.0f;
if (argc == 2); // do nothing
else if (argc == 3)
{
sscanf(argv[2], "%lf", &duration);
if (duration < 0)
{
printf("Note length can't be negative!\n");
return 1;
}
}
else
{
printf("Usage: %s notes [length]\n", argv[0]);
return 3;
}
fmt.bits = 16;
fmt.rate = SAMPLE_RATE;
fmt.channels = 1;
fmt.byte_format = AO_FMT_NATIVE;
fmt.matrix = NULL;
ao_initialize();
device = ao_open_live(ao_default_driver_id(), &fmt, NULL);
if (!device) {
printf("Error %d opening audio device!\n", errno);
return 2;
}
int i; for (i=0; i<strlen(argv[1]); i++)
{
char ch = argv[1][i];
char capital;
switch (ch)
{
case '-': octave_mult *= 0.5f; break;
case '+': octave_mult *= 2.0f; break;
default:
capital = ('A' <= ch && ch <= 'G');
if (capital) octave_mult *= 2.0f;
play_note(device, tofreq(ch) * octave_mult, duration);
if (capital) octave_mult *= 0.5f;
break; //not needed, but good practice
}
}
ao_close(device);
ao_shutdown();
return 0;
}
double tofreq(char note)
{
switch (note)
{
case 'C': case 'c': return 261.63; break;
case 'D': case 'd': return 293.66; break;
case 'E': case 'e': return 329.63; break;
case 'F': case 'f': return 349.23; break;
case 'G': case 'g': return 392.00; break;
case 'A': case 'a': return 440.00; break;
case 'B': case 'b': return 493.88; break;
default: return 0.00; break; //rest
}
}