[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [11017] branches/soc-2007-hcube/source/ blender/src/soundsystem.c: Added new blender side soundsystem implementation.

Csaba Hruska csaba.hruska at gmail.com
Fri Jun 22 19:04:48 CEST 2007


Revision: 11017
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=11017
Author:   hcube
Date:     2007-06-22 19:04:48 +0200 (Fri, 22 Jun 2007)

Log Message:
-----------
Added new blender side soundsystem implementation.

Modified Paths:
--------------
    branches/soc-2007-hcube/source/blender/src/soundsystem.c

Modified: branches/soc-2007-hcube/source/blender/src/soundsystem.c
===================================================================
--- branches/soc-2007-hcube/source/blender/src/soundsystem.c	2007-06-22 17:04:01 UTC (rev 11016)
+++ branches/soc-2007-hcube/source/blender/src/soundsystem.c	2007-06-22 17:04:48 UTC (rev 11017)
@@ -1,5 +1,6 @@
 #include <string.h>
 #include <stdlib.h>
+#include <stdio.h>
 #include <assert.h>
 
 #include "MEM_guardedalloc.h"
@@ -19,19 +20,22 @@
 #include "BKE_sound.h"
 #include "BKE_packedFile.h"
 
-#include "BAU_sound.h"
+#include "BAU_soundsystem.h"
 #include "BSE_seqaudio.h"
 
 #include "SND_C-api.h"
 
 /*
+
 typedef struct bSample {
 	ID id;
-	void *data;
-	void *snd_sample;
+
+	void *data;			// unused
+	void *snd_sample;	// attach tinySND handle here
+	
 	short type, bits;
-	short channels;
-	int len, rate;
+	short channels;		// USE THIS
+	int len, rate;		// USE THIS
 //	int buffer;
 	int alindex;
 	char fakedata[16];
@@ -46,10 +50,14 @@
 	ID id;
 	char name[160];
 	struct bSample *sample;
-	void *stream;
+	
+	void *stream;	// unused
+	
 	struct PackedFile *packedfile;
 	struct PackedFile *newpackedfile;
-	void *snd_sound;
+	
+	void *snd_sound;// unused
+	
 	struct Ipo *ipo;
 	float volume, panning;
 	float attenuation;
@@ -58,26 +66,26 @@
 	float max_gain;
 	float distance;
 	int flags;
-	int streamlen;
-	char channels;
+	int streamlen;	// DONT USE THIS
+	char channels;	// DONT USE THIS
 	char highprio;
 	char pad[10];
 } bSound;
 
 */
 
-void sound_initialize(void)
+void audio_initialize(void)
 {
 	SND_Initialize();
 	seqaudio_initialize();
 }
 
-int sound_is_initialized(void)
+int audio_isinitialized(void)
 {
 	return SND_IsInitialized();
 }
 
-void sound_finalize(void)
+void audio_finalize(void)
 {
 	seqaudio_finalize();
 	
@@ -87,76 +95,380 @@
 	}
 }
 
