0
0

Hello, I am seeking just a short, simple example of how to stream PCM audio from an AVI file using Fmod Ex.

  • You must to post comments
0
0

As I pointed out, Jamotto’s example is NOT a working segment of code. You will also need to read the appropriate AVI stream header (e.g., for stream 01) and configure FMOD to expect the format specified in the WAVEFORMATPCM structure (which is located 48 + 4 bytes after the stream header).

Regards,

  • SD
  • You must to post comments
0
0

That would be a shame, I have code that worked with Fmod 3.74. I had hoped that I could port the code to the new Fmod.

  • You must to post comments
0
0

I have heard of Directshow’s reputation for not being easy to work with. So does Fmod support any Audio/Video container that you don’t have to rely on directshow, Matroska perhaps?

  • You must to post comments
0
0

Well it appears I have gotten it working now ๐Ÿ˜ณ So I guess you can delete this thread if you wish.

  • You must to post comments
0
0

[i:2ath3d3f]jamotto[/i:2ath3d3f] did you get Fmod 4 play avi?

[i:2ath3d3f]Brett[/i:2ath3d3f]
I have some ideas that Fmod take care of playing the “audiotrack” in a video file (DivX or Mpeg) and then Fmod “tells” my DirectX code that shows/plays the video where we are in position. Is that possible or do you have some other solution ?

  • You must to post comments
0
0

[quote="Ken":4mf3mdeu][i:4mf3mdeu]jamotto[/i:4mf3mdeu] did you get Fmod 4 play avi?
[/quote:4mf3mdeu]

Yes I got it running now.

  • You must to post comments
0
0

like to share how you did it?

  • You must to post comments
0
0

This works with Dev C++ 4.9.9.2 using the C interface of Fmod 4, you need to link in libvfw32.a

Thanks to whoever created the PlayStream Example that this is based off.

[code:icjvua37]
/*===============================================================================================
PlayStream Example
Copyright (c), Firelight Technologies Pty, Ltd 2004-2005.

This example shows how to simply play a stream, such as an mp3 or wav.
The stream behaviour is achieved by specifying FMOD_CREATESTREAM in the call to
FMOD_System_CreateSound.
This makes FMOD decode the file in realtime as it plays, instead of loading it all at once.
This uses far less memory, in exchange for a small runtime cpu hit.
===============================================================================================*/

include "fmod.h"

include "fmod_errors.h"

include <stdio.h>

include <conio.h>

include <vfw.h> //AVI functions defined here

define AVI_DEBUG false

