void CRipInfoDB::DeleteTrack( int nTrack ) { // Open file for append m_pFile = CDexOpenFile( m_strFileName, _W( "r" ) ); CUStringConvert strCnv; if ( NULL != m_pFile ) { fclose( m_pFile ); CDexCopyFile( m_strFileName, m_strFileName + _W( ".bak") ); CString test( GetLastErrorString() ); m_pFile = CDexOpenFile( m_strFileName + _W( ".bak"), _W( "r" ) ); FILE* pFileOut = CDexOpenFile( m_strFileName, _W( "w+" ) ); while( ( !feof( m_pFile ) ) && ( pFileOut != NULL ) ) { TCHAR lpszLine[ 1024 ] = {'\0',}; int nCurrentTrack; // Clear string memset( lpszLine, 0x00, sizeof( lpszLine ) ); // Read the next line from the file _fgetts( lpszLine, sizeof( lpszLine ), m_pFile ); nCurrentTrack= _ttoi( &lpszLine[ 1 ] ); if ( nCurrentTrack != nTrack ) { _fputts( lpszLine, pFileOut ); } } fclose( m_pFile ); m_pFile = NULL; fclose( pFileOut ); m_pFile = NULL; //::DeleteFile( strCnv.ToT( m_strFileName ) ); //::CopyFile( strCnv.ToT( CUString( m_strFileName + _T( ".bak") ) ) , strCnv.ToT( m_strFileName ), FALSE ); CDexDeleteFile( m_strFileName + _W( ".bak") ); } if ( m_pFile ) { fclose( m_pFile ); m_pFile = NULL; } }
void CWAV::CloseNormalizeAudioFile(CUString strFileName, BOOL bAbort, BOOL bReplace) { // Close input stream CloseStream(); // Close temp output stream m_pTmpWav->CloseStream(); delete m_pTmpWav; m_pTmpWav = NULL; if ( bAbort ) { // Delete temp file if ( CDexDeleteFile(strFileName + AUDIO_FILE_NRMFILEEXT ) == 0 ) { } } else { if ( bReplace ) { // Delete un-normalized file name if ( CDexDeleteFile( strFileName + _W( ".wav" ) ) == 0 ) { } // Rename output file if ( CDexMoveFile( strFileName + AUDIO_FILE_NRMFILEEXT, strFileName + _W( ".wav" ) ) ==0 ) { CUString strMsg = GetLastErrorString(); // Display the string. CDexMessageBox( strMsg, MB_OK | MB_ICONINFORMATION); } } } }
void CRiffWavToMP3::OnTimer(UINT nIDEvent) { // Obtain the number of files if (m_Pos!=NULL) { // Get the file name to be converted CUString strFileName=m_pFileDlg->GetNextPathName(m_Pos); CUString strOrig(strFileName); // Update progress bar m_Progress.SetPos(m_nCurFile++); // Set current file name CUStringConvert strCnv; m_processingFile.SetWindowText( strCnv.ToT( strFileName ) ); // Update controls UpdateData(FALSE); CWAV myWav; // Strip extension int nPos=strFileName.ReverseFind('.'); // Stip it if (nPos>0) strFileName=strFileName.Left(nPos); // Do the conversion if (myWav.StripRiffWavHeader(strFileName)==CDEX_OK) { // delete the original file? if (m_pFileDlg->m_bDeleteOriginal) CDexDeleteFile(strOrig); } } else { OnCancel(); } CDialog::OnTimer(nIDEvent); }
CDEX_ERR CWAV::StripRiffWavHeader(CUString strFileName) { FILE* fpIn = CDexOpenFile( strFileName + _W( ".wav" ), _W( "rb" ) ); if (fpIn == NULL) { CUString strLang; CUString strMsg; strLang = g_language.GetString( IDS_ERROR_COULDNOTOPENFILE ); strMsg.Format( strLang, (LPCWSTR)( strFileName + _W( ".wav" ) ) ); CDexMessageBox( strMsg ); return CDEX_ERROR; } // Get file size; fseek(fpIn, 0, SEEK_END); int nFileSize = ftell( fpIn ); // seek back to beginning of the file fseek(fpIn, 0, SEEK_SET); // read first 8 bytes fread( gpsSamples, 1, 8, fpIn ); // check if this is a RIFF-WAVE file if (strstr((LPSTR)gpsSamples, "RIFF") == NULL) { CUString strLang; CUString strMsg; strLang = g_language.GetString( IDS_ERROR_NOTARIFFWAVFILE ); strMsg.Format( strLang, (LPCWSTR)( strFileName + _W( ".wav" ) ) ); CDexMessageBox( strMsg ); fclose( fpIn ); return CDEX_ERROR; } // read following 4 bytes fread(gpsSamples, 1, 4, fpIn); if ( NULL == strstr( (LPSTR)gpsSamples, "WAVE" ) ) { CUString strLang; CUString strMsg; strLang = g_language.GetString( IDS_ERROR_NOTARIFFWAVFILE ); strMsg.Format( strLang, (LPCWSTR)( strFileName + _W( ".wav" ) ) ); CDexMessageBox( strMsg ); fclose( fpIn ); return CDEX_ERROR; } fread(gpsSamples, 1, 4 + 4, fpIn); if (strstr((LPSTR)gpsSamples, "fmt") == NULL) { CUString strLang; CUString strMsg; strLang = g_language.GetString( IDS_ERROR_COULDNOTFINDFORMATTAG ); strMsg.Format( strLang, (LPCWSTR)( strFileName + _W( ".wav " ) ) ); CDexMessageBox( strMsg ); fclose( fpIn ); return CDEX_ERROR; } WAVEFORMATEX wfx; // read WAVE Header fread(&wfx, sizeof(wfx), 1, fpIn); if (wfx.wFormatTag != WAVE_FORMAT_MPEGLAYER3 && wfx.wFormatTag != WAVE_FORMAT_MPEG) { CUString strLang; CUString strMsg; strLang = g_language.GetString( IDS_ERROR_NOTARIFFWAVFILE ); strMsg.Format( strLang, (LPCWSTR)( strFileName + _W( ".wav" ) ) ); CDexMessageBox( strMsg ); fclose( fpIn ); return CDEX_ERROR; } // seek data tag int i = 8; BOOL bFound = FALSE; while ((i < nFileSize) && (bFound == FALSE)) { fseek(fpIn, i, SEEK_SET); fread(gpsSamples, 1, 4, fpIn); if ( NULL != strstr( (LPSTR)gpsSamples, "data" ) ) { // Skip another 4 bytes for data value bFound = TRUE; fseek(fpIn, i + 8, SEEK_SET); } i++; } if (i>nFileSize - 4) { fclose(fpIn); CUString strLang; strLang = g_language.GetString( IDS_ERROR_DATATAGNOTFOUND ); CDexMessageBox( strLang ); return CDEX_ERROR; } // And delete the WAV file first CDexDeleteFile( strFileName + _W( ".mp3" ) ); // Create output file FILE* fpOut = CDexOpenFile( strFileName + _W( ".mp3" ), _W( "wb" ) ); // How many samples to convert DWORD dwSamplesToConvert =(nFileSize - ftell(fpIn)); while (dwSamplesToConvert>0) { // Determine how many byte to convert this time DWORD dwBlockSize = min(dwSamplesToConvert, BLOCKSIZE); // read chunck fread(gpsSamples, dwBlockSize, sizeof(BYTE), fpIn); // And write the byte back to the file fwrite(gpsSamples, dwBlockSize, sizeof(BYTE), fpOut); // Decrement the number of bytes to convert dwSamplesToConvert -= dwBlockSize; } // And close the wave file fclose(fpIn); fclose(fpOut); return CDEX_OK; }
CDEX_ERR CWAV::ConvertToRiffWav( CUString strFileName, int nBitRate, int nSampleRate, WORD wChannels, int nLayer ) { DWORD dwID3TagOffset = 0; CUString strInFileName; if (0 == nLayer) { strInFileName = strFileName + _W( ".scx" ); } else if ( 3 == nLayer ) { strInFileName = strFileName + _W( ".mp3" ); } else { strInFileName = strFileName + _W( ".mp2" ); } // Check presense of ID3V2 tag // Get MP3 ID3V2 Tag offset CID3Tag ID3Tag; ID3Tag.Init(); ID3Tag.OpenFile(strInFileName); ID3Tag.LoadTagProperties(); if (ID3Tag.IsV2Tag()) { dwID3TagOffset = ID3Tag.GetTagSize(); } FILE* fpIn = NULL; fpIn = CDexOpenFile( strInFileName, _W( "rb" ) ); CUString strRiffWav( strFileName + _W( ".wav" ) ); // And delete the WAV file first CDexDeleteFile( strFileName + _W( ".wav" ) ); FILE* fpOut = CDexOpenFile( strRiffWav, _W( "wb" ) ); if ( NULL == fpIn || NULL == fpOut ) { ASSERT(FALSE); return CDEX_ERROR; } // Get number of samples in file fseek( fpIn, 0L, SEEK_END ); // Get current file position DWORD dwSamplesToConvert= ftell( fpIn ); // Go back to the start of the file, but skip ID3V2 tag fseek( fpIn, 0, SEEK_SET ); // Set this field to resulting files size - sizeof(CHUNKHEADER) // Thus total file size = 3*sizeof(CHUNCHEADER)+sizeof(MP3WAVEFORMAT)+ dwSamplesToConvert DWORD dwRiffSize; DWORD dwFmtSize; if ( 0 == nLayer ) { dwFmtSize= sizeof( SONYSCXWAVEFORMAT ); } else if ( 2 == nLayer ) { dwFmtSize= sizeof( MPEG1WAVEFORMAT ); } else { dwFmtSize= sizeof( MPEGLAYER3WAVEFORMAT ); } dwRiffSize = dwSamplesToConvert + dwFmtSize + 32; // 32 bytes for TAGS ( excluding RIFF WAV ) // Write header to output file UNICODE fprintf( fpOut, "RIFF" ); // 4 bytes fwrite( &dwRiffSize, sizeof( DWORD ), 1, fpOut ); // 4 bytes fprintf( fpOut, "WAVE" ); // 4 bytes fprintf( fpOut, "fmt " ); // 4 bytes fwrite( &dwFmtSize, sizeof( DWORD ), 1, fpOut ); // 4 bytes if ( 0 == nLayer ) { SaveAtracTag( fpOut, nBitRate, nSampleRate, wChannels ); } else if (nLayer == 2) { SaveMP2Tag( fpOut, nBitRate, nSampleRate, wChannels ); } else { SaveMP3Tag( fpOut, nBitRate, nSampleRate, wChannels ); } DWORD dwTmp = 4; /* total chunk size -4 bytes for fact, -4 bytes for length */ fprintf(fpOut, "fact" ); // 4 bytes fwrite( &dwTmp, sizeof( DWORD ), 1, fpOut ); // 4 bytes if ( 3 == nLayer ) { // Round to the number of frames dwTmp = dwSamplesToConvert/1152/2/wChannels; dwTmp *= 1152 * 2 * wChannels; // Estimate the amount of samples dwTmp *= ( (DWORD)nSampleRate * 2 * (DWORD)wChannels ); dwTmp /= ( nBitRate * 1000 / 8 ); } else { DWORD dwSampleBitRate = (DWORD)nSampleRate * (DWORD)wChannels * 16; double dCompressionFactor = (double)dwSampleBitRate / ((double)nBitRate * 1000.0 ); // Round to the number of frames dwTmp = dwSamplesToConvert / 384; dwTmp *= 384; // Estimate the amount of samples // dwTmp*=((DWORD)nSampleRate*2*(DWORD)wChannels); // dwTmp/=(nBitRate*1000/8); dwTmp =(DWORD)( (double)dwTmp * dCompressionFactor ); dwTmp /= 2; /* from bytes to samples */ dwTmp /=(DWORD)wChannels; /* in samples per channel */ } fwrite( &dwTmp, sizeof( DWORD ), 1, fpOut ); // write data tag fprintf( fpOut, "data" ); // 4 bytes fwrite( &dwSamplesToConvert, sizeof( DWORD ), 1, fpOut ); // 4 bytes while ( dwSamplesToConvert > 0 ) { // Determine how many byte to convert this time DWORD dwBlockSize = min( dwSamplesToConvert, BLOCKSIZE ); // read chunck fread( gpsSamples, dwBlockSize, sizeof( BYTE ), fpIn ); // And write the byte back to the file fwrite( gpsSamples, dwBlockSize, sizeof( BYTE ), fpOut ); // Decrement the number of bytes to convert dwSamplesToConvert -= dwBlockSize; } // flush the files fflush( fpIn ); fflush( fpOut ); // And close the wave file fclose( fpIn ); fclose( fpOut ); return CDEX_OK; }