1 /* vi:set ts=8 sts=8 sw=8 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
);
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();
81 /* initialize JACK audio */
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.
92 // This is set here so that it can catch errors in the
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");
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");
131 if((ports
= jack_get_ports(client
, NULL
, NULL
,
132 JackPortIsPhysical
|JackPortIsInput
)) == NULL
){
133 fprintf(stderr
, "Cannot find any physical playback ports\n");
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");
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();
158 void j_error(const char *desc
){
159 fprintf(stderr
, "JACK error: %s\n", desc
);
162 void j_shutdown(void *arg
){
167 int main(int argc
, char **argv
){
175 loadfile("untitled.song");
182 //jack_client_close (client);
183 }else if(!sdl_init()){
191 loadfile("untitled.song");