3 * Copyright (c) 2006 acmesystems.it - john@acmesystems.it
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
19 * Feedback, Bugs... info@acmesystems.it
30 typedef struct _MP3_STATE {
37 unsigned char name[2048];
41 unsigned char url[2048];
42 unsigned char path[256];
45 MP3_PLAYTIME playtime;
48 static MP3_STATE mp3_state;
50 void state_startup_enter(int state_last, int event, EVENT_PARAM *param){
51 mp3_state.decoder.volume = 0x30;
52 mp3_state.decoder.bass = 1;
53 mp3_state.file.name[0] = '\0';
54 mp3_state.stream.url[0] = '\0';
55 mp3_state.stream.path[0] = '\0';
56 mp3_state.stream.port = 0;
57 mp3_playtime_init(&mp3_state.playtime);
59 mp3_nix_socket_setup();
60 mp3_tcp_socket_setup();
63 void state_idle_enter(int state_last, int event, EVENT_PARAM *param){
68 void state_file_startup_enter(int state_last, int event, EVENT_PARAM *param){
69 if(mp3_file_setup(param->text, &mp3_state.file.id3) == MP3_OK){
70 strcpy(mp3_state.file.name, param->text);
72 state_event(MP3_EVENT_ERROR, NULL);
76 void state_file_startup_leave(int state_new, int event){
77 if(state_new == MP3_STATE_FILE_HANDLE){
80 mp3_nix_socket_write("START FILE\n");
84 void state_file_handle_enter(int state_last, int event, EVENT_PARAM *param){
85 int ret = mp3_file_handle();
87 state_event(MP3_EVENT_ERROR, NULL);
90 state_event(MP3_EVENT_END, NULL);
95 void state_file_handle_leave(int state_new, int event){
96 if(state_new != MP3_STATE_FILE_HANDLE){
99 mp3_nix_socket_write("STOP FILE\n");
103 void state_stream_startup_enter(int state_last, int event, EVENT_PARAM *param){
104 if(mp3_stream_setup(param->text, param->numeric, mp3_state.stream.url,
105 mp3_state.stream.path, &mp3_state.stream.port) != MP3_OK){
106 state_event(MP3_EVENT_ERROR, NULL);
110 void state_stream_startup_leave(int state_new, int event){
111 if(state_new == MP3_STATE_STREAM_HANDLE){
113 mp3_playtime_start();
114 mp3_nix_socket_write("START STREAM\n");
118 void state_stream_handle_enter(int state_last, int event, EVENT_PARAM *param){
119 if(mp3_stream_handle() == MP3_ERROR){
120 state_event(MP3_EVENT_ERROR, NULL);
124 void state_stream_handle_leave(int state_new, int event){
125 if(state_new != MP3_STATE_STREAM_HANDLE){
126 mp3_stream_cleanup();
128 mp3_nix_socket_write("STOP STREAM\n");
132 extern STATE states[MAX_STATE_COUNT];
133 void state_error_enter(int state_last, int event, EVENT_PARAM *param){
135 printf("Error in state %s -> %s\n", states[state_last].name, param->text);
136 mp3_nix_socket_write("ERROR Error in state %s -> %s\n", states[state_last].name, param->text);
138 printf("Unknown error in state %s\n", states[state_last].name);
142 void state_shutdown_enter(int state_last, int event, EVENT_PARAM *param){
143 printf("Entering state SHUTDOWN ...\n");
144 printf("Shutting down player ...\n");
145 mp3_nix_socket_cleanup();
146 mp3_tcp_socket_cleanup();
147 printf("Quitting statemachine ...\n");
150 extern int state_current;
151 void state_generic_event(unsigned int event, unsigned char in_int,
152 unsigned char *out_uchar){
154 case MP3_EVENT_GENERIC_VOLUME:
155 mp3_state.decoder.volume = in_int;
156 mp3_set_volume(mp3_state.decoder.volume,
157 mp3_state.decoder.volume);
158 mp3_nix_socket_write("VOLUME %d\n", mp3_state.decoder.volume);
160 case MP3_EVENT_GENERIC_BASS:
161 mp3_state.decoder.bass = in_int;
162 mp3_bass(8, mp3_state.decoder.treble,
163 8, mp3_state.decoder.bass);
164 mp3_nix_socket_write("BASS %d\n", mp3_state.decoder.bass);
166 case MP3_EVENT_GENERIC_STATE:
168 sprintf(out_uchar, "%sVOLUME %d\n",
169 out_uchar, mp3_state.decoder.volume);
170 sprintf(out_uchar, "%sBASS %d\n",
171 out_uchar, mp3_state.decoder.bass);
172 sprintf(out_uchar, "%sTREBLE %d\n",
173 out_uchar, mp3_state.decoder.treble);
174 sprintf(out_uchar, "%sPLAYTIME %d\n",
176 (mp3_state.playtime.playtime_mins * 60) + mp3_state.playtime.playtime_secs);
177 switch(state_current){
179 sprintf(out_uchar, "%sSTATE MP3_STATE_IDLE\n", out_uchar);
181 case MP3_STATE_FILE_START:
182 case MP3_STATE_FILE_HANDLE:
183 sprintf(out_uchar, "%sFILE %s\n", out_uchar,
184 mp3_state.file.name);
185 if(strlen(mp3_state.file.id3.artist)){
186 sprintf(out_uchar, "%sID3_ARTIST %s\n",
188 mp3_state.file.id3.artist);
190 if(strlen(mp3_state.file.id3.album)){
191 sprintf(out_uchar, "%sID3_ALBUM %s\n",
193 mp3_state.file.id3.album);
195 if(strlen(mp3_state.file.id3.album)){
196 sprintf(out_uchar, "%sID3_TRACK %s\n",
198 mp3_state.file.id3.track);
200 sprintf(out_uchar, "%sSTATE MP3_STATE_FILE\n", out_uchar);
202 case MP3_STATE_STREAM_START:
203 case MP3_STATE_STREAM_HANDLE:
204 sprintf(out_uchar, "%sSTREAM %s:%d%s\n", out_uchar,
205 mp3_state.stream.url,
206 mp3_state.stream.port,
207 mp3_state.stream.path);
208 sprintf(out_uchar, "%sSTATE MP3_STATE_STREAM\n", out_uchar);
211 sprintf(out_uchar, "STATE Unkonwn\n");
214 sprintf(out_uchar, "%sOK\n", out_uchar);
216 case MP3_EVENT_GENERIC_PLAYTIME:
217 // printf("%02d:%02d\n", mp3_state.playtime.playtime_mins,
218 // mp3_state.playtime.playtime_secs);