Just some notes and SPEC stuff.
[pineappletracker.git] / main.c
blob9e5267b0c74e2233ebb3e28fcb595cf101424f48
1 /* vi:set ts=8 sts=8 sw=8 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 //\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\/\\
70 \\\ < void sdl_callbackbuffer(void*,Uint8*,int) > .|
71 /// Called by SDL. Fills the dac buffer. .\
72 \\/\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\//
73 //void sdl_callbackbuffer(PT_TUNE *pt, Uint8 *buf, int len){
74 void sdl_callbackbuffer(void *userdata, Uint8 *buf, int len){
75 for(int i = 0; i < len; i++){
76 buf[i] = interrupthandler();
80 #ifdef JACK
81 /* initialize JACK audio */
82 u8 j_init(void){
83 jack_client_t *client;
84 const intptr_t **ports;
86 fprintf(stderr, "Trying jack....\n");
88 // tell the JACK server to call error() whenever it
89 //experiences an error. Notice that this callback is
90 // global to this process, not specific to each client.
91 //
92 // This is set here so that it can catch errors in the
93 // connection process
94 jack_set_error_function(j_error);
96 // try to become a client of the JACK server
98 if((client = jack_client_new("pineappletracker")) == 0){
99 fprintf(stderr, "jack server not running?\n");
100 return 1;
103 // tell the JACK server to call `process()' whenever
104 // there is work to be done.
106 jack_set_process_callback(client, j_process, 0);
108 // tell the JACK server to call `jack_shutdown()' if
109 // it ever shuts down, either entirely, or if it
110 // just decides to stop calling us.
112 jack_on_shutdown(client, j_shutdown, 0);
114 // display the current sample rate. once the client is activated
115 // (see below), you should rely on your own sample rate
116 // callback (see above) for this value.
117 fprintf(stderr, "engine sample rate: %d\n", jack_get_sample_rate (client));
119 sr=jack_get_sample_rate(client);
121 output_port = jack_port_register(client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
123 // tell the JACK server that we are ready to roll
125 if(jack_activate(client)){
126 fprintf (stderr, "cannot activate client");
127 return 1;
130 // connect the ports
131 if((ports = jack_get_ports(client, NULL, NULL,
132 JackPortIsPhysical|JackPortIsInput)) == NULL){
133 fprintf(stderr, "Cannot find any physical playback ports\n");
134 return 1;
137 int i=0;
138 while(ports[i]!=NULL){
139 if(jack_connect(client, jack_port_name (output_port), ports[i]))
140 fprintf(stderr, "cannot connect output ports\n");
141 i++;
144 return 0;
147 int j_process(jack_nframes_t nframes, void *arg){
148 // grab our output buffer
149 sample_t *out = (sample_t *) jack_port_get_buffer(output_port, nframes);
151 // For each required sample
152 for(jack_nframes_t i=0; i<nframes; i++){
153 out[i] = (sample_t) interrupthandler();
155 return 0;
158 void j_error(const char *desc){
159 fprintf(stderr, "JACK error: %s\n", desc);
162 void j_shutdown(void *arg){
163 exit(1);
165 #endif // JACK
167 int main(int argc, char **argv){
168 #ifdef JACK
169 if(!j_init()){
171 initchip();
172 initgui();
174 if(argc != 2){
175 loadfile("untitled.song");
176 }else{
177 loadfile(argv[1]);
179 guiloop();
181 //free (ports);
182 //jack_client_close (client);
183 }else if(!sdl_init()){
184 #else
185 if(!sdl_init()){
186 #endif
187 initchip();
188 initgui();
190 if(argc != 2){
191 loadfile("untitled.song");
192 }else{
193 loadfile(argv[1]);
196 SDL_PauseAudio(0);
197 guiloop();
199 SDL_Quit();
202 return 0;