void ERRCHECK(FMOD_RESULT result)
{
if (result != FMOD_OK)
{
printf("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
exit(-1);
}
}

int main(int argc, char *argv[])
{
FMOD_SYSTEM *system;
FMOD_SOUND *sound;
FMOD_CHANNEL *channel = 0;
FMOD_RESULT result;
int key;
unsigned int version;

PAVIFILE pAviFile;
AVIFILEINFO avi_file_info;
PAVISTREAM pAudio;
AVISTREAMINFO stream_info;
LPBYTE pBuffer;

/*fmod definitions*/
FMOD_CREATESOUNDEXINFO exinfo;

/*
    Create a System object and initialize.
*/
result = FMOD_System_Create(&amp;system);
ERRCHECK(result);

result = FMOD_System_GetVersion(system, &amp;version);
ERRCHECK(result);

if (version &lt; FMOD_VERSION)
{
    printf(&quot;Error!  You are using an old version of FMOD %08x.  This program requires %08x\n&quot;, version, FMOD_VERSION);
    return 0;
}

result = FMOD_System_Init(system, 1, FMOD_INIT_NORMAL, NULL);
ERRCHECK(result);

/*********************************************************************************************************/

/begin AVI read functions/
AVIFileOpen(&pAviFile, "test2.avi", OF_READ, NULL);//open somefile.avi

//open the audio stream
if(AVIFileGetStream(pAviFile,&pAudio,streamtypeAUDIO,0))
{
printf("Error getting audio stream. \n");
}

//get some info about the stream
if(AVIStreamInfo(pAudio, &stream_info, sizeof(stream_info)))
{
printf("Error getting avi stream info. \n");
}

long Samples_per_second = stream_info.dwRate/stream_info.dwScale;
float Length_in_seconds = (float)stream_info.dwLength * (float)stream_info.dwScale/(float)stream_info.dwRate;
float buffersize=Length_in_seconds * (float)Samples_per_second; /*figure out how large the buffer has too be here*/


#if(AVI_DEBUG)
puts(&quot;AVISTREAMINFO:&quot;);
printf(&quot;Scale is %ld \n&quot;,stream_info.dwScale);
printf(&quot;Rate is %ld \n&quot;,stream_info.dwRate);
printf(&quot;Start is %ld \n&quot;,stream_info.dwStart);
printf(&quot;Length is %ld \n&quot;,stream_info.dwLength);
printf(&quot;Rate in samples per second is %ld \n&quot;,Samples_per_second);
printf(&quot;Length in seconds is %.02f \n&quot;,Length_in_seconds);
printf(&quot;Buffer is length x rate = %.02f samples \n&quot;,Length_in_seconds*(float)Samples_per_second );
printf(&quot;Audio skew is %ld \n&quot;,stream_info.dwInitialFrames);
printf(&quot;Suggested buffer size is %ld \n&quot;,stream_info.dwSuggestedBufferSize);
#endif

long aSize; //get the size of the audio stream

/start of sound ini/
if(AVIStreamReadFormat(pAudio,AVIStreamStart(pAudio),NULL,&aSize))
{
printf("Error with stream format. \n");
}

LPBYTE pChunk = new BYTE[aSize];

if(AVIStreamReadFormat(pAudio,AVIStreamStart(pAudio),pChunk,&amp;aSize))
{
    printf(&quot;Error with stream format. \n&quot;);
}

LPWAVEFORMATEX pWaveFormatex = (LPWAVEFORMATEX) pChunk;


#if(AVI_DEBUG)
puts(&quot;Wave format data: &quot;);
printf(&quot;Format tag is 0 x %x \n&quot;,pWaveFormatex-&gt;wFormatTag);
printf(&quot;Number of channels are %d \n&quot;,pWaveFormatex-&gt;nChannels);
printf(&quot;Bits per sample are %d \n&quot;,pWaveFormatex-&gt;wBitsPerSample);
printf(&quot;Samples per second are %ld \n&quot;,pWaveFormatex-&gt;nSamplesPerSec);
printf(&quot;Average bytes per second are %ld \n&quot;,pWaveFormatex-&gt;nAvgBytesPerSec);
printf(&quot;Block Align is %d \n&quot;,pWaveFormatex-&gt;nBlockAlign);
printf(&quot;CB size is %d \n&quot;,pWaveFormatex-&gt;cbSize);
#endif

long lASize;
if(AVIStreamRead(pAudio,0,(LONG)buffersize,NULL,0,&lASize,NULL))
{
printf("Error with stream read. \n");
}
#if(AVI_DEBUG)
printf("Buffer size is %ld. \n",lASize);
#endif

long plSamples;
long plBytes;

pBuffer = new BYTE[lASize];//create buffer for audio

if(AVIStreamRead(pAudio,0,(LONG)buffersize,pBuffer,lASize,&plBytes,&plSamples))
{
printf("ERROR. \n");
}
#if(AVI_DEBUG)
printf("The number of samples read were %ld \n",plSamples);
printf("The number of bytes read were %ld \n",plBytes);
#endif

/start loading sound stream into Fmod/
memset(&exinfo, 0, sizeof(FMOD_CREATESOUNDEXINFO));
exinfo.cbsize = sizeof(FMOD_CREATESOUNDEXINFO);
exinfo.length = lASize;

result = FMOD_System_CreateStream(system ,(const char *)pBuffer , FMOD_OPENMEMORY, &amp;exinfo , &amp;sound);
ERRCHECK(result);

printf(&quot;====================================================================\n&quot;);
printf(&quot;PlayStream Example.  Copyright (c) Firelight Technologies 2004-2005.\n&quot;);
printf(&quot;====================================================================\n&quot;);
printf(&quot;\n&quot;);
printf(&quot;Press space to pause, Esc to quit\n&quot;);
printf(&quot;\n&quot;);

/*
    Play the sound.
*/

result = FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, sound, 0, &amp;channel);
ERRCHECK(result);

/*
    Main loop.
*/
do
{
    if (kbhit())
    {
        key = getch();

        switch (key)
        {
            case ' ' :
            {
                int paused;
                FMOD_Channel_GetPaused(channel, &amp;paused);
                FMOD_Channel_SetPaused(channel, !paused);
                break;
            }
        }
    }

    FMOD_System_Update(system);

    if (channel)
    {
        unsigned int ms;
        unsigned int lenms;
        int          playing;
        int          paused;

        FMOD_Channel_IsPlaying(channel, &amp;playing);
        if ((result != FMOD_OK) &amp;&amp; (result != FMOD_ERR_INVALID_HANDLE) &amp;&amp; (result != FMOD_ERR_CHANNEL_STOLEN))
        {
            ERRCHECK(result);
        }

        result = FMOD_Channel_GetPaused(channel, &amp;paused);
        if ((result != FMOD_OK) &amp;&amp; (result != FMOD_ERR_INVALID_HANDLE) &amp;&amp; (result != FMOD_ERR_CHANNEL_STOLEN))
        {
            ERRCHECK(result);
        }

        result = FMOD_Channel_GetPosition(channel, &amp;ms, FMOD_TIMEUNIT_MS);
        if ((result != FMOD_OK) &amp;&amp; (result != FMOD_ERR_INVALID_HANDLE) &amp;&amp; (result != FMOD_ERR_CHANNEL_STOLEN))
        {
            ERRCHECK(result);
        }

        result = FMOD_Sound_GetLength(sound, &amp;lenms, FMOD_TIMEUNIT_MS);
        if ((result != FMOD_OK) &amp;&amp; (result != FMOD_ERR_INVALID_HANDLE) &amp;&amp; (result != FMOD_ERR_CHANNEL_STOLEN))
        {
            ERRCHECK(result);
        }

        printf(&quot;Time %02d:%02d:%02d/%02d:%02d:%02d : %s\r&quot;, ms / 1000 / 60, ms / 1000 % 60, ms / 10 % 100, lenms / 1000 / 60, lenms / 1000 % 60, lenms / 10 % 100, paused ? &quot;Paused &quot; : playing ? &quot;Playing&quot; : &quot;Stopped&quot;);
    }

    Sleep(10);

} while (key != 27);

printf(&quot;\n&quot;);

/*
    Shut down
*/
result = FMOD_Sound_Release(sound);
ERRCHECK(result);
result = FMOD_System_Close(system);
ERRCHECK(result);
result = FMOD_System_Release(system);
ERRCHECK(result);

return 0;

}[/code:icjvua37]

  • You must to post comments
