svn merge -r 15392:15551 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / src / editsound.c
index e3d3e09bf40b18638254db699d70edd486cf9e7f..05eb094a7c21a3b13b8533eb62c27ed623afb719 100644 (file)
@@ -1,15 +1,12 @@
 /**
  * $Id$
  *
- * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
+ * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version. The Blender
- * Foundation also sells licenses for use in proprietary software under
- * the Blender License.  See http://www.blender.org/BL/ for information
- * about this.
+ * of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  *
  * Contributor(s): none yet.
  *
- * ***** END GPL/BL DUAL LICENSE BLOCK *****
+ * ***** END GPL LICENSE BLOCK *****
  */
 
 #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"
 
 #include "DNA_object_types.h"
 #include "DNA_screen_types.h"
+#include "DNA_space_types.h"
 #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"
 #include "BIF_interface.h"
 #include "BIF_editsound.h"
 #include "BIF_mywindow.h"
+#include "BIF_toolbox.h"
 
 #include "BSE_drawipo.h"
 #include "BSE_headerbuttons.h"
+#include "BSE_time.h"
+
+#include "BDR_editobject.h"
 
 #include "blendef.h"
 
-#include "interface.h"
 #include "mydevice.h"
 
 #include "SND_C-api.h"
 
 #include "SYS_System.h"
 
+#include "PIL_time.h"
+
+
 /* 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);
 void sound_read_wav_data(bSound * sound, PackedFile * pf);
 void sound_stop_sound(void *object, bSound *sound);
-void winqreadsoundspace(unsigned short event, short val, char ascii);
+void winqreadsoundspace(struct ScrArea *sa, void *spacedata, struct BWinEvent *evt);
 /*  void sound_stop_all_sounds(void); already in BIF_editsound.h */
 
 
 
 /* Right. Now for some implementation: */
