Annotation of previous/src/audio.c, revision 1.1

1.1     ! root        1: #include "main.h"
        !             2: #include "dialog.h"
        !             3: #include "configuration.h"
        !             4: #include "m68000.h"
        !             5: #include "sysdeps.h"
        !             6: #include "audio.h"
        !             7: #include "dma.h"
        !             8: #include "snd.h"
        !             9: 
        !            10: #include <SDL.h>
        !            11: 
        !            12: 
        !            13: /* Sound emulation SDL interface */
        !            14: SDL_AudioDeviceID Audio_Input_Device;
        !            15: SDL_AudioDeviceID Audio_Output_Device;
        !            16: 
        !            17: int nAudioFrequency = 44100;            /* Sound playback frequency */
        !            18: bool bSoundOutputWorking = false;       /* Is sound output OK */
        !            19: bool bSoundInputWorking = false;        /* Is sound input OK */
        !            20: volatile bool bPlayingBuffer = false;   /* Is playing buffer? */
        !            21: volatile bool bRecordingBuffer = false; /* Is recording buffer? */
        !            22: 
        !            23: 
        !            24: /*-----------------------------------------------------------------------*/
        !            25: /**
        !            26:  * SDL audio callback functions - move sound between emulation and audio system.
        !            27:  * Note: These functions will run in a separate thread.
        !            28:  */
        !            29: static void Audio_Output_CallBack(void *userdata, Uint8 *stream, int len)
        !            30: {
        !            31:     //printf("AUDIO CALLBACK, size = %i\n",len);
        !            32:     sndout_queue_poll(stream, len);
        !            33: }
        !            34: 
        !            35: static void Audio_Input_CallBack(void *userdata, Uint8 *stream, int len)
        !            36: {
        !            37:     /* FIXME: send data to emulator */
        !            38: }
        !            39: 
        !            40: 
        !            41: /*-----------------------------------------------------------------------*/
        !            42: /**
        !            43:  * Initialize the audio subsystem. Return true if all OK.
        !            44:  */
        !            45: void Audio_Output_Init(void)
        !            46: {
        !            47:     SDL_AudioSpec request;    /* We fill in the desired SDL audio options here */
        !            48:     SDL_AudioSpec granted;
        !            49:     
        !            50:     /* Init the SDL's audio subsystem: */
        !            51:     if (SDL_WasInit(SDL_INIT_AUDIO) == 0)
        !            52:     {
        !            53:         if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0)
        !            54:         {
        !            55:             Log_Printf(LOG_WARN, "Could not init audio output: %s\n", SDL_GetError());
        !            56:             DlgAlert_Notice("Error: Can't open SDL audio subsystem.");
        !            57:             bSoundOutputWorking = false;
        !            58:             return;
        !            59:         }
        !            60:     }
        !            61:     
        !            62:     /* Set up SDL audio: */
        !            63:     request.freq = 44100;           /* 44,1 kHz */
        !            64:     request.format = AUDIO_S16MSB;     /* 16-Bit signed, big endian */
        !            65:     request.channels = 2;                      /* stereo */
        !            66:     request.callback = Audio_Output_CallBack;
        !            67:     request.userdata = NULL;
        !            68:     request.samples = 1024;    /* buffer size in samples (4096 byte) */
        !            69: 
        !            70:     Audio_Output_Device = SDL_OpenAudioDevice(NULL, 0, &request, &granted, 0);
        !            71:     if (Audio_Output_Device==0)        /* Open audio device */
        !            72:     {
        !            73:         Log_Printf(LOG_WARN, "Can't use audio: %s\n", SDL_GetError());
        !            74:         DlgAlert_Notice("Error: Can't open audio output device. No sound output.");
        !            75:         bSoundOutputWorking = false;
        !            76:         return;
        !            77:     }
        !            78: 
        !            79:     /* All OK */
        !            80:     bSoundOutputWorking = true;
        !            81: }
        !            82: 
        !            83: void Audio_Input_Init(void)
        !            84: {
        !            85:     SDL_AudioSpec request;    /* We fill in the desired SDL audio options here */
        !            86:     SDL_AudioSpec granted;
        !            87:     
        !            88:     /* Init the SDL's audio subsystem: */
        !            89:     if (SDL_WasInit(SDL_INIT_AUDIO) == 0)
        !            90:     {
        !            91:         if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0)
        !            92:         {
        !            93:             Log_Printf(LOG_WARN, "Could not init audio input: %s\n", SDL_GetError());
        !            94:             DlgAlert_Notice("Error: Can't open SDL audio subsystem.");
        !            95:             bSoundInputWorking = false;
        !            96:             return;
        !            97:         }
        !            98:     }
        !            99:     
        !           100:     /* Set up SDL audio: */
        !           101:     request.freq = 8000;
        !           102:     request.format = AUDIO_S16MSB;     /* 16-Bit signed, big endian */
        !           103:     request.channels = 1;                      /* mono */
        !           104:     request.callback = Audio_Input_CallBack;
        !           105:     request.userdata = NULL;
        !           106:     request.samples = 1024;    /* buffer size in samples (2048 byte) */
        !           107:     
        !           108:     Audio_Input_Device = SDL_OpenAudioDevice(NULL, 0, &request, &granted, 0);
        !           109:     if (Audio_Input_Device==0) /* Open audio device */
        !           110:     {
        !           111:         Log_Printf(LOG_WARN, "Can't use audio: %s\n", SDL_GetError());
        !           112:         DlgAlert_Notice("Error: Can't open audio input device. No sound recording.");
        !           113:         bSoundInputWorking = false;
        !           114:         return;
        !           115:     }
        !           116:     
        !           117:     /* All OK */
        !           118:     bSoundInputWorking = true;
        !           119: }
        !           120: 
        !           121: 
        !           122: /*-----------------------------------------------------------------------*/
        !           123: /**
        !           124:  * Free audio subsystem
        !           125:  */
        !           126: void Audio_Output_UnInit(void)
        !           127: {
        !           128:     if (bSoundOutputWorking)
        !           129:     {
        !           130:         /* Stop */
        !           131:         Audio_Output_Enable(false);
        !           132:         
        !           133:         SDL_CloseAudioDevice(Audio_Output_Device);
        !           134:         
        !           135:         bSoundOutputWorking = false;
        !           136:     }
        !           137: }
        !           138: 
        !           139: void Audio_Input_UnInit(void)
        !           140: {
        !           141:     if (bSoundInputWorking)
        !           142:     {
        !           143:         /* Stop */
        !           144:         Audio_Input_Enable(false);
        !           145:         
        !           146:         SDL_CloseAudioDevice(Audio_Input_Device);
        !           147:         
        !           148:         bSoundInputWorking = false;
        !           149:     }
        !           150: }
        !           151: 
        !           152: 
        !           153: /*-----------------------------------------------------------------------*/
        !           154: /**
        !           155:  * Lock the audio sub system so that the callback function will not be called.
        !           156:  */
        !           157: void Audio_Output_Lock(void)
        !           158: {
        !           159:     SDL_LockAudioDevice(Audio_Output_Device);
        !           160: }
        !           161: 
        !           162: void Audio_Input_Lock(void)
        !           163: {
        !           164:     SDL_LockAudioDevice(Audio_Input_Device);
        !           165: }
        !           166: 
        !           167: 
        !           168: /*-----------------------------------------------------------------------*/
        !           169: /**
        !           170:  * Unlock the audio sub system so that the callback function will be called again.
        !           171:  */
        !           172: void Audio_Output_Unlock(void)
        !           173: {
        !           174:     SDL_UnlockAudioDevice(Audio_Output_Device);
        !           175: }
        !           176: 
        !           177: void Audio_Input_Unlock(void)
        !           178: {
        !           179:     SDL_UnlockAudioDevice(Audio_Input_Device);
        !           180: }
        !           181: 
        !           182: 
        !           183: /*-----------------------------------------------------------------------*/
        !           184: /**
        !           185:  * Set audio playback frequency variable, pass as PLAYBACK_xxxx
        !           186:  */
        !           187: void Audio_Output_SetFreq(int nNewFrequency)
        !           188: {
        !           189:     /* Do not reset sound system if nothing has changed! */
        !           190:     if (nNewFrequency != nAudioFrequency)
        !           191:     {
        !           192:         Log_Printf(LOG_WARN, "[SDL Audio] Changing frequency from %i Hz to %i Hz",
        !           193:                    nAudioFrequency,nNewFrequency);
        !           194:         /* Set new frequency */
        !           195:         nAudioFrequency = nNewFrequency;
        !           196:         
        !           197:         /* Re-open SDL audio interface if necessary: */
        !           198:         if (bSoundOutputWorking)
        !           199:         {
        !           200:             Audio_Output_UnInit();
        !           201:             Audio_Output_Init();
        !           202:         }
        !           203:     }
        !           204: }
        !           205: 
        !           206: 
        !           207: /*-----------------------------------------------------------------------*/
        !           208: /**
        !           209:  * Start/Stop sound buffer
        !           210:  */
        !           211: void Audio_Output_Enable(bool bEnable)
        !           212: {
        !           213:     if (bEnable && !bPlayingBuffer)
        !           214:     {
        !           215:         /* Start playing */
        !           216:         SDL_PauseAudioDevice(Audio_Output_Device, false);
        !           217:         bPlayingBuffer = true;
        !           218:     }
        !           219:     else if (!bEnable && bPlayingBuffer)
        !           220:     {
        !           221:         /* Stop from playing */
        !           222:         SDL_PauseAudioDevice(Audio_Output_Device, true);
        !           223:         bPlayingBuffer = false;
        !           224:     }
        !           225: }
        !           226: 
        !           227: void Audio_Input_Enable(bool bEnable)
        !           228: {
        !           229:     if (bEnable && !bRecordingBuffer)
        !           230:     {
        !           231:         /* Start playing */
        !           232:         SDL_PauseAudioDevice(Audio_Input_Device, false);
        !           233:         bRecordingBuffer = true;
        !           234:     }
        !           235:     else if (!bEnable && bPlayingBuffer)
        !           236:     {
        !           237:         /* Stop from playing */
        !           238:         SDL_PauseAudioDevice(Audio_Input_Device, true);
        !           239:         bRecordingBuffer = false;
        !           240:     }
        !           241: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.