1 /* vi:set ts=4 sts=4 sw=4 noexpandtab: */
12 #include <jack/jack.h>
15 #include "pineapple.h"
16 #include "musicchip_file.h"
20 //void sdl_callbackbuffer(PT_TUNE *tune, Uint8 *buf, int len);
21 void sdl_callbackbuffer(void *userdata
, Uint8
*buf
, int len
);
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
);
37 /* initialize SDL audio */
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());
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
);
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();
78 /* initialize JACK audio */
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.
89 // This is set here so that it can catch errors in the
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");
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");
128 if((ports
= jack_get_ports(client
, NULL
, NULL
,
129 JackPortIsPhysical
|JackPortIsInput
)) == NULL
){
130 fprintf(stderr
, "Cannot find any physical playback ports\n");
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");
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();
155 void j_error(const char *desc
){
156 fprintf(stderr
, "JACK error: %s\n", desc
);
159 void j_shutdown(void *arg
){
164 int main(int argc
, char **argv
){
172 loadfile("untitled.song");
179 //jack_client_close (client);
180 }else if(!sdl_init()){
188 loadfile("untitled.song");