Let the game engine manage it's own sound scene. This is to fix bug 1415 (Patch...
[blender.git] / source / blender / src / editsound.c
index 29942926d5f54de7d64f319a02a54c47635be64b..b409804a9f83487163b681f03a550355433b81ed 100644 (file)
@@ -31,6 +31,7 @@
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <math.h>
 #include <string.h>
 #include <fcntl.h>
 #endif
 
 #ifndef WIN32 
+#define __USE_XOPEN /* Needed for swab on linux */
 #include <unistd.h>
+#undef __USE_XOPEN
 #else
+
 #include <io.h>
 #endif
 #include "MEM_guardedalloc.h"
@@ -54,6 +58,7 @@
 #include "DNA_scene_types.h"
 #include "DNA_sound_types.h"
 #include "DNA_packedFile_types.h"
+#include "DNA_userdef_types.h"
 
 #include "BKE_utildefines.h"
 #include "BKE_global.h"
@@ -73,7 +78,6 @@
 
 #include "blendef.h"
 
-#include "interface.h"
 #include "mydevice.h"
 
 #include "SND_C-api.h"
@@ -84,8 +88,8 @@
 /* this might move to the external header */
 void* sound_get_libraryinterface(void);
 
-static SND_SceneHandle ghSoundScene;
-static SND_AudioDeviceInterfaceHandle ghAudioDeviceInterface;
+static SND_SceneHandle ghSoundScene=NULL;
+static SND_AudioDeviceInterfaceHandle ghAudioDeviceInterface=NULL;
 
 /* que? why only here? because of the type define? */
 bSound *sound_find_sound(char *id_name);
