int main(int argc, char **argv){ FILE *infile, *outfile; HeaderType inHeader, outHeader; uint16_t *indata, *outdata = NULL; // verifica o uso if(argc != 3){ printf("use: %s infile outfile\n", argv[0]); return 1; } // abre arquivo de entrada infile = fopen(argv[1], "rb"); if(infile == NULL){ perror(argv[1]); exit(1); } // le arquivo de entrada fread(&inHeader, sizeof(HeaderType), 1, infile); indata = malloc(inHeader.Subchunk2Size); fread(indata, 1, inHeader.Subchunk2Size, infile); // verifica formato if(inHeader.AudioFormat == PCM_FORMAT){ adpcm_code(&outHeader, &outdata, inHeader, &indata); }else if(inHeader.AudioFormat == ADPCM_FORMAT){ adpcm_decode(&outHeader, &outdata, inHeader, &indata); }else{ printf("Invalid file format\n"); exit(1); } // abre arquivo de saida outfile = fopen(argv[2], "wb"); if(outfile == NULL){ perror(argv[2]); exit(1); } // escreve arquivo de saida fwrite(&outHeader, sizeof(HeaderType), 1, outfile); fwrite(outdata, 1, outHeader.Subchunk2Size, outfile); if(outdata != NULL){ free(outdata); } free(indata); fclose(outfile); fclose(infile); return 0; }
void sb_device::device_timer(emu_timer &timer, device_timer_id tid, int param, void *ptr) { // printf("DMA timer expire\n"); if (tid) { device_serial_interface::device_timer(timer, tid, param, ptr); return; } UINT16 lsample, rsample; switch (m_dsp.flags) { case 0: // 8-bit unsigned mono m_dacl->write_unsigned8(m_dsp.data[m_dsp.d_rptr]); m_dacr->write_unsigned8(m_dsp.data[m_dsp.d_rptr]); m_dsp.data[m_dsp.d_rptr++] = 0x80; break; case SIGNED: // 8-bit signed mono m_dacl->write_unsigned8(m_dsp.data[m_dsp.d_rptr] + 128); m_dacr->write_unsigned8(m_dsp.data[m_dsp.d_rptr] + 128); m_dsp.data[m_dsp.d_rptr++] = 0x00; break; case STEREO: // 8-bit unsigned stereo m_dacl->write_unsigned8(m_dsp.data[m_dsp.d_rptr]); m_dsp.data[m_dsp.d_rptr++] = 0x80; m_dacr->write_unsigned8(m_dsp.data[m_dsp.d_rptr]); m_dsp.data[m_dsp.d_rptr++] = 0x80; break; case SIGNED | STEREO: // 8-bit signed stereo m_dacl->write_unsigned8(m_dsp.data[m_dsp.d_rptr] + 128); m_dsp.data[m_dsp.d_rptr++] = 0x00; m_dacr->write_unsigned8(m_dsp.data[m_dsp.d_rptr] + 128); m_dsp.data[m_dsp.d_rptr++] = 0x00; break; case SIXTEENBIT: // 16-bit unsigned mono lsample = m_dsp.data[m_dsp.d_rptr] | (m_dsp.data[m_dsp.d_rptr+1] << 8); m_dsp.data[m_dsp.d_rptr++] = 0x00; m_dsp.data[m_dsp.d_rptr++] = 0x80; m_dacl->write_unsigned16(lsample); m_dacr->write_unsigned16(lsample); break; case SIXTEENBIT | SIGNED: // 16-bit signed mono lsample = m_dsp.data[m_dsp.d_rptr] | (m_dsp.data[m_dsp.d_rptr+1] << 8); m_dsp.data[m_dsp.d_rptr++] = 0x00; m_dsp.data[m_dsp.d_rptr++] = 0x00; m_dacl->write_unsigned16(lsample + 32768); m_dacr->write_unsigned16(lsample + 32768); break; case SIXTEENBIT | STEREO: // 16-bit unsigned stereo lsample = m_dsp.data[m_dsp.d_rptr] | (m_dsp.data[m_dsp.d_rptr+1] << 8); m_dsp.data[m_dsp.d_rptr++] = 0x00; m_dsp.data[m_dsp.d_rptr++] = 0x80; m_dsp.d_rptr %= 128; rsample = m_dsp.data[m_dsp.d_rptr] | (m_dsp.data[m_dsp.d_rptr+1] << 8); m_dsp.data[m_dsp.d_rptr++] = 0x00; m_dsp.data[m_dsp.d_rptr++] = 0x80; m_dacl->write_unsigned16(lsample); m_dacr->write_unsigned16(rsample); break; case SIXTEENBIT | SIGNED | STEREO: // 16-bit signed stereo lsample = m_dsp.data[m_dsp.d_rptr] | (m_dsp.data[m_dsp.d_rptr+1] << 8); m_dsp.data[m_dsp.d_rptr++] = 0x00; m_dsp.data[m_dsp.d_rptr++] = 0x00; m_dsp.d_rptr %= 128; rsample = m_dsp.data[m_dsp.d_rptr] | (m_dsp.data[m_dsp.d_rptr+1] << 8); m_dsp.data[m_dsp.d_rptr++] = 0x00; m_dsp.data[m_dsp.d_rptr++] = 0x00; m_dacl->write_unsigned16(lsample + 32768); m_dacr->write_unsigned16(rsample + 32768); break; case ADPCM2: if(m_dsp.adpcm_new_ref) { m_dsp.adpcm_ref = m_dsp.data[m_dsp.d_rptr++]; m_dsp.adpcm_new_ref = false; m_dacl->write_unsigned8(m_dsp.adpcm_ref); m_dacr->write_unsigned8(m_dsp.adpcm_ref); break; } lsample = m_dsp.data[m_dsp.d_rptr]; switch(m_dsp.adpcm_count++) { case 0: adpcm_decode(lsample >> 6, 2); break; case 1: adpcm_decode((lsample >> 4) & 3, 2); break; case 2: adpcm_decode((lsample >> 2) & 3, 2); break; case 3: adpcm_decode(lsample & 3, 2); m_dsp.data[m_dsp.d_rptr++] = 0x80; m_dsp.adpcm_count = 0; break; } break; case ADPCM3: if(m_dsp.adpcm_new_ref) { m_dsp.adpcm_ref = m_dsp.data[m_dsp.d_rptr++]; m_dsp.adpcm_new_ref = false; m_dacl->write_unsigned8(m_dsp.adpcm_ref); m_dacr->write_unsigned8(m_dsp.adpcm_ref); break; } lsample = m_dsp.data[m_dsp.d_rptr]; switch(m_dsp.adpcm_count++) { case 0: adpcm_decode(lsample >> 5, 3); break; case 1: adpcm_decode((lsample >> 2) & 7, 3); break; case 2: adpcm_decode(((lsample & 2) << 1) | (lsample & 1), 3); m_dsp.data[m_dsp.d_rptr++] = 0x80; m_dsp.adpcm_count = 0; break; } break; case ADPCM4: if(m_dsp.adpcm_new_ref) { m_dsp.adpcm_ref = m_dsp.data[m_dsp.d_rptr++]; m_dsp.adpcm_new_ref = false; m_dacl->write_unsigned8(m_dsp.adpcm_ref); m_dacr->write_unsigned8(m_dsp.adpcm_ref); break; } lsample = m_dsp.data[m_dsp.d_rptr]; switch(m_dsp.adpcm_count++) { case 0: adpcm_decode(lsample >> 4, 4); break; case 1: adpcm_decode(lsample & 15, 4); m_dsp.data[m_dsp.d_rptr++] = 0x80; m_dsp.adpcm_count = 0; break; } break; default: logerror("SB: unimplemented sample type %x\n", m_dsp.flags); } m_dsp.d_rptr %= 128; if (m_dsp.dma_throttled) { if (m_dsp.d_rptr == m_dsp.d_wptr) { // printf("unthrottling DRQ\n"); if(m_dsp.flags & SIXTEENBIT) // 16-bit audio through 8-bit dma? drq16_w(1); else drq_w(1); // raise DRQ m_dsp.dma_throttled = false; } } }
void Hes_Apu_Adpcm::run_until( blip_time_t end_time ) { int volume = state.volume; int fadetimer = state.fadetimer; int fadecount = state.fadecount; int last_time = this->last_time; double next_timer = this->next_timer; int last_amp = this->last_amp; Blip_Buffer* output = this->output; // cache often-used values while ( state.playflag && last_time < end_time ) { while ( last_time >= next_timer ) { if ( fadetimer ) { if ( fadecount > 0 ) { fadecount--; volume = 0xFF * fadecount / fadetimer; } else if ( fadecount < 0 ) { fadecount++; volume = 0xFF - ( 0xFF * fadecount / fadetimer ); } } next_timer += 7159.091; } int amp; if ( state.ad_low_nibble ) { amp = adpcm_decode( state.pcmbuf[ state.playptr ] & 0x0F ); state.ad_low_nibble = false; state.playptr++; state.playedsamplecount++; if ( state.playedsamplecount == state.playlength ) { state.playflag = 0; } } else { amp = adpcm_decode( state.pcmbuf[ state.playptr ] >> 4 ); state.ad_low_nibble = true; } amp = amp * volume / 0xFF; int delta = amp - last_amp; if ( output && delta ) { last_amp = amp; synth.offset_inline( last_time, delta, output ); } last_time += state.freq; } if ( !state.playflag ) { while ( next_timer <= end_time ) next_timer += 7159.091; last_time = end_time; } this->last_time = last_time; this->next_timer = next_timer; this->last_amp = last_amp; state.volume = volume; state.fadetimer = fadetimer; state.fadecount = fadecount; }
BOOLEAN adpcm_voice_encode(tBTA_ATT_HSDATA *att_data) { //BTM_TRACE_DEBUG0("adpcm_voice_encode"); uint8_t buffer[MAX_FRAME_SIZE]; int size; int frame_size; UINT8 *p = NULL; FILE *fp; fp = fopen (RTK_STORAGE_PCM_FILE, "a+"); if (fp == NULL) { BTM_TRACE_WARNING0("open file failed\n"); return FALSE; } p = (UINT8*)(att_data->data+3); size = att_data->data_len - 3; /* we must receive 12 20-bytes packets followed by a 19-bytes packet */ switch (size) { case 20: adpcm_rec_cb.n_20bytes_pkts++; if (adpcm_rec_cb.n_20bytes_pkts > 12){ BTM_TRACE_DEBUG0("20bytes pkt is exceed 12"); adpcm_rec_cb.n_20bytes_pkts = 1; memcpy(adpcm_rec_cb.buffer + (adpcm_rec_cb.n_20bytes_pkts - 1) * 20,p, 20); } else { memcpy(adpcm_rec_cb.buffer + (adpcm_rec_cb.n_20bytes_pkts - 1) * 20,p, 20); } break; case 19: if (adpcm_rec_cb.n_20bytes_pkts != 12) { BTM_TRACE_DEBUG0("20bytes pkt!=12,discard the frame"); adpcm_rec_cb.n_20bytes_pkts = 0; } else { memcpy(adpcm_rec_cb.buffer + adpcm_rec_cb.n_20bytes_pkts * 20, p, 19); adpcm_rec_cb.n_20bytes_pkts = 0; frame_size = adpcm_decode(adpcm_rec_cb.adpcm_state, buffer, adpcm_rec_cb.buffer,ADPCM_ENCODED_FRAME_SIZE); BTM_TRACE_DEBUG1("frame size= %d",frame_size); if(frame_size == 1020) { fwrite(buffer, frame_size, 1, fp); if(adpcm_rec_cb.channel_status) { if(isEmpty()) { BTM_TRACE_DEBUG0("the buffer empty,send data to socket"); VOHOG_LOCK(); if(!UIPC_RTKBT_VR_Send_noblock(UIPC_CH_ID_RTKBT_VR_AUDIO, 0, buffer, frame_size)) { BTM_TRACE_WARNING0("send data fail, save data to buffer"); saveDATAtobuffer(buffer, frame_size); } VOHOG_UNLOCK(); } else { BTM_TRACE_DEBUG0("the buffer is not empty, save data to buffer"); saveDATAtobuffer(buffer, frame_size); } } else { if(!adpcm_rec_cb.channel_status) { BTM_TRACE_DEBUG0("socket is close"); } saveDATAtobuffer(buffer, frame_size); } }else { BTM_TRACE_DEBUG0("frame_size is error"); } } break; default: BTM_TRACE_DEBUG0("size is error"); } //BTM_TRACE_DEBUG1("20bytes pkt= %d",adpcm_rec_cb.n_20bytes_pkts); free(att_data); //fseek(fp,0,SEEK_END); //int lSize = ftell(fp); //BTM_TRACE_WARNING1("file length %d\n",lSize); fclose(fp); return TRUE; }
int borplay(FILE *fd, char *fname, unsigned int off, unsigned int size) { bor_header bh; int len, kb = 0; short *out; unsigned char *in; char *p, *artist, *title; if(fseek(fd, off, SEEK_SET) < 0) std_err(); if(!fread(&bh, sizeof(bh), 1, fd)) return(-1); size -= sizeof(bh); if(strncmp(bh.identifier, BOR_IDENTIFIER, sizeof(bh.identifier))) return(0); if((bh.version != BOR_MUSIC_VERSION) && (bh.version != NEW_MUSIC_VERSION)) { printf("- warning: unknown file version (%08x)\n", bh.version); } // fix title for(p = bh.title; *p; p++) { if (*p == '_') *p = ' '; } // fix artist for(p = bh.artist; *p; p++) { if (*p == '_') *p = ' '; } // force mono if it's a v1 file if ((bh.version == BOR_MUSIC_VERSION) && (bh.channels != 1)) { printf("- warning: forcing mono playback because of file version; " "use the newest Wav2Bor from LavaLit.com to create BOR files " "with more than 1 channel of audio\n"); bh.channels = 1; } printf("- %08x (%dhz %dch) %s\n", off, bh.frequency, bh.channels, fname); // only print title and artist if at least one of them is specified if (strlen(bh.artist) && strlen(bh.title)) printf(" %s - %s\n", bh.artist, bh.title); else if (strlen(bh.artist)) printf(" %s\n", bh.artist); else if (strlen(bh.title)) printf(" %s\n", bh.title); if(bh.datastart > sizeof(bh)) { bh.datastart -= sizeof(bh); if(fseek(fd, bh.datastart, SEEK_CUR) < 0) std_err(); size -= bh.datastart; } in = malloc(MUSIC_BUF_SIZE); if(!in) std_err(); out = malloc(MUSIC_BUF_SIZE * 4); if(!out) std_err(); aoinit_fmt(bh.channels, bh.frequency); #ifndef WIN32 init_keyboard(); #endif while(size && (len = fread(in, 1, MUSIC_BUF_SIZE, fd))) { if(size < len) len = size; size -= len; adpcm_decode(in, out, len, bh.channels); if(!ao_play(device, (void *)out, len * 4)) break; kb = check_kbhit(); if(kb) break; } #ifndef WIN32 close_keyboard(); #endif ao_close(device); free(in); free(out); return(kb); }