-bSound* sound_new_sound(char *name)
+int audio_getmixrate(void)
 {
+	return SND_GetMixRate();
+}
+
+struct bSample*	audio_sample_new( char *name )
+{
+	bSample *sample = NULL;
+	int len;
+	
+	assert( name != NULL );
+	
+	len = strlen(name);
+	// do some name magic 
+	while( len > 0 && name[len - 1] != '/' && name[len - 1] != '\\' )
+	{
+		len--;
+	}
+		
+	// allocate the memory for the sample 
+	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;
+		
+	// some default settings. We get divide by zero if these values are not set	
+	sample->channels = 1;
+	sample->rate = 44100;
+	sample->bits = 16;
+	sample->alindex = SAMPLE_INVALID;
+
+	// convert sound->name to abolute filename 
+	strcpy( sample->name, name );
+	BLI_convertstringcode( sample->name, G.sce, G.scene->r.cfra );
+		
+	sample->packedfile = NULL;
+	// connect the pf to the sample 
+	/*
+	if( sound->newpackedfile )
+	{
+		sample->packedfile = sound->newpackedfile;
+	}
+	else
+	{
+		sample->packedfile = sound_find_packedfile(sound);
+	}
+	*/
+	return sample;
+}
+
+struct bSample*	audio_sample_find( char *fname )
+{
+	bSample *sample;
+	char name[FILE_MAXDIR + FILE_MAXFILE];
+	char samplename[FILE_MAXDIR + FILE_MAXFILE];
+	
+	assert( fname != NULL );
+	
+	// convert fname to abolute filename
+	strcpy( name, fname );
+	BLI_convertstringcode( name, G.sce, G.scene->r.cfra );
+	
+	// search through the list of loaded samples 
+	sample = samples->first;
+	while( sample )
+	{
+		strcpy( samplename, sample->name );
+		BLI_convertstringcode( samplename, G.sce, G.scene->r.cfra );
+		
+		if( strcmp( name, samplename ) == 0 )
+		{
+			break;
+		}
+		sample = sample->id.next;
+	}
+	
+	return sample;
+}
+
+int audio_sample_load( struct bSample* sample )
+{
+	SND_SoundIHandle sndHandle;
+	SND_SoundInfo sndInfo;
+	
+	assert( sample != NULL );
+
+	if( sample->type != SAMPLE_UNKNOWN )
+	{
+		// return if sample is already loaded
+		return TRUE;
+	}
+	
+	sndHandle = SND_NewSound( sample->name );
+	if( sndHandle == NULL )
+	{
+		return FALSE;
+	}
+	sample->snd_sample = sndHandle;
+	sample->type = SAMPLE_WAV;
+	
+	// fill sample info fields
+	SND_GetSoundInfo( sndHandle, &sndInfo );
+	sample->len			= sndInfo.framesNum;
+	sample->channels	= sndInfo.channelsNum;
+	sample->bits		= sndInfo.bitRate;
+	sample->rate		= sndInfo.frameRate;
+	
+	return TRUE;
+/*
+	int result = FALSE;
+	PackedFile *pf;
+	int freePF = FALSE;
+	int buffer = -1;
+
+
+	// check the sample (valid?) 
+	if (sound->sample->type == SAMPLE_UNKNOWN || sound->snd_sound == NULL) {
+		// find... 
+		pf = sound_find_packedfile(sound);
+
+		// ...or create a (temp)packedfile 
+		if (pf == NULL) {
+			pf = newPackedFile(sound->name);
+			
+			// if autopack is off, free the pf afterwards 
+			if ((G.fileflags & G_AUTOPACK) == 0)
+				freePF = TRUE;
+		}
+		
+		// if we have a valid 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) {
+				// register the sample at the audiodevice 
+				buffer = SND_AddSample(ghSoundScene, sound->sample->name, pf->data, pf->size);
+
+				// create a soundobject 
+				sound->snd_sound = SND_CreateSound();
+				SND_SetSampleName(sound->snd_sound, sound->sample->name);
+					
+				// add the soundobject to the soundscene  
+				if (SND_CheckBuffer(ghSoundScene, sound->snd_sound))
+					SND_AddSound(ghSoundScene, sound->snd_sound);
+				else
+					if (G.f & G_DEBUG) printf("error: sample didn't load properly\n");
+				
+				// if it was places in buffer[0] or higher, it succeeded 
+				if (buffer >= 0)
+					result = TRUE;
+			}
+			// if not, free the pf 
+			else {
+				freePF = TRUE;
+			}
+			
+			// if you want it freed, make it so 
+			if (freePF) {
+				freePackedFile(pf);
+				pf = NULL;
+			} 
+			// or else connect the pf to the sound and sample 
+//			else {
+				sound->newpackedfile = pf;
+				sound->sample->packedfile = pf;
+//			}
+		}
+		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) {
+		result = TRUE;
+	}
+
+	return result;
+*/
+}
+
+void audio_sample_unload( struct bSample* sample )
+{
+	assert( sample != NULL );
+
+	if( sample->type == SAMPLE_UNKNOWN )
+	{
+		// return if sample is not loaded
+		return;
+	}
+	
+	SND_DeleteSound( (SND_SoundIHandle)sample->snd_sample );
+	sample->snd_sample = NULL;
+	sample->type = SAMPLE_UNKNOWN;
+}
+
+struct bSound* audio_sound_new( char *name )
+{
 	bSound *sound = NULL;
-	SND_SoundIHandle soundHandle;
-//	char str[FILE_MAXDIR+FILE_MAXFILE];
+	int len;
+	FILE *file;
+	char str[FILE_MAXDIR+FILE_MAXFILE];
+
+	assert( name != NULL );
 	
+	// set it up in a  proper funcyion, not here
+	//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 );
+
+	// check if the sample on disk can be opened 
+	file = fopen( str, "rb" );
+	
+	if( file == NULL )
+	{
+		return NULL;
+	}
+	
+	fclose(file);
+
+	// do some name magic 
+	len = strlen( name );
+	while( len > 0 && name[len - 1] != '/' && name[len - 1] != '\\' )
+	{
+		len--;
+	}
+	
 	// allocate some memory for the sound 
-	sound = alloc_libblock(&G.main->sound, ID_SO, "sound");
-	strcpy(sound->name, name);
-		
-	soundHandle = SND_NewSound( name );
-	if (soundHandle == NULL)
+	sound = alloc_libblock( &G.main->sound, ID_SO, name + len );
+	strcpy( sound->name, name );
+	
+	// load the sample & check if this blender supports the sound format 
+	sound->sample = audio_sample_new( sound->name );
+	audio_sample_load( sound->sample );
+
+	if( sound->sample->type == SAMPLE_INVALID )
 	{
-		free_libblock(&G.main->sound, sound);
+		free_libblock( &G.main->sound, sound );
 		sound = NULL;
 	} 
 	else
 	{
-		bSample *sample;
-		SND_SoundInfo sndInfo;
+		sound->volume = 1.0;
+		sound->attenuation = 1.0;
+		sound->distance = 1.0;
+		sound->min_gain = 0.0;
+		sound->max_gain = 1.0;
+	}
+	
+	return sound;
+}
+
+struct bSound* audio_sound_clone( struct bSound* originalsound )
+{
+	bSound *sound = NULL;
+	char name[160];
+	int len;
+	
+	assert( originalsound != NULL );
+	
+	// do some name magic 
+	strcpy( name, originalsound->name );
+	len = strlen( name );
+	while( (len > 0) && (name[len - 1] != '/') && (name[len - 1] != '\\') )
+	{
+		len--;
+	}
 		
-		SND_GetSoundInfo( soundHandle, &sndInfo );
+	// allocate the needed memory 
+	sound = alloc_libblock( &G.main->sound, ID_SO, name + len );
 		
-		sound->volume		= 1.0;
-		sound->attenuation	= 1.0;
-		sound->distance		= 1.0;
-		sound->min_gain		= 0.0;
-		sound->max_gain		= 1.0;
-		sound->streamlen	= sndInfo.framesNum;
-		sound->channels		= sndInfo.channelsNum;
-		// private data
-		sound->snd_sound	= soundHandle;
-		
-		sample = MEM_callocN(sizeof(bSample), "sample");
+	// set the samplename 
+	strcpy( sound->name, name );
 
-		// needed by sound_free_all_samples
-		BLI_addtail(samples, sample);	/* samples is ugly global */
+	// clone sample too
+	sound->sample = audio_sample_new( sound->name );
+	audio_sample_load( sound->sample );
+
+	// and copy the data from the original 
+	sound->attenuation	= originalsound->attenuation;
+	sound->distance		= originalsound->distance;
+	sound->max_gain		= originalsound->max_gain;
+	sound->min_gain		= originalsound->min_gain;
+	sound->newpackedfile= originalsound->newpackedfile;
+	sound->panning		= originalsound->panning;
+	sound->pitch		= originalsound->pitch;
+	sound->sample		= originalsound->sample;
+	sound->volume		= originalsound->volume;
 		
-		sample->len			= sndInfo.framesNum;
-		sample->channels	= sndInfo.channelsNum;
-		sample->bits		= sndInfo.bitRate;
-		sample->rate		= sndInfo.frameRate;
-		sample->packedfile	= NULL;
-		
-		sound->sample = sample;
+	if( originalsound->flags & SOUND_FLAGS_3D )
+	{
+		sound->flags |= SOUND_FLAGS_3D;
 	}
+	else
+	{
+		sound->flags &= ~SOUND_FLAGS_3D;
+	}
 	
-	return (sound);
+	return sound;
 }
 
-void sound_play_sound(struct bSound *sound)
+struct bSound* audio_sound_findbyid(char *id_name)
 {
+	bSound *sound;
+	
+	assert( id_name != NULL );
+	
+	// look for sound with same *id* name
+	sound = G.main->sound.first;
+	while( sound )
+	{
+		if( strcmp(sound->id.name + 2, id_name) == 0 )
+		{
+			break;
+		}
+		sound = sound->id.next;
+	}
+	
+	return sound;
+}
+
+void audio_sound_loadall(void)
+{
+	bSound *sound;
+
+	// initialize sample blocks 
+	sound = G.main->sound.first;
+	while( sound )
+	{
+		if( sound->sample == NULL )
+		{

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list