Timing is correct now! Forgot to update samples_per_tick when parsing the 0xf effect.
[pineappletracker.git] / hively / main.c
blobc2d8f33c248c17656445ffc3cc42d2a980e9066e
1 //just gonna see if i can display a hively position list and all the hively tracks and make it scroll
3 #include <stdio.h>
4 #include <stdint.h>
5 #include <stdlib.h>
6 #include <ncurses.h>
7 #include <SDL/SDL.h>
8 #include "hvl_replay.h"
10 #include "pineapple.h"
11 #include "gui.h"
13 #define FREQ 48000
14 #define HIVELY_LEN FREQ/50
15 #define OUTPUT_LEN 4096
17 int16 hivelyLeft[HIVELY_LEN], hivelyRight[HIVELY_LEN];
18 size_t hivelyIndex;
20 void mix_and_play( struct hvl_tune *ht, uint8 *stream, int length );
22 BOOL init( void )
24 //uint32 i;
25 SDL_AudioSpec wanted;
27 if(SDL_Init(SDL_INIT_AUDIO)< 0) {
28 printf("Could not initialize SDL: %s\n", SDL_GetError());
29 SDL_Quit();
30 return FALSE;
33 wanted.freq = FREQ;
34 wanted.format = AUDIO_S16SYS;
35 wanted.channels = 2; /* 1 = mono, 2 = stereo */
36 wanted.samples = OUTPUT_LEN; // HIVELY_LEN;
38 wanted.callback = (void*) mix_and_play;
39 wanted.userdata = tune;
41 if(SDL_OpenAudio(&wanted, NULL) < 0) {
42 printf("Failed to open audio device.\n");
43 SDL_Quit();
44 return FALSE;
47 return TRUE;
50 void mix_and_play( struct hvl_tune *ht, uint8 *stream, int length )
52 int16 *out;
53 int i;
54 size_t streamPos = 0;
55 length = length >> 1;
57 uint32 samples;
58 samples = tune->ht_Frequency/50/ht->ht_SpeedMultiplier;
60 if(tune && play) {
61 // Mix to 16bit interleaved stereo
62 out = (int16*) stream;
63 // Flush remains of previous frame
64 for(i = hivelyIndex; i < (HIVELY_LEN); i++) {
65 out[streamPos++] = hivelyLeft[i];
66 out[streamPos++] = hivelyRight[i];
69 while(streamPos < length) {
70 hvl_DecodeFrame( tune, (int8 *) hivelyLeft, (int8 *) hivelyRight, 2 );
71 for(i = 0; i < (HIVELY_LEN) && streamPos < length; i++) {
72 out[streamPos++] = hivelyLeft[i];
73 out[streamPos++] = hivelyRight[i];
76 hivelyIndex = i;
77 } else if(tune && plonked) {
78 // Mix to 16bit interleaved stereo
79 out = (int16*) stream;
80 // Flush remains of previous frame
81 for(i = hivelyIndex; i < (HIVELY_LEN); i++) {
82 out[streamPos++] = hivelyLeft[i];
83 out[streamPos++] = hivelyRight[i];
86 while(streamPos < length) {
87 hvl_playNote( tune, (int8 *) hivelyLeft, (int8 *) hivelyRight, 2, &tune->ht_Voices[0]);
88 for(i = 0; i < (HIVELY_LEN) && streamPos < length; i++) {
89 out[streamPos++] = hivelyLeft[i];
90 out[streamPos++] = hivelyRight[i];
93 hivelyIndex = i;
97 int main(int argc, char *argv[])
99 hivelyIndex = HIVELY_LEN;
100 if(argc < 2){
101 printf( "Usage: play_hvl <tune>\n" );
102 return 0;
105 if(init()){
106 hvl_InitReplayer();
107 tune = hvl_LoadTune(argv[1], FREQ, 4);
108 if(tune){
109 //BOOL done;
110 //uint32 gotsigs;
112 hvl_InitSubsong( tune, 0 );
113 initgui();
114 play = 0;
115 SDL_PauseAudio(0);
116 guiloop();
120 SDL_PauseAudio(1);
121 hvl_FreeTune(tune);
122 SDL_Quit();
123 return 0;