0
0

Hello,

I got a bit of a problem trying to do the following:

  1. Load a .xm or .it song
  2. get all samples, pack as wave and save them to separate files

problem ( i suppose):
sample speed (freq) differs in the song and from the retrieved value.
When I run the code below, all samps are marked 44100 but I know
some of those in the song are 8khz, 11 or etc.
The output wave just doesn’t sound as the original.

I’m using much of the code as in the ‘record’ example.

[code:3t8p4igv]

BOOL CSampleFM::SaveToWave( LPCTSTR lpszOutputFileName )
{
ASSERT( m_pSample != NULL );
ASSERT( lpszOutputFileName != NULL );

int nDefFreq, nVarFreq;
FSOUND_Sample_GetDefaultsEx( m_pSample, &nDefFreq, 0, 0, 0,
                                        &nVarFreq, 0, 0);

void *p1 = NULL, 
     *p2 = NULL;
UINT len1, len2;

UINT uiMode = FSOUND_Sample_GetMode( m_pSample );
int bits     = (uiMode & FSOUND_16BITS) ? 16 : 8;
int channels = (uiMode & FSOUND_STEREO) ? 2  : 1;
UINT uiLength = FSOUND_Sample_GetLength( m_pSample ) * channels * bits / 8;

if ( uiLength == 0 )
{
    // dont save 0 len sams
    TRACE( _T("\n sample won't be saved. sample length: %d"), uiLength );
    return FALSE;
}

// Wave Stuff (as from fmod sample)

#if defined(WIN32) || defined(_WIN64) || defined(__WATCOMC__) || defined(_WIN32) || defined(__WIN32__)
#pragma pack(1)
#endif

typedef struct
{
    signed char id[4];
    int         size;
} RiffChunk;

struct
{
    RiffChunk       chunk;
    unsigned short  wFormatTag;             /* format type  */
    unsigned short  nChannels;              /* number of channels (i.e. mono, stereo...)  */
    unsigned int    nSamplesPerSec;         /* sample rate  */
    unsigned int    nAvgBytesPerSec ;       /* for buffer estimation  */
    unsigned short  nBlockAlign;            /* block size of data  */
    unsigned short  wBitsPerSample;         /* number of bits per sample of mono data */
} FmtChunk  = { 
    {  {'f','m','t',' '}, sizeof(FmtChunk) - sizeof(RiffChunk)       }, 
    1, channels, nDefFreq, 
    nDefFreq * channels * bits / 8, 
    1 * channels * bits / 8, 
    bits 
};

struct
{
    RiffChunk   chunk;
} DataChunk = { {{'d','a','t','a'}, uiLength } };

struct
{
    RiffChunk   chunk;
    signed char rifftype[4];
} WavHeader = { 
    {
        {'R','I','F','F'}, 
        sizeof(FmtChunk) + sizeof(RiffChunk) + uiLength 
    }, 
    {'W','A','V','E'} 
};

#if defined(WIN32) || defined(_WIN64) || defined(__WATCOMC__) || defined(_WIN32) || defined(__WIN32__)
#pragma pack()
#endif

// save to file
CFile file;
if ( !file.Open( lpszOutputFileName, CFile::modeCreate | CFile::modeWrite ) )
{
    return FALSE;
}

// write headers
file.Write( &WavHeader, sizeof(WavHeader) );
file.Write( &FmtChunk, sizeof(FmtChunk) );
file.Write( &DataChunk, sizeof(DataChunk) );

FSOUND_Sample_Lock( m_pSample, 0, uiLength, &p1, &p2, &len1, &len2 );

file.Write( p1, len1 );

FSOUND_Sample_Unlock( m_pSample, p1, p2, len1, len2 );

file.Close();

return TRUE;

}
[/code:3t8p4igv]

Any help will be greatly appreciated, thanx ๐Ÿ˜‰

  • You must to post comments
0
0

wow, thanx a lot brett. I think this did it ๐Ÿ˜€
I really don’t have much experience with sound programming ๐Ÿ˜ฅ

  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.