-void winqreadsoundspace(unsigned short event, short val, char ascii)
+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;
 
        if(val) {
                
-               if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
+               if( uiDoBlocks(&curarea->uiblocks, event, 1)!=UI_NOTHING ) event= 0;
+
+               /* swap mouse buttons based on user preference */
+               if (U.flag & USER_LMOUSESELECT) {
+                       if (event == LEFTMOUSE) {
+                               event = RIGHTMOUSE;
+                               mousebut = L_MOUSE;
+                       } else if (event == RIGHTMOUSE) {
+                               event = LEFTMOUSE;
+                               mousebut = R_MOUSE;
+                       }
+               }
 
-               switch(event)
-               {
+               switch(event) {
                case LEFTMOUSE:
-                       if( view2dmove()==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+0.5f);
+                               if(cfra< 1) cfra= 1;
                                
-                       }
+                               if( cfra!=CFRA || first )
+                               {
+                                       first= 0;
+                                       CFRA= cfra;
+                                       update_for_newframe();
+                                       force_draw_all(0);
+                               }
+                               else PIL_sleep_ms(30);
+                       
+                       } while(get_mbut() & mousebut);
+                       ssound->flag &= ~SND_CFRA_NUM;
+                       
+                       doredraw= 1;
+
                        break;
                case MIDDLEMOUSE:
                case WHEELUPMOUSE:
                case WHEELDOWNMOUSE:
-                       view2dmove();   /* in drawipo.c */
+                       view2dmove(event);      /* in drawipo.c */
                        break;
                case RIGHTMOUSE:
-                       /* mouse_select_seq(); */
+                       {
+                               TimeMarker *marker;
+                               
+                               getmouseco_areawin(mval);
+                               areamouseco_to_ipoco(G.v2d, mval, &dx, &dy);
+
+                               marker = find_nearest_marker(SCE_MARKERS, 0);
+                               if (marker) {
+                                       if ((G.qual & LR_SHIFTKEY)==0)
+                                               deselect_markers(0, 0);
+                                               
+                                       if (marker->flag & SELECT)
+                                               marker->flag &= ~SELECT;
+                                       else
+                                               marker->flag |= SELECT;
+                               }
+                               
+                               force_draw(0);
+                               std_rmouse_transform(transform_markers);
+                       }
                        break;
+                       
                case PADPLUSKEY:
                        dx= (float)(0.1154*(G.v2d->cur.xmax-G.v2d->cur.xmin));
                        G.v2d->cur.xmin+= dx;
                        G.v2d->cur.xmax-= dx;
                        test_view2d(G.v2d, curarea->winx, curarea->winy);
-
+                       view2d_do_locks(curarea, V2D_LOCK_COPY);
                        doredraw= 1;
                        break;
                case PADMINUS:
@@ -155,12 +199,68 @@ void winqreadsoundspace(unsigned short event, short val, char ascii)
                        G.v2d->cur.xmin-= dx;
                        G.v2d->cur.xmax+= dx;
                        test_view2d(G.v2d, curarea->winx, curarea->winy);
-
+                       view2d_do_locks(curarea, V2D_LOCK_COPY);
                        doredraw= 1;
                        break;
                case HOMEKEY:
                        do_sound_buttons(B_SOUNDHOME);
                        break;
+                       
+               case PAGEUPKEY: /* cfra to next marker */
+                       nextprev_marker(1);
+                       break;
+               case PAGEDOWNKEY: /* cfra to prev marker */
+                       nextprev_marker(-1);
+                       break;
+               
+               case AKEY: /* select/deselect all  */
+                       deselect_markers(1, 0);
+                       
+                       allqueue(REDRAWMARKER, 0);
+                       break;
+                       
+               case BKEY: /* borderselect markers */
+                       borderselect_markers();
+                       break;
+               
+               case DKEY: /* duplicate selected marker(s) */
+                       if (G.qual & LR_SHIFTKEY) {
+                               duplicate_marker();
+                               
+                               allqueue(REDRAWMARKER, 0);
+                       }
+                       break;
+                       
+               case GKEY:
+                       transform_markers('g', 0);
+                       break;
+                       
+               case MKEY: /* add marker or rename first selected */
+                       if (G.qual & LR_CTRLKEY)
+                               rename_marker();
+                       else
+                               add_marker(CFRA);
+                       
+                       allqueue(REDRAWMARKER, 0);
+                       break;          
+               
+               case TKEY: /* toggle time display */
+                       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 DELKEY: /* delete selected markers */
+               case XKEY:
+                       if (okee("Erase selected")) {
+                               remove_marker();
+                               allqueue(REDRAWMARKER, 0);
+                       }
+                       break;
                }
        }
 
