* Wish I were a good writer.
[pineappletracker.git] / main.c
blobb3bc56cb3bcfde8a6511d176515e738abce243d8
1 /* vi:set ts=4 sts=4 sw=4 noexpandtab: */
2 #include <stdio.h>
3 #include <stdint.h>
5 #include <SDL/SDL.h>
7 #ifndef WINDOWS
8 #include <err.h>
9 #endif
11 #ifdef JACK
12 #include <jack/jack.h>
13 #endif
15 #include "pineapple.h"
16 #include "musicchip_file.h"
18 #define FREQ 48000
20 //void sdl_callbackbuffer(PT_TUNE *tune, Uint8 *buf, int len);
21 void sdl_callbackbuffer(void *userdata, Uint8 *buf, int len);
23 //PT_TUNE *tune;
25 #ifdef JACK
26 jack_nframes_t sr; // The current sample rate
27 jack_port_t *output_port;
28 typedef jack_default_audio_sample_t sample_t;
30 void j_error(const char *desc);
31 int j_process(jack_nframes_t nframes, void *arg);
32 int j_srate(jack_nframes_t nframes, void *arg);
33 void j_shutdown(void *arg);
34 #endif
37 /* initialize SDL audio */
38 u8 sdl_init(void){
39 SDL_AudioSpec requested, obtained;
41 fprintf(stderr, "Trying SDL....\n");
43 if(SDL_Init( SDL_INIT_AUDIO ) < 0){
44 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
45 return 1;
48 atexit(SDL_Quit);
50 requested.freq = FREQ;
51 //requested.freq = 16000;
52 //requested.format = AUDIO_S16SYS;
53 requested.format = AUDIO_U8;
54 requested.samples = 256;
55 requested.channels = 1;
56 requested.callback = sdl_callbackbuffer;
57 //requested.userdata = tune;
59 SDL_OpenAudio(&requested, &obtained);
61 fprintf(stderr, "freq %d\n", obtained.freq);
62 fprintf(stderr, "req. format %d\n", obtained.format);
63 fprintf(stderr, "obtained format %d\n", obtained.format);
64 fprintf(stderr, "samples %d\n", obtained.samples);
66 return 0;
69 /* called by SDL */
70 //void sdl_callbackbuffer(PT_TUNE *pt, Uint8 *buf, int len){
71 void sdl_callbackbuffer(void *userdata, Uint8 *buf, int len){
72 for(int i = 0; i < len; i++){
73 buf[i] = interrupthandler();
77 #ifdef JACK
78 /* initialize JACK audio */
79 u8 j_init(void){
80 jack_client_t *client;
81 const intptr_t **ports;
83 fprintf(stderr, "Trying jack....\n");
85 // tell the JACK server to call error() whenever it
86 //experiences an error. Notice that this callback is
87 // global to this process, not specific to each client.
88 //
89 // This is set here so that it can catch errors in the
90 // connection process
91 jack_set_error_function(j_error);
93 // try to become a client of the JACK server
95 if((client = jack_client_new("pineappletracker")) == 0){
96 fprintf(stderr, "jack server not running?\n");
97 return 1;
100 // tell the JACK server to call `process()' whenever
101 // there is work to be done.
103 jack_set_process_callback(client, j_process, 0);
105 // tell the JACK server to call `jack_shutdown()' if
106 // it ever shuts down, either entirely, or if it
107 // just decides to stop calling us.
109 jack_on_shutdown(client, j_shutdown, 0);
111 // display the current sample rate. once the client is activated
112 // (see below), you should rely on your own sample rate
113 // callback (see above) for this value.
114 fprintf(stderr, "engine sample rate: %d\n", jack_get_sample_rate (client));
116 sr=jack_get_sample_rate(client);
118 output_port = jack_port_register(client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
120 // tell the JACK server that we are ready to roll
122 if(jack_activate(client)){
123 fprintf (stderr, "cannot activate client");
124 return 1;
127 // connect the ports
128 if((ports = jack_get_ports(client, NULL, NULL,
129 JackPortIsPhysical|JackPortIsInput)) == NULL){
130 fprintf(stderr, "Cannot find any physical playback ports\n");
131 return 1;
134 int i=0;
135 while(ports[i]!=NULL){
136 if(jack_connect(client, jack_port_name (output_port), ports[i]))
137 fprintf(stderr, "cannot connect output ports\n");
138 i++;
141 return 0;
144 int j_process(jack_nframes_t nframes, void *arg){
145 // grab our output buffer
146 sample_t *out = (sample_t *) jack_port_get_buffer(output_port, nframes);
148 // For each required sample
149 for(jack_nframes_t i=0; i<nframes; i++){
150 out[i] = (sample_t) interrupthandler();
152 return 0;
155 void j_error(const char *desc){
156 fprintf(stderr, "JACK error: %s\n", desc);
159 void j_shutdown(void *arg){
160 exit(1);
162 #endif // JACK
164 int main(int argc, char **argv){
165 #ifdef JACK
166 if(!j_init()){
168 initchip();
169 initgui();
171 if(argc != 2){
172 loadfile("untitled.song");
173 }else{
174 loadfile(argv[1]);
176 guiloop();
178 //free (ports);
179 //jack_client_close (client);
180 }else if(!sdl_init()){
181 #else
182 if(!sdl_init()){
183 #endif
184 initchip();
185 initgui();
187 if(argc != 2){
188 loadfile("untitled.song");
189 }else{
190 loadfile(argv[1]);
193 SDL_PauseAudio(0);
194 guiloop();
196 SDL_Quit();
199 return 0;