int main(int argc, char *argv[]) { uint32_t wav_buf[WAV_HEADER_SIZE / 4 + 1]; struct wav_info wav_info; off_t file_end, data_off; char *caf = NULL; char *wav = NULL; if (argc > 1) caf = argv[1]; if (argc > 2) wav = argv[2]; if (caf == NULL) { fputs("Usage: test <caf> [wav]\n", stderr); return 1; } if (load(caf) < 0) return 1; if (wav != NULL) { if ((wav_fd = fs_open(wav, FS_CREAT | FS_TRUNC | FS_WRONLY)) < 0) perror("Open failed"); else { fs_write(wav_fd, wav_buf, WAV_HEADER_SIZE); data_off = fs_seek(wav_fd, 0, FS_SEEK_CUR); assert(data_off == WAV_HEADER_SIZE); } } play(); if (wav_fd > 0) { file_end = fs_seek(wav_fd, 0, FS_SEEK_CUR); fs_seek(wav_fd, 0, FS_SEEK_SET); wav_info.blocks = NULL; wav_info.size = file_end - data_off; wav_info.sample_rate = ima_info.sample_rate; wav_info.frame_count = ima_info.frame_count; wav_info.sample_format = WAV_FORMAT_INT16; wav_info.channel_count = ima_info.channel_count; wav_write(wav_buf, &wav_info); fs_write(wav_fd, wav_buf, WAV_HEADER_SIZE); fs_close(wav_fd); } return 0; }
int encode_to_wav(FILE *payload, const char *out_fname, const quiet_encoder_options *opt) { SNDFILE *wav = wav_open(out_fname, sample_rate); if (wav == NULL) { printf("failed to open wav file for writing\n"); return 1; } quiet_encoder *e = quiet_encoder_create(opt, sample_rate); size_t block_len = 16384; uint8_t *readbuf = malloc(block_len * sizeof(uint8_t)); size_t samplebuf_len = 16384; quiet_sample_t *samplebuf = malloc(samplebuf_len * sizeof(quiet_sample_t)); quiet_encoder_clamp_frame_len(e, samplebuf_len); bool done = false; if (readbuf == NULL) { return 1; } if (samplebuf == NULL) { return 1; } while (!done) { size_t nread = fread(readbuf, sizeof(uint8_t), block_len, payload); if (nread == 0) { break; } else if (nread < block_len) { done = true; } size_t frame_len = quiet_encoder_get_frame_len(e); for (size_t i = 0; i < nread; i += frame_len) { frame_len = (frame_len > (nread - i)) ? (nread - i) : frame_len; quiet_encoder_send(e, readbuf + i, frame_len); } size_t written = samplebuf_len; while (written == samplebuf_len) { written = quiet_encoder_emit(e, samplebuf, samplebuf_len); wav_write(wav, samplebuf, written); } } quiet_encoder_destroy(e); free(readbuf); free(samplebuf); wav_close(wav); return 0; }
int main(int ac, char** av) { wav_handle_t iw; wav_handle_t ow; cmd_handle_t cmd; int err = -1; if (cmd_init(&cmd, ac - 1, av + 1)) { PERROR(); goto on_error_0; } if ((cmd.flags & CMD_FLAG_IPATH) == 0) { PERROR(); goto on_error_0; } if ((cmd.flags & CMD_FLAG_OPATH) == 0) { PERROR(); goto on_error_0; } if (wav_open(&iw, cmd.ipath)) { PERROR(); goto on_error_0; } if (wav_copy(&ow, &iw)) { PERROR(); goto on_error_1; } if (wav_write(&ow, cmd.opath)) { PERROR(); goto on_error_2; } err = 0; on_error_2: wav_close(&ow); on_error_1: wav_close(&iw); on_error_0: return err; }
/** * \brief Redux the Page file data. * \param[in] vsfname data file name. * \param[in] wallPath Path to save wall data. * \param[in] spritePath Path to save sprite data. * \param[in] soundPath Path to save sound data. * \param[in] palette Palette array. * \return On success true, otherwise false. * \note Caller is responsible for freeing allocated data by calling MM_FREE. */ PUBLIC wtBoolean PageFile_ReduxDecodePageData( const char *vsfname, const char *wallPath, const char *spritePath, const char *soundPath, W8 *palette ) { void *data; void *decdata; W32 length; char tempFileName[ 1024 ]; W32 i; W32 SpriteStart, NumBlocks, SoundStart; W32 soundBufferSize; W8 *soundBuffer; W32 totallength; printf( "Decoding Page Data..." ); if( ! PageFile_Setup( vsfname, &NumBlocks, &SpriteStart, &SoundStart ) ) { PageFile_Shutdown(); return false; } // //////////////////////////////////////////////////////////////////////// // Decode Walls for( i = 0 ; i < SpriteStart ; ++i ) { data = PageFile_getPage( i, &length ); if( data == NULL ) { continue; } decdata = PageFile_decodeWall_RGB32( (PW8)data, palette ); if( decdata == NULL ) { fprintf( stderr, "[PageFile_ReduxDecodePageData]: Unable to decode wall (%d).\n", i ); MM_FREE( data ); continue; } if( _filterScale > 0 ) { void *scaledImgBuf; scaledImgBuf = (void *) MM_MALLOC( 128 * 128 * 4 ); if( NULL == scaledImgBuf ) { MM_FREE( data ); MM_FREE( decdata ); continue; } // Scale2x if( _filterScale == 1 ) { scale( 2, (void *)scaledImgBuf, 128 * 4, decdata, 64 * 4, 4, 64, 64 ); RGB32toRGB24( (const PW8)scaledImgBuf, (PW8)scaledImgBuf, 128 * 128 * 4 ); } else { // hq2x RGB32toRGB24( (const PW8)decdata, (PW8)decdata, 64 * 64 * 4 ); RGB24toBGR565( decdata, decdata, 64 * 64 * 3 ); hq2x_32( (PW8)decdata, (PW8)scaledImgBuf, 64, 64, 64 * 2 * 4 ); RGB32toRGB24( (const PW8)scaledImgBuf, (PW8)scaledImgBuf, 128 * 128 * 4 ); } wt_snprintf( tempFileName, sizeof( tempFileName ), "%s%c%.3d.tga", wallPath, PATH_SEP, GetWallMappedIndex( i ) ); TGA_write( tempFileName, 24, 128, 128, scaledImgBuf, 0, 1 ); MM_FREE( scaledImgBuf ); } else { wt_snprintf( tempFileName, sizeof( tempFileName ), "%s%c%.3d.tga", wallPath, PATH_SEP, GetWallMappedIndex( i ) ); RGB32toRGB24( (const PW8)decdata, (PW8)decdata, 64 * 64 * 4 ); TGA_write( tempFileName, 24, 64, 64, decdata, 0, 1 ); } MM_FREE( data ); MM_FREE( decdata ); } // //////////////////////////////////////////////////////////////////////// // Decode Sprites for( i = SpriteStart ; i < SoundStart ; ++i ) { data = PageFile_getPage( i, &length ); if( data == NULL ) { continue; } decdata = PageFile_decodeSprite_RGB32( (PW8)data, palette ); if( decdata == NULL ) { MM_FREE( data ); continue; } if( _filterScale_Sprites > 0 ) { W8 *scaledImgBuf; scaledImgBuf = (PW8) MM_MALLOC( 128 * 128 * 4 ); if( NULL == scaledImgBuf ) { MM_FREE( data ); MM_FREE( decdata ); continue; } if( _filterScale_Sprites == 1 ) { scale( 2, (void *)scaledImgBuf, 128 * 4, decdata, 64 * 4, 4, 64, 64 ); } else { // hq2x RGB32toRGB24( (const PW8)decdata, (PW8)decdata, 64 * 64 * 4 ); RGB24toBGR565( decdata, decdata, 64 * 64 * 3 ); hq2x_32( (PW8)decdata, (PW8)scaledImgBuf, 64, 64, 64 * 2 * 4 ); ReduxAlphaChannel_hq2x( scaledImgBuf, 128, 128 ); } wt_snprintf( tempFileName, sizeof( tempFileName ), "%s%c%.3d.tga", spritePath, PATH_SEP, GetSpriteMappedIndex( i - SpriteStart ) ); TGA_write( tempFileName, 32, 128, 128, scaledImgBuf, 0, 1 ); MM_FREE( scaledImgBuf ); } else { wt_snprintf( tempFileName, sizeof( tempFileName ), "%s%c%.3d.tga", spritePath, PATH_SEP, GetSpriteMappedIndex( i - SpriteStart ) ); TGA_write( tempFileName, 32, 64, 64, decdata, 0, 1 ); } MM_FREE( data ); MM_FREE( decdata ); } // //////////////////////////////////////////////////////////////////////// // Decode SFX soundBufferSize = 20 * 4096; soundBuffer = (PW8) MM_MALLOC( soundBufferSize ); if( soundBuffer == NULL ) { PageFile_Shutdown(); return false; } totallength = 0; for( i = SoundStart ; i < NumBlocks ; ++i ) { data = PageFile_getPage( i, &length ); if( data == NULL ) { continue; } if( (totallength + length) > soundBufferSize ) { fprintf( stderr, "[PageFile_ReduxDecodePageData]: Buffer not large enough to hold sound data!\n" ); MM_FREE( data ); MM_FREE( soundBuffer ); return false; } MM_MEMCPY( soundBuffer + totallength, data, length ); totallength += length; if( length < 4096 ) { wt_snprintf( tempFileName, sizeof( tempFileName ), "%s%c%.3d.wav", soundPath, PATH_SEP, i - SoundStart ); wav_write( tempFileName, soundBuffer, totallength, 1, SAMPLERATE, 1 ); totallength = 0; } MM_FREE( data ); } MM_FREE( soundBuffer ); PageFile_Shutdown(); MM_FREE( data ); printf( "Done\n" ); return true; }
static int wave_write(struct audio_output_struct* ao, unsigned char *bytes, int count) { return wav_write(bytes, count); }
/** * \brief Decode music chunks * \param[in] start Start of music chunks. * \param[in] end End of music chunks. * \param[in] songNames Song titles. * \return On success true, otherwise false. */ PUBLIC wtBoolean AudioFile_ReduxDecodeMusic( const W32 start, const W32 end, const char *path, char *songNames[] ) { SW8 *buffChunk; void *buffWav; W32 i; W32 length; char filename[ 1024 ]; W32 uncompr_length; printf( "Decoding Music (This could take a while)..." ); if( ! ADLIB_Init( 44100 ) ) { return false; } for( i = start ; i < end ; ++i ) { buffChunk = (PSW8) AudioFile_CacheAudioChunk( i ); if( buffChunk == NULL ) { continue; } uncompr_length = ADLIB_getLength( buffChunk ); if( uncompr_length <= 1 ) { MM_FREE( buffChunk ); continue; } ADLIB_LoadMusic( buffChunk ); buffWav = MM_MALLOC( uncompr_length * 64 * 2 ); if( buffWav == NULL ) { MM_FREE( buffChunk ); continue; } length = ADLIB_UpdateMusic( uncompr_length, buffWav ); #ifdef BIG_ENDIAN_SYSTEM AudioFile_dataByteSwap( buffWav, length ); #endif // Save audio buffer if( _saveMusicAsWav ) { if( songNames ) { wt_snprintf( filename, sizeof( filename ), "%s%c%s.wav", path, PATH_SEP, songNames[ i - start ] ); } else { wt_snprintf( filename, sizeof( filename ), "%s%c%d.wav", path, PATH_SEP, i - start ); } wav_write( filename, buffWav, length, 1, 44100, 2 ); } else { if( songNames ) { wt_snprintf( filename, sizeof( filename ), "%s%c%s.ogg", path, PATH_SEP, songNames[ i - start ] ); } else { wt_snprintf( filename, sizeof( filename ), "%s%c%d.ogg", path, PATH_SEP, i - start ); } vorbis_encode( filename, buffWav, length, 1, 16, 44100, 0, 0, 0 ); } MM_FREE( buffWav ); MM_FREE( buffChunk ); } ADLIB_Shutdown(); printf( "Done\n" ); return true; }
/** * \brief Decode sound fx. * \param[in] start Start of sound fx chunks. * \param[in] end End of sound fx chunks. * \param[in] path Directory path to save file to. * \return On success true, otherwise false. */ PUBLIC wtBoolean AudioFile_ReduxDecodeSound( const W32 start, const W32 end, const char *path ) { SW8 *buffChunk; void *buffWav; W32 i; W32 length; char filename[ 1024 ]; printf( "Decoding Sound FX..." ); if( ! ADLIB_Init( 22050 ) ) { return false; } for( i = start ; i < end ; ++i ) { buffChunk = (PSW8) AudioFile_CacheAudioChunk( i ); if( buffChunk == NULL ) { continue; } buffWav = ADLIB_DecodeSound( (AdLibSound *)buffChunk, &length ); if( buffWav == NULL ) { MM_FREE( buffChunk ); continue; } #ifdef BIG_ENDIAN_SYSTEM AudioFile_dataByteSwap( buffWav, length ); #endif if( _saveAudioAsWav ) { wt_snprintf( filename, sizeof( filename ), "%s%c%.3d.wav", path, PATH_SEP, GetSoundMappedIndex( i - start ) ); wav_write( filename, buffWav, length, 1, 22050, 2 ); } else { wt_snprintf( filename, sizeof( filename ), "%s%c%.3d.ogg", path, PATH_SEP, GetSoundMappedIndex( i - start ) ); vorbis_encode( filename, buffWav, length, 1, 16, 22050, 0, 0, 0 ); } MM_FREE( buffWav ); MM_FREE( buffChunk ); } ADLIB_Shutdown(); printf( "Done\n" ); return true; }
/*////////////////////////*/ unsigned int __stdcall detect(void *a){ char flag1 =0; char flag2 =0; long counter =0; int size; int size2; short *buf; char through_flag=1; DWORD msg; char szFullPath[MAX_PATH] = {'\0'}; char *szFilePart; //各種設定 in outで共通 wfe.wFormatTag = WAVE_FORMAT_PCM; wfe.nChannels = 1;//モノラル 1 or 2 wfe.nSamplesPerSec = SRATE;//サンプリング周波数 wfe.wBitsPerSample = BITS;// 8 or 16 wfe.nBlockAlign = wfe.nChannels * wfe.wBitsPerSample / 8;//byte/sample wfe.nAvgBytesPerSec = wfe.nSamplesPerSec * wfe.nBlockAlign;//SRATE;//1秒間のバイト数 bufferIN = (short*)calloc(1,wfe.nAvgBytesPerSec);//1秒分のバッファを用意 bufferIN2 = (short*)calloc(5,wfe.nAvgBytesPerSec);//1秒分のバッファを用意 ////////////////////////// //入力準備 whdrIn.lpData = (LPSTR)bufferIN; whdrIn.dwBufferLength = wfe.nAvgBytesPerSec; waveInOpen(&hWaveIn , WAVE_MAPPER , &wfe , (DWORD)waveInProc , 0 , CALLBACK_FUNCTION); waveInPrepareHeader(hWaveIn , &whdrIn , sizeof(WAVEHDR)); waveInAddBuffer(hWaveIn , &whdrIn , sizeof(WAVEHDR)); waveInStart(hWaveIn);//録音開始 whdrIn2.lpData = (LPSTR)bufferIN2; whdrIn2.dwBufferLength = wfe.nAvgBytesPerSec*5; waveInOpen(&hWaveIn2 , WAVE_MAPPER , &wfe , (DWORD)waveInProc2 , 0 , CALLBACK_FUNCTION); waveInPrepareHeader(hWaveIn2 , &whdrIn2 , sizeof(WAVEHDR)); waveInAddBuffer(hWaveIn2 , &whdrIn2 , sizeof(WAVEHDR)); ///////// through_flag=1; while(detection_flag){ while(detect_thread_flag){ for(int i=0;i<SRATE;i++) { // printf("%d\n",(int)bufferIN[i]); if(abs((int)bufferIN[i])>THRESHOLD){ flag1=1; break; } } if(flag1){ if(through_flag){ printf("speak:\n"); waveInStart(hWaveIn2);//録音開始 through_flag=0; flag2=1; } }else{ if(flag2){ printf("end\n"); flag2=0; counter=0; size2 = (SRATE*10); // 5秒間 wav_write("rec.wav", bufferIN2,size2); printf("created wave file\n"); _fullpath(szFullPath, "..\\rec.wav", sizeof(szFullPath)/sizeof(szFullPath[0])); send(sock,szFullPath,sizeof(szFullPath)/sizeof(szFullPath[0]), 0); waveInAddBuffer(hWaveIn , &whdrIn , sizeof(WAVEHDR)); waveInAddBuffer(hWaveIn2 , &whdrIn2 , sizeof(WAVEHDR)); through_flag=1; detect_thread_flag=0; } } flag1=0; Sleep(100); } Sleep(100); } //入力解放 waveInUnprepareHeader(hWaveIn2 , &whdrIn2 , sizeof(WAVEHDR)); waveInStop(hWaveIn); waveInClose(hWaveIn2); waveInUnprepareHeader(hWaveIn , &whdrIn , sizeof(WAVEHDR)); waveInStop(hWaveIn); waveInClose(hWaveIn); // free(bufferIN); // free(bufferIN2); detection_flag=0; detect_thread_flag=0; _endthreadex(0); return 0; }