@@ -172,32 +272,33 @@ void winqreadsoundspace(unsigned short event, short val, char ascii)
 
 void sound_initialize_sounds(void)
 {
-#if GAMEBLENDER == 1
-       bSound* sound;
+       bSound *sound;
 
-       /* clear the soundscene */
-       SND_RemoveAllSounds(ghSoundScene);
-       SND_RemoveAllSamples(ghSoundScene);
+       if(ghSoundScene) {
 
-       /* initialize sounds */
+               /* clear the soundscene */
+               SND_RemoveAllSounds(ghSoundScene);
+               SND_RemoveAllSamples(ghSoundScene);
+       }
+       
+       /* initialize sample blocks (doesnt call audio system, needs to be done once after load */
        sound = G.main->sound.first;
-       while (sound)
-       {
+       while (sound) {
                sound_sample_is_null(sound);
                sound = (bSound *) sound->id.next;
        }
-#endif
 }
 
 
 
-bSound* sound_make_copy(bSound* originalsound)
+bSound *sound_make_copy(bSound *originalsound)
 {
-       bSound* sound = NULL;
-#if GAMEBLENDER == 1
+       bSound *sound = NULL;
        char name[160];
        int len;
        
+       if(ghSoundScene==NULL) sound_init_audio();
+       
        /* only copy sounds that are sounds */
        if (originalsound)
        {
@@ -237,21 +338,21 @@ bSound* sound_make_copy(bSound* originalsound)
                        sound->flags &= ~SOUND_FLAGS_3D;
        }
        
-#endif
        return sound;
 }
 
 
 
-void sound_initialize_sample(bSoundsound)
+void sound_initialize_sample(bSound *sound)
 {
+       if(ghSoundScene==NULL) sound_init_audio();
+
        if (sound && sound->sample == NULL)
                sound_sample_is_null(sound);
 }
 
 
-
-void sound_read_wav_data(bSound* sound, PackedFile* pf)
+void sound_read_wav_data(bSound *sound, PackedFile *pf)
 {
        int i, temp;
        short shortbuf, *temps;
@@ -264,49 +365,35 @@ void sound_read_wav_data(bSound* sound, PackedFile* pf)
        
        /* prepare for the worst... */
        sound->sample->type = SAMPLE_INVALID;
-       
+
        rewindPackedFile(pf);
-       
+
        /* check to see if it is a file in "RIFF WAVE fmt" format */
-       if (readPackedFile(pf, buffer, 16) != 16)
-       {
+       if (readPackedFile(pf, buffer, 16) != 16) {
                if (G.f & G_DEBUG) printf("File too short\n");
                return;
        }
-       
-       if(!(memcmp(buffer, "RIFF", 4) && memcmp(&(buffer[8]), "WAVEfmt ", 8)))
-       {
+
+       if(!(memcmp(buffer, "RIFF", 4) && memcmp(&(buffer[8]), "WAVEfmt ", 8))) {
                readPackedFile(pf, &i, 4);// start of data
                if(G.order==B_ENDIAN)
                        SWITCH_INT(i);
-               
+
                /* read the sampleformat */
                readPackedFile(pf, &shortbuf, 2);
-               if(G.order==B_ENDIAN)
-               {
-                       /* was SWITCH_SHORT before */
-                       char s_i, *p_i;
-                       p_i= (char *)&(shortbuf);
-                       s_i= p_i[0];
-                       p_i[0]= p_i[1];
-                       p_i[1]= s_i;
+               if(G.order==B_ENDIAN) {
+                       SWITCH_SHORT(shortbuf);
                }
-               
+
                /* read the number of channels */
                readPackedFile(pf, &shortbuf, 2);
-               if(G.order==B_ENDIAN)
-               {
-                       /* was SWITCH_SHORT before */
-                       char s_i, *p_i;
-                       p_i= (char *)&(shortbuf);
-                       s_i= p_i[0];
-                       p_i[0]= p_i[1];
-                       p_i[1]= s_i;
+
+               if(G.order==B_ENDIAN) {
+                       SWITCH_SHORT(shortbuf);
                }
-               
+
                /* check the number of channels */
-               if(shortbuf != 1 && shortbuf != 2)
-               {
+               if(shortbuf != 1 && shortbuf != 2) {
                        if (G.f & G_DEBUG) printf("Unsupported number of channels\n");
                        return;
                }
@@ -314,6 +401,8 @@ void sound_read_wav_data(bSound* sound, PackedFile* pf)
                
                /* read the samplerate */
                readPackedFile(pf, &longbuf, 4);
+
+
                if(G.order==B_ENDIAN)
                        SWITCH_INT(longbuf);
                rate = longbuf;
@@ -321,6 +410,7 @@ void sound_read_wav_data(bSound* sound, PackedFile* pf)
                /* read the bitrate */
                // Ton's way
                readPackedFile(pf, &temp, 4);
+
                if(G.order==B_ENDIAN)
                        SWITCH_INT(temp);
 
@@ -328,25 +418,18 @@ void sound_read_wav_data(bSound* sound, PackedFile* pf)
                        bits= 8*temp/(rate * channels);
                
                // Frank's way
+
                readPackedFile(pf, &shortbuf, 2);
                readPackedFile(pf, &shortbuf, 2);
-               if(G.order==B_ENDIAN)
-               {
-                       /* was SWITCH_SHORT before */
-                       char s_i, *p_i;
-                       p_i= (char *)&(shortbuf);
-                       s_i= p_i[0];
-                       p_i[0]= p_i[1];
-                       p_i[1]= s_i;
+               if(G.order==B_ENDIAN) {
+                       SWITCH_SHORT(shortbuf);
                }
                bits = shortbuf;
                
                seekPackedFile(pf, i-16, SEEK_CUR);
                readPackedFile(pf, buffer, 4);
-               
                /* check if we have a 'data' chunk */
-               while(memcmp(buffer, "data", 4)!=0)
-               {
+               while(memcmp(buffer, "data", 4)!=0) {
                        if (readPackedFile(pf, &i, 4) != 4)
                                break;
                        
@@ -354,59 +437,43 @@ void sound_read_wav_data(bSound* sound, PackedFile* pf)
                                SWITCH_INT(i);
 
                        seekPackedFile(pf, i, SEEK_CUR);
-                       
                        if (readPackedFile(pf, buffer, 4) != 4)
                                break;
                }
                
                /* guess not */
-               if (memcmp(buffer, "data", 4) !=0)
-               {
+               if (memcmp(buffer, "data", 4) !=0) {
                        if (G.f & G_DEBUG) printf("No data found\n");
                }
                /* or maybe we do! */
-               else
-               {
+               else {
                        readPackedFile(pf, &longbuf, 4); 
                        if(G.order==B_ENDIAN) SWITCH_INT(longbuf);
                        
                        /* handle 8 and 16 bit samples differently */
-                       if (bits == 8)
-                               data = (char *)MEM_mallocN(2 * longbuf, "sample data");
-                       else if (bits == 16)
+                       /* intrr: removed, longbuf is length in bytes, not samples */
+                       if (bits == 16)
                                data = (char *)MEM_mallocN(longbuf, "sample data");
+                       else 
+                               data = (char *)MEM_mallocN(longbuf*2, "sample data");
 
-                       len = longbuf;
+                       len = longbuf /*/ 4.0*/; /* for some strange reason the sample length is off by a factor of 4... */
+                       /* intrr's comment: Funny eh, how one 16-bit stereo sample is 4 bytes? :-) */
                        
-                       if(data)
-                       {
+                       if(data) {
                                readPackedFile(pf, data, len);
-                               
                                /* data is only used to draw! */
-                               if (bits == 8)
-                               {
+                               if (bits == 8) {
                                        temps = (short *) data;
                                        tempc = (char *) data;
                                        for (i = len - 1; i >= 0; i--)
                                                temps[i] = tempc[i] << 8;
                                }
-                               else
-                               {
-                                       if(G.order==B_ENDIAN)
-                                       {
-                                               temps= (short *)data;
-                                               for(i=0; i< len / 2; i++, temps++)
-                                               {
-                                                       /* was SWITCH_SHORT before */
-                                                       char s_i, *p_i;
-                                                       p_i= (char *)&(temps);
-                                                       s_i= p_i[0];
-                                                       p_i[0]= p_i[1];
-                                                       p_i[1]= s_i;
-                                               }
+                               else {
+                                       if(G.order==B_ENDIAN) {
+                                               swab(data, data, len);
                                        }
                                }
-                               
                                /* fill the sound with the found data */
                                sample = sound->sample;
                                sample->channels = channels;
@@ -418,8 +485,7 @@ void sound_read_wav_data(bSound* sound, PackedFile* pf)
                        }
                }
        }
-       else
-       {
+       else {
                sound->sample->type = SAMPLE_INVALID;
                if (G.f & G_DEBUG) printf("Unsupported sound format: %s\n", sound->name);
        }
@@ -428,31 +494,28 @@ void sound_read_wav_data(bSound* sound, PackedFile* pf)
 
 
 /* ugly, but it works (for now) */
-int sound_get_filetype_from_header(bSound* sound, PackedFile* pf)
+static int sound_get_filetype_from_header(bSound *sound, PackedFile *pf)
 {
-       int i, filetype = SAMPLE_INVALID;
-#if GAMEBLENDER == 1
+       int filetype = SAMPLE_INVALID;
+       int i;
        char buffer[25];
        short shortbuf;
        
        rewindPackedFile(pf);
        
-       if (readPackedFile(pf, buffer, 16) != 16)
-       {
+       if (readPackedFile(pf, buffer, 16) != 16) {
                if (G.f & G_DEBUG) printf("File too short\n");
                return filetype;
        }
        
-       if(!(memcmp(buffer, "RIFF", 4) && memcmp(&(buffer[8]), "WAVEfmt ", 8)))
-       {
+       if(!(memcmp(buffer, "RIFF", 4) && memcmp(&(buffer[8]), "WAVEfmt ", 8))) {
                readPackedFile(pf, &i, 4);
                if(G.order==B_ENDIAN)
                        SWITCH_INT(i);
                
                /* read the sampleformat */
                readPackedFile(pf, &shortbuf, 2);
-               if(G.order==B_ENDIAN)
-               {
+               if(G.order==B_ENDIAN) {
                        char s_i, *p_i;
                        p_i= (char *)&(shortbuf);
                        s_i= p_i[0];
@@ -460,8 +523,7 @@ int sound_get_filetype_from_header(bSound* sound, PackedFile* pf)
                        p_i[1]= s_i;
                }
                
-               if (shortbuf == SND_WAVE_FORMAT_PCM)
-               {
+               if (shortbuf == SND_WAVE_FORMAT_PCM) {
                        filetype = SAMPLE_WAV;
                }
                else
@@ -490,28 +552,24 @@ int sound_get_filetype_from_header(bSound* sound, PackedFile* pf)
 #ifdef USE_FMOD
                }
        }
-       else if (!memcmp(buffer, "OggS", 4))
-       {
+       else if (!memcmp(buffer, "OggS", 4)) {
                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;
        }
 #endif
-       else
-       {
+       else {
                filetype = SAMPLE_INVALID;
                if (G.f & G_DEBUG) printf("Unsupported sound format: %s\n", sound->name);
        }
        
-#endif
        return filetype;
 }
 
 
 
-int check_filetype(bSound* sound, PackedFile* pf)
+static int check_filetype(bSound *sound, PackedFile *pf)
 {
 //     char* pdest;
        sound->sample->type = SAMPLE_INVALID;
@@ -541,8 +599,7 @@ int check_filetype(bSound* sound, PackedFile* pf)
        sound->sample->type = sound_get_filetype_from_header(sound, pf);
 
        /* get some info from the sample */
-       switch (sound->sample->type)
-       {
+       switch (sound->sample->type) {
        case SAMPLE_WAV:
                {
                        sound_read_wav_data(sound, pf);
@@ -567,23 +624,22 @@ int check_filetype(bSound* sound, PackedFile* pf)
 
 
 
-int sound_load_sample(bSoundsound)
+int sound_load_sample(bSound *sound)
 {
        int result = FALSE;
-#if GAMEBLENDER == 1
-       PackedFile* pf;
+       PackedFile *pf;
        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)
-       {
+       if (sound->sample->type == SAMPLE_UNKNOWN || sound->snd_sound == NULL) {
                /* find... */
                pf = sound_find_packedfile(sound);
 
                /* ...or create a (temp)packedfile */
-               if (pf == NULL)
-               {
+               if (pf == NULL) {
                        pf = newPackedFile(sound->name);
                        
                        /* if autopack is off, free the pf afterwards */
@@ -592,14 +648,12 @@ int sound_load_sample(bSound* sound)
                }
                
                /* if we have a valid pf... */
-               if (pf)
-               {
+               if (pf) {
                        /* check the content of the pf */
                        check_filetype(sound, pf);
 
                        /* check if the sampletype is supported */
-                       if (sound->sample->type != SAMPLE_INVALID && sound->sample->type != SAMPLE_UNKNOWN)
-                       {
+                       if (sound->sample->type != SAMPLE_INVALID && sound->sample->type != SAMPLE_UNKNOWN) {
                                /* register the sample at the audiodevice */
                                buffer = SND_AddSample(ghSoundScene, sound->sample->name, pf->data, pf->size);
 
@@ -618,61 +672,56 @@ int sound_load_sample(bSound* sound)
                                        result = TRUE;
                        }
                        /* if not, free the pf */
-                       else
-                       {
+                       else {
                                freePF = TRUE;
                        }
                        
                        /* if you want it freed, make it so */
-                       if (freePF)
-                       {
+                       if (freePF) {
                                freePackedFile(pf);
                                pf = NULL;
                        } 
                        /* or else connect the pf to the sound and sample */
-//                     else
-//                     {
+//                     else {
                                sound->newpackedfile = pf;
                                sound->sample->packedfile = pf;
 //                     }
                }
-               else 
-               {
+               else  {
                        if (G.f & G_DEBUG) printf("%s: File not found!\n", sound->name);
                        sound->sample->type = SAMPLE_INVALID;
                }
        }
        /* if the sample ain't invalid, we're ready to go! */
-       else if (sound->sample->type != SAMPLE_INVALID)
-       {
+       else if (sound->sample->type != SAMPLE_INVALID) {
                result = TRUE;
        }
 
-#endif
-
        return result;
 }
 
 
 
-bSound* sound_new_sound(char* name)
+bSound *sound_new_sound(char *name)
 {
        bSound *sound = NULL;
-#if GAMEBLENDER == 1
        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);
-       BLI_convertstringcode(str, G.sce, G.scene->r.cfra);
+       BLI_convertstringcode(str, G.sce);
 
        /* check if the sample on disk can be opened */
        file = open(str, O_BINARY|O_RDONLY);
        
-       if (file != -1)
-       {
+       if (file != -1) {
                close(file);
 
+
                /* do some name magic */
                len = strlen(name);
                while (len > 0 && name[len - 1] != '/' && name[len - 1] != '\\')
@@ -688,8 +737,7 @@ bSound* sound_new_sound(char* name)
                /* load the sample & check if this blender supports the sound format */
 //             sound_load_sample(sound);
 
-               if (sound->sample->type == SAMPLE_INVALID)
-               {
+               if (sound->sample->type == SAMPLE_INVALID) {
                        free_libblock(&G.main->sound, sound);
                        sound = NULL;
                } 
@@ -703,7 +751,6 @@ bSound* sound_new_sound(char* name)
                }
        }
        
-#endif 
        return (sound);
 }
 
@@ -712,14 +759,11 @@ bSound* sound_new_sound(char* name)
 int sound_set_sample(bSound *sound, bSample *sample)
 {
        int result = TRUE;
-#if GAMEBLENDER == 1
-       /* decrease the usernumber for this sample */
-       if (sound->sample)
-               sound->sample->id.us--;
-
+       
+       if(ghSoundScene==NULL) sound_init_audio();
+       
        /* delete the soundobject */
-       if (sound->snd_sound)
-       {
+       if (sound->snd_sound) {
                SND_RemoveSound(ghSoundScene, sound->snd_sound);
                sound->snd_sound = NULL;
        }
@@ -728,42 +772,35 @@ int sound_set_sample(bSound *sound, bSample *sample)
        sound->sample = sample;
        sound->newpackedfile = NULL;
        
-       /* increase the usercount */
-       if (sound->sample)
-       {
-               sound->sample->id.us++;
+       if (sound->sample) {
 
                /* and set the right pf */
                sound->newpackedfile = sample->packedfile;
 
                /* if the sampletype is unknown initialize it */
-               if (sound->sample->type == SAMPLE_UNKNOWN)
-               {
+               if (sound->sample->type == SAMPLE_UNKNOWN) {
                        sound_initialize_sample(sound);
                        
                        /* load the sample & check if this blender supports the sound format */
-                       if (!sound_load_sample(sound))
-                       {
+                       if (!sound_load_sample(sound)) {
                                result = FALSE;
                        }
                }
        }
 
-#endif 
-
        return result;
 }
 
 
 
-bSample *sound_new_sample(bSound * sound)
+bSample *sound_new_sample(bSound *sound)
 {
+       char samplename[FILE_MAX];
        bSample *sample = NULL;
        int len;
        char *name;
        
-       if (sound != NULL)
-       {
+       if (sound != NULL) {
                name = sound->name;     
                len = strlen(name);
                /* do some name magic */
@@ -771,7 +808,10 @@ bSample *sound_new_sample(bSound * sound)
                        len--;
                
                /* allocate the memory for the sample */
-               sample = alloc_libblock(samples, ID_SAMPLE, name + len);
+               sample = MEM_callocN(sizeof(bSample), "sample");
+               BLI_strncpy(sample->id.name+2, name+len, 20);
+               BLI_addtail(samples, sample);   /* samples is ugly global */
+               
                sample->data = &sample->fakedata[0];
                sample->type = SAMPLE_UNKNOWN;
                
@@ -782,9 +822,12 @@ bSample *sound_new_sample(bSound * sound)
                sample->alindex = SAMPLE_INVALID;
 
                /* convert sound->name to abolute filename */
-               strcpy(sample->name, sound->name);
-               BLI_convertstringcode(sample->name, G.sce, G.scene->r.cfra);
-               
+               /* TODO: increase sound->name, sample->name and strip->name to FILE_MAX, to avoid
+                  cutting off sample name here - elubie */
+               BLI_strncpy(samplename, sound->name, FILE_MAX);         
+               BLI_convertstringcode(samplename, G.sce);
+               BLI_strncpy(sample->name, samplename, FILE_MAXDIR);
+
                /* connect the pf to the sample */
                if (sound->newpackedfile)
                        sample->packedfile = sound->newpackedfile;
@@ -798,26 +841,23 @@ bSample *sound_new_sample(bSound * sound)
 
 
 /* find a sample that might already be loaded */
-bSample* sound_find_sample(bSound* sound)
+bSample *sound_find_sample(bSound *sound)
 {
-       bSamplesample;
+       bSample *sample;
        char name[FILE_MAXDIR + FILE_MAXFILE];
        char samplename[FILE_MAXDIR + FILE_MAXFILE];
        
        // convert sound->name to abolute filename
        strcpy(name, sound->name);
-       BLI_convertstringcode(name, G.sce, G.scene->r.cfra);
+       BLI_convertstringcode(name, G.sce);
        
        /* search through the list of loaded samples */
        sample = samples->first;
-       
-       while (sample)
-       {
+       while (sample) {
                strcpy(samplename, sample->name);
-               BLI_convertstringcode(samplename, G.sce, G.scene->r.cfra);
+               BLI_convertstringcode(samplename, G.sce);
                
-               if (strcmp(name, samplename) == 0)
-               {
+               if (strcmp(name, samplename) == 0)      {
                        break;
                }
                sample = sample->id.next;
@@ -828,14 +868,15 @@ bSample* sound_find_sample(bSound* sound)
 
 
 
-int sound_sample_is_null(bSoundsound)
+int sound_sample_is_null(bSound *sound)
 {
        int result = FALSE;
-       bSample* sample;
+       bSample *sample;
+       
+       if(ghSoundScene==NULL) sound_init_audio();
        
        /* find the right sample or else create one */
-       if (sound->sample == NULL)
-       {
+       if (sound->sample == NULL) {
                /* find... */
                sample = sound_find_sample(sound);
 
@@ -855,8 +896,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 
 }
 
@@ -865,16 +908,20 @@ 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
 }
 
 
 
-void sound_play_sound(bSoundsound)
+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);
 
@@ -981,59 +1028,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)
 {
-#if GAMEBLENDER == 1
        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 (noaudio)
-               SND_SetDeviceType(snd_e_dummydevice);
-
-       ghAudioDeviceInterface = SND_GetAudioDevice();
-       ghSoundScene = SND_CreateScene(ghAudioDeviceInterface);
-
-       sound_init_listener();
-#endif 
+               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)
 {
-#if GAMEBLENDER == 1
-       SND_DeleteScene(ghSoundScene);
-       SND_ReleaseDevice();
-       sound_exit_listener();
-#endif 
+       if(ghSoundScene) {
+               SND_DeleteScene(ghSoundScene);
+               SND_ReleaseDevice();
+               ghSoundScene = NULL;
+       }
 }