Fix for slow starting of Blender.
authorTon Roosendaal <ton@blender.org>
Wed, 21 Apr 2004 13:38:54 +0000 (13:38 +0000)
committerTon Roosendaal <ton@blender.org>
Wed, 21 Apr 2004 13:38:54 +0000 (13:38 +0000)
This was caused by calling sound_init_audio() at startup. In situations
where Blender was first started, or when other applications used memory,
this could take 5-15 seconds.
I have moved the init call to 'start ketsji', and made sure any call
to an audio play routine will invoke an init as well.

Tested with engine and loading/play sound in F10 menu. I don't know how
the BlenderPlayer handles it... should be investigated.

Result: At OSX Blender starts in a second again! :)

source/blender/src/editsound.c
source/blender/src/space.c
source/blender/src/usiblender.c
source/creator/creator.c

index 2e7a2375ce4cd65807b406c699ebd470f482be9d..95699032e867f2294975a284bc4c2fb48a31e7f4 100644 (file)
@@ -83,8 +83,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);
@@ -181,16 +181,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;
+               }
        }
 }
 
@@ -202,6 +205,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)
        {
@@ -248,6 +253,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);
 }
@@ -569,6 +576,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)
        {
@@ -653,6 +662,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);
@@ -704,6 +715,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--;
@@ -822,6 +836,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)
        {
@@ -844,8 +860,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 
 }
 
@@ -854,8 +872,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
 }
 
@@ -864,6 +884,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);
 
@@ -970,55 +992,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) {
+               
+               printf("sound init audio\n");
        
-       if (noaudio)/*(noaudio) intrr: disable game engine audio (openal) */
-               SND_SetDeviceType(snd_e_dummydevice);
-
-       ghAudioDeviceInterface = SND_GetAudioDevice();
-       ghSoundScene = SND_CreateScene(ghAudioDeviceInterface);
-
-       sound_init_listener();
+               hSystem = SYS_GetSystem();
+               noaudio = SYS_GetCommandLineInt(hSystem,"noaudio",0);
+               
+               if (noaudio)/*(noaudio) intrr: disable game engine audio (openal) */
+                       SND_SetDeviceType(snd_e_dummydevice);
+       
+               ghAudioDeviceInterface = SND_GetAudioDevice();
+               ghSoundScene = SND_CreateScene(ghAudioDeviceInterface);
+       }
 }
 
 
-
 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();
+       }
 }
index a3f24557d3a11039343c6825c84b96b183a0ac95..af369e3d9370b10cab5b285daab28c89a4395148 100644 (file)
@@ -406,8 +406,10 @@ void start_game(void)
        
        BPY_end_python();
 
+       /* sound init is save, only handles once */
+       sound_init_audio();
        sound_stop_all_sounds();
-
+       
        /* Before jumping into Ketsji, we configure some settings. */
        space_set_commmandline_options();
 
index a46b1fe2b6ee9a364bb79a3f9463a3734d6c1a22..8a1d046ca3f888342c2080a1cc137cedfc8ab97d 100644 (file)
@@ -65,6 +65,7 @@
 #include "DNA_object_types.h"
 #include "DNA_space_types.h"
 #include "DNA_userdef_types.h"
+#include "DNA_sound_types.h"
 
 #include "BKE_blender.h"
 #include "BKE_curve.h"
@@ -477,13 +478,23 @@ static void initbuttons(void)
        clear_matcopybuf();
 }
 
+
+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 BIF_init(void)
 {
 
        initscreen();   /* for (visuele) speed, this first, then setscreen */
        initbuttons();
        InitCursorData();
-
+       sound_init_listener();
+       
        init_draw_rects();      /* drawobject.c */
        BIF_read_homefile();
        init_gl_stuff();        /* drawview.c, after homefile */
@@ -538,6 +549,7 @@ void exit_usiblender(void)
        free_txt_data();
 
        sound_exit_audio();
+       MEM_freeN(G.listener);
 
 #ifdef WITH_QUICKTIME
        quicktime_exit();
index 0bc922e2d04e4c80dee437c0da65d5c7eebb809e..48aa5afcb84f0c0052b08ea65f524b1f9024e3c1 100644 (file)
@@ -414,6 +414,7 @@ int main(int argc, char **argv)
                        }
                }
 
+printf("before BPY_start_python\n");
                BPY_start_python();
                
                /**
@@ -421,8 +422,9 @@ int main(int argc, char **argv)
                 * at least on FreeBSD.
                 */
 
-               sound_init_audio();
-
+//printf("before init_audio\n");
+//             sound_init_audio();
+printf("before BIF_init\n");
                BIF_init();
 
                /**
@@ -438,10 +440,12 @@ int main(int argc, char **argv)
        }
        else {
                BPY_start_python();
-               SYS_WriteCommandLineInt(syshandle,"noaudio",1);
-        audio = 0;
-        sound_init_audio();
-        if (G.f & G_DEBUG) printf("setting audio to: %d\n", audio);
+               
+               // (ton) Commented out. I have no idea whats thisfor... will mail around!
+               // SYS_WriteCommandLineInt(syshandle,"noaudio",1);
+        // audio = 0;
+        // sound_init_audio();
+        // if (G.f & G_DEBUG) printf("setting audio to: %d\n", audio);
        }
 
        RE_init_filt_mask();