@@ -99,11 +103,13 @@ void winqreadsoundspace(struct ScrArea *sa, void *spacedata, struct BWinEvent *e
 /* Right. Now for some implementation: */
 void winqreadsoundspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
 {
+       SpaceSound *ssound= spacedata;
        unsigned short event= evt->event;
        short val= evt->val;
        float dx, dy;
        int doredraw= 0, cfra, first = 0;
-       short mval[2];
+       short mval[2], nr;
+       short mousebut = L_MOUSE;
        
        if(curarea->win==0) return;
 
@@ -111,30 +117,40 @@ void winqreadsoundspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
                
                if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
 
-               switch(event)
-               {
+               /* swap mouse buttons based on user preference */
+               if (U.flag & USER_LMOUSESELECT) {
+                       if (evt->event == LEFTMOUSE) {
+                               event = RIGHTMOUSE;
+                               mousebut = L_MOUSE;
+                       } else if (evt->event == RIGHTMOUSE) {
+                               event = LEFTMOUSE;
+                               mousebut = R_MOUSE;
+                       }
+               }
+
+               switch(event) {
                case LEFTMOUSE:
-                       if( view2dmove(event)==0 )
-                       {
-                               do
-                               {
-                                       getmouseco_areawin(mval);
-                                       areamouseco_to_ipoco(G.v2d, mval, &dx, &dy);
-                                       
-                                       cfra = (int)dx;
-                                       if(cfra< 1) cfra= 1;
-                                       
-                                       if( cfra!=CFRA || first )
-                                       {
-                                               first= 0;
-                                               CFRA= cfra;
-                                               update_for_newframe();
-                                               force_draw_plus(SPACE_VIEW3D);
-                                       }
+                       ssound->flag |= SND_CFRA_NUM;
+                       do {
+                               getmouseco_areawin(mval);
+                               areamouseco_to_ipoco(G.v2d, mval, &dx, &dy);
                                
-                               } while(get_mbut()&L_MOUSE);
+                               cfra = (int)dx;
+                               if(cfra< 1) cfra= 1;
                                
-                       }
+                               if( cfra!=CFRA || first )
+                               {
+                                       first= 0;
+                                       CFRA= cfra;
+                                       update_for_newframe();
+                                       force_draw_plus(SPACE_VIEW3D);
+                               }
+                       
+                       } while(get_mbut() & mousebut);
+                       ssound->flag &= ~SND_CFRA_NUM;
+                       
+                       doredraw= 1;
+
                        break;
                case MIDDLEMOUSE:
                case WHEELUPMOUSE:
@@ -142,7 +158,13 @@ void winqreadsoundspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
                        view2dmove(event);      /* in drawipo.c */
                        break;
                case RIGHTMOUSE:
-                       /* mouse_select_seq(); */
+                       nr= pupmenu("Time value%t|Frames %x1|Seconds%x2");
+                       if (nr>0) {
+                               if(nr==1) ssound->flag |= SND_DRAWFRAMES;
+                               else ssound->flag &= ~SND_DRAWFRAMES;
+                               doredraw= 1;
+                       }
+
                        break;
                case PADPLUSKEY:
                        dx= (float)(0.1154*(G.v2d->cur.xmax-G.v2d->cur.xmin));
@@ -176,16 +198,19 @@ void sound_initialize_sounds(void)
 {
        bSound* sound;
 
-       /* clear the soundscene */
-       SND_RemoveAllSounds(ghSoundScene);
-       SND_RemoveAllSamples(ghSoundScene);
+       if(ghSoundScene) {
 
-       /* initialize sounds */
-       sound = G.main->sound.first;
-       while (sound)
-       {
-               sound_sample_is_null(sound);
-               sound = (bSound *) sound->id.next;
+               /* clear the soundscene */
+               SND_RemoveAllSounds(ghSoundScene);
+               SND_RemoveAllSamples(ghSoundScene);
+       
+               /* initialize sounds */
+               sound = G.main->sound.first;
+               while (sound)
+               {
+                       sound_sample_is_null(sound);
+                       sound = (bSound *) sound->id.next;
+               }
        }
 }
 
@@ -197,6 +222,8 @@ bSound* sound_make_copy(bSound* originalsound)
        char name[160];
        int len;
        
+       if(ghSoundScene==NULL) sound_init_audio();
+       
        /* only copy sounds that are sounds */
        if (originalsound)
        {
@@ -243,6 +270,8 @@ bSound* sound_make_copy(bSound* originalsound)
 
 void sound_initialize_sample(bSound* sound)
 {
+       if(ghSoundScene==NULL) sound_init_audio();
+
        if (sound && sound->sample == NULL)
                sound_sample_is_null(sound);
 }
@@ -485,7 +514,7 @@ int sound_get_filetype_from_header(bSound* sound, PackedFile* pf)
        {
                filetype = SAMPLE_OGG_VORBIS;
        }
-       else if ((!memcmp(buffer, "ID3", 3)) || (!memcmp(buffer, "ÿû", 2)))
+       else if ((!memcmp(buffer, "ID3", 3)) || (!memcmp(buffer, "", 2)))
        {
                filetype = SAMPLE_MP3;
        }
@@ -564,6 +593,8 @@ int sound_load_sample(bSound* sound)
        int freePF = FALSE;
        int buffer = -1;
 
+       if(ghSoundScene==NULL) sound_init_audio();
+
        /* check the sample (valid?) */
        if (sound->sample->type == SAMPLE_UNKNOWN || sound->snd_sound == NULL)
        {
@@ -648,6 +679,8 @@ bSound* sound_new_sound(char* name)
        int len, file;
        char str[FILE_MAXDIR+FILE_MAXFILE];
 
+       if(ghSoundScene==NULL) sound_init_audio();
+
        if (!G.scene->audio.mixrate) G.scene->audio.mixrate = 44100;
        /* convert the name to absolute path */
        strcpy(str, name);
@@ -699,6 +732,9 @@ bSound* sound_new_sound(char* name)
 int sound_set_sample(bSound *sound, bSample *sample)
 {
        int result = TRUE;
+       
+       if(ghSoundScene==NULL) sound_init_audio();
+       
        /* decrease the usernumber for this sample */
        if (sound->sample)
                sound->sample->id.us--;
@@ -817,6 +853,8 @@ int sound_sample_is_null(bSound* sound)
        int result = FALSE;
        bSample* sample;
        
+       if(ghSoundScene==NULL) sound_init_audio();
+       
        /* find the right sample or else create one */
        if (sound->sample == NULL)
        {
@@ -839,8 +877,10 @@ int sound_sample_is_null(bSound* sound)
 void sound_stop_all_sounds(void)
 {
 #if GAMEBLENDER == 1
-       SND_StopAllSounds(ghSoundScene);
-       SND_Proceed(ghAudioDeviceInterface, ghSoundScene);
+       if(ghSoundScene) {
+               SND_StopAllSounds(ghSoundScene);
+               SND_Proceed(ghAudioDeviceInterface, ghSoundScene);
+       }
 #endif 
 }
 
@@ -849,8 +889,10 @@ void sound_stop_all_sounds(void)
 void sound_end_all_sounds(void)
 {
 #if GAMEBLENDER == 1
-       sound_stop_all_sounds();
-       SND_RemoveAllSounds(ghSoundScene);
+       if(ghSoundScene) {
+               sound_stop_all_sounds();
+               SND_RemoveAllSounds(ghSoundScene);
+       }
 #endif
 }
 
@@ -859,6 +901,8 @@ void sound_end_all_sounds(void)
 void sound_play_sound(bSound* sound)
 {
 #if GAMEBLENDER == 1
+       if(ghSoundScene==NULL) sound_init_audio();
+       
        /* first check if we want sound or not */
        SND_IsPlaybackWanted(ghSoundScene);
 
@@ -965,55 +1009,37 @@ bSound *sound_find_sound(char *id_name)
        return sound;
 }
 
-
-
-static void sound_init_listener(void)
-{
-       G.listener = MEM_callocN(sizeof(bSoundListener), "soundlistener");
-       G.listener->gain = 1.0;
-       G.listener->dopplerfactor = 1.0;
-       G.listener->dopplervelocity = 1.0;
-}
-
-
-
 void sound_init_audio(void)
 {
        int noaudio;
        SYS_SystemHandle hSystem = NULL;
-       ghAudioDeviceInterface = NULL;
        
-       hSystem = SYS_GetSystem();
-       noaudio = SYS_GetCommandLineInt(hSystem,"noaudio",0);
+       if(ghSoundScene==NULL) {
+               hSystem = SYS_GetSystem();
+               noaudio = SYS_GetCommandLineInt(hSystem,"noaudio",0);
+               
+               if (noaudio)/*(noaudio) intrr: disable game engine audio (openal) */
+                       SND_SetDeviceType(snd_e_dummydevice);
        
-       if (1)/*(noaudio) intrr: disable game engine audio (openal) */
-               SND_SetDeviceType(snd_e_dummydevice);
-
-       ghAudioDeviceInterface = SND_GetAudioDevice();
-       ghSoundScene = SND_CreateScene(ghAudioDeviceInterface);
-
-       sound_init_listener();
+               ghAudioDeviceInterface = SND_GetAudioDevice();
+               ghSoundScene = SND_CreateScene(ghAudioDeviceInterface);
+               // also called after read new file, but doesnt work when no audio initialized
+               sound_initialize_sounds();
+       }
 }
 
 
-
 int sound_get_mixrate(void)
 {
        return MIXRATE;
 }
 
 
-
-static void sound_exit_listener(void)
-{
-       MEM_freeN(G.listener);
-}
-
-
-
 void sound_exit_audio(void)
 {
-       SND_DeleteScene(ghSoundScene);
-       SND_ReleaseDevice();
-       sound_exit_listener();
+       if(ghSoundScene) {
+               SND_DeleteScene(ghSoundScene);
+               SND_ReleaseDevice();
+               ghSoundScene = NULL;
+       }
 }