int decodeMPEG(struct AUDIO_HEADER *header) { int cnt, g, snd_eof; /* * decoder loop ********************************** */ snd_eof=0; cnt=0; while (!snd_eof) { while (!snd_eof && ready_audio()) { if ((g=gethdr(header))!=0) { report_header_error(g); snd_eof=1; break; } if (header->protection_bit==0) getcrc(); #if 0 statusDisplay(header,cnt); #endif if (header->layer==1) { if (layer3_frame(header,cnt)) { yell(" error. blip."); return -1; } } else if (header->layer==2) { if (layer2_frame(header,cnt)) { yell(" error. blip."); return -1; } } cnt++; } } return 0; }
/* * TODO: add some kind of error reporting here */ void play(char *inFileStr) { char *f; long totalframes = 0; long tseconds = 0; struct AUDIO_HEADER header; int bitrate, fs, g, cnt = 0; while ((f = new_next_arg(inFileStr, &inFileStr))) { if (!f || !*f) return; if ((in_file=fopen(f,"r"))==NULL) { if (!do_hook(MODULE_LIST, "AMP ERROR open %s", f)) put_it("Could not open file: %s\n", f); continue; } filesize = file_size(f); initialise_globals(); if ((g=gethdr(&header))!=0) { report_header_error(g); continue; } if (header.protection_bit==0) getcrc(); if (setup_audio(&header)!=0) { yell("Cannot set up audio. Exiting"); continue; } filesize -= sizeof(header); switch (header.layer) { case 1: { if (layer3_frame(&header,cnt)) { yell(" error. blip."); continue; } break; } case 2: { if (layer2_frame(&header,cnt)) { yell(" error. blip."); continue; } break; } default: continue; } bitrate=t_bitrate[header.ID][3-header.layer][header.bitrate_index]; fs=t_sampling_frequency[header.ID][header.sampling_frequency]; if (header.ID) framesize=144000*bitrate/fs; else framesize=72000*bitrate/fs; totalframes = (filesize / (framesize + 1)) - 1; tseconds = (totalframes * 1152/ t_sampling_frequency[header.ID][header.sampling_frequency]); if (A_AUDIO_PLAY) { char *p = strrchr(f, '/'); if (!p) p = f; else p++; if (!do_hook(MODULE_LIST, "AMP PLAY %lu %lu %s", tseconds, filesize, p)) bitchsay("Playing: %s\n", p); } /* * */ if (!(fseek(in_file, 0, SEEK_END))) { char id3_tag[256]; if (!fseek(in_file, -128, SEEK_END) && (fread(id3_tag,128, 1, in_file) == 1)) { if (!strncmp(id3_tag, "TAG", 3)) print_id3_tag(in_file, id3_tag); } fseek(in_file,0,SEEK_SET); } decodeMPEG(&header); do_hook(MODULE_LIST, "AMP CLOSE %s", f); close_audio(); fclose(in_file); } }
int decodeMPEG_2(int inFilefd) { struct AUDIO_HEADER header; int cnt,g,err=0; TControlMsg message; if ((in_file=fdopen(inFilefd,"r"))==NULL) { return(1); } append=data=nch=0; /* initialize globals */ GUI_STOPPED = FALSE; GUI_PLAYING = TRUE; GUI_FD_TO_PLAY = -1; for (cnt=0;;cnt++) { if ((g=gethdr(&header))!=0) { switch (g) { case GETHDR_ERR: die("error reading mpeg bitstream. exiting.\n"); break; case GETHDR_NS : warn("this is a file in MPEG 2.5 format, which is not defined\n"); warn("by ISO/MPEG. It is \"a special Fraunhofer format\".\n"); warn("amp does not support this format. sorry.\n"); break; case GETHDR_FL1: warn("ISO/MPEG layer 1 is not supported by amp (yet).\n"); break; case GETHDR_FF : warn("free format bitstreams are not supported. sorry.\n"); break; case GETHDR_SYN: warn("oops, we're out of sync.\n"); break; default: } break; } if (!(cnt%10)){ GUIstatusDisplay(cnt); } if(get_msg(&message) >= 0) { int pflag = 0; cnt = parse_msg(&message,&header,cnt); if (GUI_PAUSE) { int flags; pflag = 1; #if 0 /* Set stdin to blocking */ if((flags = fcntl(STDIN_FILENO, F_GETFL, 0)) < 0) perror("fcntl"); flags ^= O_NONBLOCK; if(fcntl(STDIN_FILENO, F_SETFL, flags) < 0) perror("fcntl"); #endif } while(GUI_PAUSE){ if(get_msg(&message) >= 0) cnt = parse_msg(&message,&header,cnt); } if (pflag) { int flags; /* Set stdin to non-blocking */ #if 0 if((flags = fcntl(STDIN_FILENO, F_GETFL, 0)) < 0) perror("fcntl"); flags |= O_NONBLOCK; if(fcntl(STDIN_FILENO, F_SETFL, flags) < 0) perror("fcntl"); #endif } if (GUI_STOP || (GUI_FD_TO_PLAY != -1)){ break; } } /* crc is read just to get out of the way. */ if (header.protection_bit==0) getcrc(); if (!cnt && A_AUDIO_PLAY) { /* setup the audio when we have the frame info */ if (AUDIO_BUFFER_SIZE==0) audioOpen(t_sampling_frequency[header.ID][header.sampling_frequency], (header.mode!=3), A_SET_VOLUME); else audioBufferOpen(t_sampling_frequency[header.ID][header.sampling_frequency],(header.mode!=3),A_SET_VOLUME); } if (layer3_frame(&header,cnt)) { warn(" error. blip.\n"); err=1; break; } } fclose(in_file); if (A_AUDIO_PLAY) if (AUDIO_BUFFER_SIZE!=0) audioBufferClose(); else audioClose(); else fclose(out_file); if (!(GUI_STOP) && (GUI_FD_TO_PLAY == -1)) { /* We've reached the end of the track, notify the jukebox... */ TControlMsg rmsg; rmsg.type = MSG_NEXT; rmsg.data = 0; send_msg(&rmsg, TRUE); } GUI_STOPPED = TRUE; GUI_PLAYING = FALSE; return(err); }