0
0

๐Ÿ˜• Huh?

I don’t understand how that code does it, because the only thing specified for fmod is the length of the audio data.

What if the (AVI) audio stream is stereo vs. mono? 16-bit vs. 24-bit? 44kHz vs. 32kHz? MPEG vs. uncompressed PCM?

Or, do you mean it just happens to work with AVIs with a particular type of audio stream?

Please advise,

  • SD
  • You must to post comments
0
0

[quote:2dx4galx]What if the (AVI) audio stream is stereo vs. mono?[/quote:2dx4galx]
The number channels should be stored in
[code:2dx4galx] pWaveFormatex->nChannels[/code:2dx4galx] but as far as how to extract the second or more channels I’m not sure. My guess is that you would need two copies of pAudio then call this function twice with slight changes
[code:2dx4galx]AVIFileGetStream(pAviFile, pAudio[0],streamtypeAUDIO,0) [/code:2dx4galx]
[code:2dx4galx]AVIFileGetStream(pAviFile, pAudio[1],streamtypeAUDIO,1) [/code:2dx4galx]

then you would have to create two copies everywhere where pAudio is used to handle each stream, I think then that would handle two channels but I could most certainly be wrong ๐Ÿ˜‰

[quote:2dx4galx]
16-bit vs. 24-bit?
[/quote:2dx4galx]
pWaveFormatex->wBitsPerSample should hold that info.

[quote:2dx4galx] 44kHz vs. 32kHz?
[/quote:2dx4galx]

pWaveFormatex->nSamplesPerSec would hold that

[quote:2dx4galx]
MPEG vs. uncompressed PCM?
[/quote:2dx4galx]
pWaveFormatex->wFormatTag holds what format it is in.

[quote:2dx4galx]
Or, do you mean it just happens to work with AVIs with a particular type of audio stream?[/quote:2dx4galx]
This code will play mono MP3 and OGG encoded avi’s if you wanted to play PCM you would have to add FMOD_OPENRAW to [code:2dx4galx]FMOD_System_CreateStream(system ,(const char *)pBuffer , FMOD_OPENMEMORY , &exinfo , &sound); [/code:2dx4galx] according to the fmod manual.

So in the end if you want to make this more flexible, you could just pull all of the info you need to setup Fmod from the pWaveFormatex structure

  • You must to post comments
0
0

I can’t play avi’s with PCM sound with your example. It always returns format error.
Is there any way to play any sound format?
Thanks!

  • You must to post comments
Showing 11 results
Your Answer

Please first to submit.