-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
153 lines (128 loc) · 3.91 KB
/
main.cpp
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <SDL/SDL.h>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <utility>
#include <sndfile.h>
#include "MidiFile.h"
#include "Midi.h"
#include "MidiDevice.h"
#include "ChannelEvent.h"
#include "synth/synth.h"
#define PLAYER
static bool done_playing = false;
static unsigned int fill_audio_pos = 0;
void fill_audio(void *udata, Uint8 *stream, int len);
static int init_audio(){
SDL_AudioSpec wanted;
wanted.freq = g_samplerate;
wanted.format = AUDIO_S16;
wanted.channels = 2; /* 1 = mono, 2 = stereo */
wanted.samples = 8192; /* Good low-latency value for callback */
wanted.callback = fill_audio;
wanted.userdata = NULL;
/* Open the audio device, forcing the desired format */
if ( SDL_OpenAudio(&wanted, NULL) < 0 ) {
fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
return(-1);
}
return(0);
}
/* SDL Audio callback. Currently just playing our audio which is generated offline.
* A proper player should probably parse the stream and mix in realtime together with a working thread */
void fill_audio(void *udata, Uint8 *stream, int len){
int samplesize = 4; //16 -bit samples, stereo
int num_samples = len / samplesize;
stereo_sample_interleaved* sampleBuffer = (stereo_sample_interleaved*)stream;
unsigned int remaining = audio_data.size() - fill_audio_pos;
if(remaining >= num_samples){
for(int i = 0; i < num_samples; i++){
short sample = audio_data[fill_audio_pos + i];
sampleBuffer[i].left = sample;
sampleBuffer[i].right = sample;
}
fill_audio_pos += num_samples;
} else {
memset(stream, 0, len);
done_playing = true;
}
}
/* Dump the song as 16-bit Mono, 44100hz */
void dump(const std::string& name){
SNDFILE *file;
SF_INFO sfinfo;
int k ;
memset (&sfinfo, 0, sizeof (sfinfo)) ;
sfinfo.samplerate = g_samplerate;
sfinfo.frames = audio_data.size();
sfinfo.channels = 1 ;
sfinfo.format = (SF_FORMAT_WAV | SF_FORMAT_PCM_16);
if (! (file = sf_open (name.c_str(), SFM_WRITE, &sfinfo))){
throw std::runtime_error("Error : Not able to open output file.");
}
sf_writef_short (file, &audio_data[0], audio_data.size());
sf_close (file) ;
}
void textdump(){
std::shared_ptr<Midi> midi = f->data();
Midi::MidiEventArray evts = midi->getAllEvents();
unsigned int tempo = midi->getTicksPerSecond();
size_t i = 0;
while(i < evts.size()){
unsigned int tick = evts[i]->getTimestamp();
unsigned int channel = evts[i]->getChannelIndex();
float t = (float)tick / (float)tempo;
printf("Time %f, tick %u, index %zu, channel %u: ", t, tick, i, channel);
evts[i]->print();
i++;
}
}
void usage(const std::string& program_name){
printf("Usage: %s <midi file> <output .wav file>\n", program_name.c_str());
}
int main(int argc, char* argv[]) {
#if 0
if(argc != 3){
usage(argv[0]);
return 0;
}
try {
f = std::move(std::unique_ptr<MidiFile>(new MidiFile(argv[1])));
} catch(const std::exception& ex){
printf("Error when parsing midi file: %s\n", ex.what());
return 1;
}
if(!f) return 1;
std::shared_ptr<Midi> midi = f->data();
// Generate tempered scale frequencies
generate_tone_table();
generate_song();
textdump();
dump(argv[2]);
#ifdef PLAYER
SDL_Init(SDL_INIT_AUDIO);
if(init_audio()){
SDL_Quit();
return 1;
}
printf("Running..\n");
SDL_PauseAudio(0);
while(!done_playing){}
SDL_PauseAudio(1);
SDL_CloseAudio();
SDL_Quit();
#endif
#else
MidiDevice dev;
try {
dev.open("default");
while(1){
std::shared_ptr<MidiEvent> ev = dev.read();
}
} catch(std::exception& ex){
printf("Exception in main: %s\n", ex.what());
}
#endif
return 0;
}