Example #1
0
static void soundPlay(struct Sound *s,char *filename,int fileofs,int filesize,int loop)
{
    // read sound file header
    int err;
    fileHandle fh=0;
    long fsize;
    #define HEADSIZE 78
    unsigned char buf[HEADSIZE];
    memset(buf,0,HEADSIZE);
#ifdef EXTENDFILE
    rfSuperStart=fileofs;
    rfSuperSize=filesize;
#endif
    s->loop=loop;
    #define MCALL1(f) err=f;if(err) {reportErr(err); return;}
    #define MCALL2(f) err=f;if(err) {reportErr(err); err=fileClose(fh); if(err) reportErr(err); return;}
    MCALL1(fileOpen(filename,fileOpenRead,&fh));
    MCALL2(fileGetSize(fh,&fsize));
    MCALL2(fileRead(fh,buf,fsize<HEADSIZE?fsize:HEADSIZE));
    MCALL1(fileClose(fh));
    // analyze sound file header
    // waw sample? starts with "RIFF" with fmt!=55h
    if (buf[0]=='R' && buf[1]=='I' && buf[2]=='F' && buf[3]=='F' /*&& buf[8]=='W' && buf[9]=='A' && buf[10]=='V' && buf[11]=='E'*/  && buf[20]!=0x55)
    {
        extern int lastWAVfreq;
        s->type=Sample;
        s->datahandle=(int)MIDASloadWaveSample(filename,loop?MIDAS_LOOP_YES:MIDAS_LOOP_NO);
        if(s->datahandle) s->playhandle=(int)MIDASplaySample((MIDASsample)s->datahandle,MIDAS_CHANNEL_AUTO,0/*priority*/,lastWAVfreq,64,MIDAS_PAN_MIDDLE);
    }
#ifdef EXTENDFILE
    // iff sample with 0x16 characters long name? (FT2 makes them)
    // it isn't ment as IFF support, just quick 99% working hack
    else if (buf[0]=='F' && buf[1]=='O' && buf[2]=='R' && buf[3]=='M' && buf[8]=='8' && buf[9]=='S' && buf[10]=='V' && buf[11]=='X' && buf[12]=='N' && buf[13]=='A' && buf[14]=='M' && buf[15]=='E' && buf[16]==0 && buf[17]==0 && buf[18]==0 && buf[19]==0x16)
    {
        int rate=(buf[62]<<8)+buf[63];
        rfSuperStart+=78;
        rfSuperSize=MIN(fsize-78,(buf[74]<<24)+(buf[75]<<16)+(buf[76]<<8)+buf[77]);
        rfSuperXor=128;
        s->type=Sample;
        s->datahandle=(int)MIDASloadRawSample(filename,MIDAS_SAMPLE_8BIT_MONO,loop?MIDAS_LOOP_YES:MIDAS_LOOP_NO);
        if(s->datahandle) s->playhandle=(int)MIDASplaySample((MIDASsample)s->datahandle,MIDAS_CHANNEL_AUTO,0/*priority*/,rate,64,MIDAS_PAN_MIDDLE);
    }
#endif
    // mp3 stream? starts with FF Fx or FF Ex or "ID3" or "RIFF" with fmt=55h
    else if ( (buf[0]==0xff && (buf[1] & 0xe0)==0xe0)
           || (buf[0]=='I' && buf[1]=='D' && buf[2]=='3')
           || (buf[0]=='R' && buf[1]=='I' && buf[2]=='F' && buf[3]=='F' && buf[20]==0x55) )
    {
        int rate,stereo;
        s->type=Stream;
        s->datahandle=(int)MP3open(filename,fileofs,filesize,&rate,&stereo,errAdd);
        if(s->datahandle) s->playhandle=(int)MIDASplayStreamPolling(stereo?MIDAS_SAMPLE_16BIT_STEREO:MIDAS_SAMPLE_16BIT_MONO,rate,1000*MP3PRECALCSEC);
        s->bufferbytes=rate*(stereo?4:2)*MP3PRECALCSEC;
    }
    // maybe module?
    else
    {
        s->type=Module;
        s->datahandle=(int)MIDASloadModule(filename);
        if(s->datahandle) s->playhandle=(int)MIDASplayModule((MIDASmodule)s->datahandle,loop);
    }
#ifdef EXTENDFILE
    rfSuperStart=0;
    rfSuperSize=0;
    rfSuperXor=0;
#endif
    MIDASreportErr();
}
Example #2
0
int main(int argc, char *argv[])
{
    char        oldTitle[256];
    unsigned    i;
/*    char        *foo;*/

    MIDASstartup();
    
    /* Keep the test somewhat reproducable: */
    srand(17);

    setbuf(stdout, NULL);

    puts("MIDAS stream test\n\n"
         "The sound output may sound strange, but the program should _NOT_ "
         "crash");

    puts("Trying to get window handle");

    /* Hack - try to figure out our console window's window handle: */
    GetConsoleTitle(oldTitle, 255);
    SetConsoleTitle("DirectSound TestiKala");
    Sleep(250);
    EnumWindows(&EnumWindowsProc, (LPARAM) NULL);
    SetConsoleTitle(oldTitle);

    printf("%P, %u\n", consoleHwnd, sizeof(HWND));

    if ( consoleHwnd == NULL )
        Error("Couldn't find window handle");

    puts("Trying to use DirectSound primary buffer mode");
    MIDASsetOption(MIDAS_OPTION_DSOUND_HWND, (DWORD) consoleHwnd);
    MIDASsetOption(MIDAS_OPTION_DSOUND_MODE, MIDAS_DSOUND_PRIMARY);

    /* Initialize MIDAS and start background playback: */
    if ( !MIDASinit() )
        MIDASerror();
/*    if ( !MIDASstartBackgroundPlay(0) )
        MIDASerror();*/

#ifdef _MSC_VER   
    if ( _beginthread(PollThread, 4096, NULL) == -1 )
        Error("Unable to create polling thread");
#else    
    if ( _beginthread(PollThread, NULL, 4096, NULL) == -1 )
        Error("Unable to create polling thread");
#endif    

    /* Open enough channels: */
    if ( !MIDASopenChannels(32) )
        MIDASerror();

    /* Allocate some channels for automatic effects: */
    if ( !MIDASallocAutoEffectChannels(12) )
        MIDASerror();

    /* Load the initial module and sample: */
    puts("Loading modules");
    if ( (module1 = MIDASloadModule(MODULE1)) == NULL )
        MIDASerror();
    if ( (module2 = MIDASloadModule(MODULE2)) == NULL )
        MIDASerror();
    puts("Loading samples");
    if ( (sample1 = MIDASloadRawSample(SAMPLE1, MIDAS_SAMPLE_8BIT_MONO, FALSE))
         == 0 )
        MIDASerror();
    if ( (sample2 = MIDASloadRawSample(SAMPLE2, MIDAS_SAMPLE_16BIT_MONO,
                                       FALSE)) == 0 )
         MIDASerror();

    /* Swap stuff out: */
    /*
    puts("Touching a LOT of memory");
    if ( (foo = malloc(64*1024*1024)) == NULL )
        Error("Out of memory");
    for ( i = 0; i < (64*1024*1024); i++ )
        foo[i] = (i & 255);
        */

    puts("Playing stream");
    if ( (stream1 = MIDASplayStreamWaveFile(STREAM1, 500, TRUE)) == NULL )
        MIDASerror();

    /* Play the module: */
    puts("Playing module");
    if ( (playHandle1 = MIDASplayModule(module1, TRUE)) == 0 )
        MIDASerror();

    /* Play effects for a while: */
    puts("Playing samples");
    for ( i = 0; i < 1000; i++ )
    {
        switch ( rand() & 8 )
        {
            case 0:
                if ( MIDASplaySample(sample1, MIDAS_CHANNEL_AUTO, 0, 22050 +
                                     ((rand() % 30000) - 15000), rand() % 24,
                                     (rand() % 128) - 64) == 0 )
                    MIDASerror();
                break;

            case 8:
                if ( MIDASplaySample(sample2, MIDAS_CHANNEL_AUTO, 0, 22050 +
                                     ((rand() % 30000) - 15000), rand() % 24,
                                     (rand() % 128) - 64) == 0 )
                    MIDASerror();
                break;
        }
                
        Sleep(rand() % 27);
        if ( (rand() % 8) < 2 )
            RandomPriority();

        printf("%u\n", MIDASgetStreamBytesBuffered(stream1));
    }

    puts("Stopping");

    stopPoll = 1;
    while ( stopPoll )
        Sleep(10);

    if ( !MIDASstopStream(stream1) )
        MIDASerror();
    /* Stop playback and uninitialize MIDAS: */
/*    if ( !MIDAScloseChannels() )
        MIDASerror();*/
/*    if ( !MIDASstopBackgroundPlay() )
        MIDASerror();*/
    if ( !MIDASclose() )
        MIDASerror();

    printf("Poll total %u, min %u, max %u, avg %u, count %u\n", pollTotal,
           pollMin, pollMax, pollTotal/(pollCount-100), pollCount);

    return 0;
}