Anonymous avatar Anonymous committed ae3b4d5

Fixed sound issues(hopefully). I did not hear static on my end, and
neither did any friends. However, I did take the time to make the sound
process more efficient, do less copying of buffers(before I was copying
buffers 3-4 times...XD), and this should also hopefully deal better with
memory leak issues.

Comments (0)

Files changed (6)

 SCRAP = -lX11
 PORTMIDI = -lportmidi
 PORTTIME = -lporttime
-AVCODEC   = -lavcodec 
+#AVCODEC   = -lavcodec 
 AVFORMAT  = -lavformat 
-AVDEVICE  = -lavdevice 
-AVUTIL    = -lavutil 
+#AVDEVICE  = -lavdevice 
+#AVUTIL    = -lavutil 
 SWSCALE   = -lswscale 
-ZLIB = -lz
+#ZLIB = -lz
 #--EndConfig
 
 #DEBUG = -C-W -C-Wall
 _numericsndarray src/_numericsndarray.c $(SDL) $(MIXER) $(DEBUG)
 movie src/movie.c $(SDL) $(SMPEG) $(DEBUG)
 #_movie src/_gsound.c src/_gmovie.c src/_gcommand.c src/gmovie.c $(SDL) $(AVCODEC) $(AVFORMAT) $(AVUTIL)  $(AVDEVICE)  $(SWSCALE) $(MIXER) -lz -lwsock32  $(DEBUG)
+_movie src/_gsound.c src/_gmovie.c src/_gcommand.c src/gmovie.c $(SDL)  $(AVFORMAT) $(SWSCALE) $(MIXER) $(DEBUG)
 scrap src/scrap.c $(SDL) $(SCRAP) $(DEBUG)
 _camera src/_camera.c src/camera_v4l2.c src/camera_v4l.c $(SDL) $(DEBUG)
 pypm src/pypm.c $(SDL) $(PORTMIDI) $(PORTTIME) $(DEBUG)
         Dependency('SCRAP', '', 'libX11', ['X11']),
         Dependency('PORTMIDI', 'portmidi.h', 'libportmidi.so', ['portmidi']),
         Dependency('PORTTIME', 'porttime.h', 'libporttime.so', ['porttime']),
-        DebugDependency('FFMPEG', 'libavformat/avformat.h', 'libavformat.a', ['avformat', 'swscale', 'SDL_mixer']),        
+        DebugDependency('AVFORMAT', 'libavformat/avformat.h', 'libavformat.a', ['avformat']),
+        DebugDependency('SWSCALE', 'libswscale/swscale.h', 'libswscale.a', ['swscale']),        
         #Dependency('GFX', 'SDL_gfxPrimitives.h', 'libSDL_gfx.so', ['SDL_gfx']),
     ]
     if not DEPS[0].found:
 
 print "Please give an (absolute)filename of a movie file you'd like to play: ",
 #filename = raw_input()
-filename="./video.flv"
+filename="/home/tyler/video.flv"
 #initialization. It could also have a surface as a second argument, and every 
 # frame will be blitted to that surface. It is the programmer's responsibility
 # to be on time for rendering that surface.
             }
         }
         av_free_packet(pkt);
+	    av_free(frame);
     }
     while(0);
 
 the_end:
-    av_free(frame);
     return 0;
 }
 #if 0
 
 void cb_mixer(int channel)
 {
-    playBuffer(NULL, (uint32_t) 0, channel, 0);
+	Mix_Chunk *mix;
+	mix= Mix_GetChunk(channel);
+	if(mix->abuf)
+		PyMem_Free(mix->abuf);
+	if(mix)
+		PyMem_Free(mix);
+    playBufferQueue();
+    
 }
 
 //initialize the mixer audio subsystem, code cribbed from mixer.c
 {
 	//SDL_mutexP(ainfo->mutex);
     Mix_Chunk *mix;
-    int false=0;
-    int allocated=0;
     if(!ainfo->ended && (ainfo->queue.size>0||ainfo->playing))
     {
-        if(buf)
-        {   
-        	//not a callback call, so we copy the buffer into a buffernode and add it to the queue.
-            BufferNode *node;
-            node = (BufferNode *)PyMem_Malloc(sizeof(BufferNode));
-            node->buf = (uint8_t *)PyMem_Malloc((size_t)len);
-            memcpy(node->buf, buf, (size_t)len);
-            node->len = len;
-            node->next =NULL;
-            node->pts = pts;
-            queue_put(&ainfo->queue, node);
-            //SDL_mutexV(ainfo->mutex);
-            if(ainfo->channel<0)
-            	ainfo->channel=channel;
-            return ainfo->channel;
-        }
-        else if(!buf && ainfo->queue.size==0)
-        {  
-            
-            //callback call but when the queue is empty, so we just load a short empty sound.
-            buf = (uint8_t *) PyMem_Malloc((size_t)1024);
-            memset(buf, 0, (size_t)1024);
-            ainfo->current_frame_size=1;
-            len=1024;
-            allocated =1;
-        	false=1;
-        }
-        else
-        {
-            //callback call, and convenienty enough, the queue has a buffer ready to go, so we copy it into buf
-            BufferNode *newNode;
-            queue_get(&ainfo->queue, &newNode);
-            if(!newNode)
-            {
-                //SDL_mutexV(ainfo->mutex);
-                return -1;
-            }
-            ainfo->current_frame_size=newNode->len;
-            pts=newNode->pts;
-            buf = (uint8_t *)PyMem_Malloc((size_t)newNode->len);
-            memcpy(buf, newNode->buf, newNode->len);
-            len=newNode->len;
-            PyMem_Free(newNode->buf);
-            newNode->buf=NULL;
-            PyMem_Free(newNode);
-            newNode=NULL;
-            allocated=1;
-        }
-    }
-    
-    //we assume that if stopped is true, then
-    if(ainfo->ended && !buf)
-    {
-        //callback call but when the queue is empty, so we just load a short empty sound.
-        buf = (uint8_t *) PyMem_Malloc((size_t)1024);
-        memset(buf, 0, (size_t)1024);
-        ainfo->current_frame_size=1;
-        len=1024;
-        allocated =1;
-        false=1;
-    }
-    else if(ainfo->ended && buf)
-    {
-    	//toss the buffer out. we don't need it.
-    	return ainfo->channel;
-    }
+    	//not a callback call, so we copy the buffer into a buffernode and add it to the queue.
+        BufferNode *node;
+        node = (BufferNode *)PyMem_Malloc(sizeof(BufferNode));
+        node->buf = (uint8_t *)PyMem_Malloc((size_t)len);
+        memcpy(node->buf, buf, (size_t)len);
+        node->len = len;
+        node->next =NULL;
+        node->pts = pts;
+        queue_put(&ainfo->queue, node);
+        //SDL_mutexV(ainfo->mutex);
+        if(ainfo->channel<0)
+        	ainfo->channel=channel;
+        return ainfo->channel;
+    }     
     //regardless of 1st call, or a callback, we load the data from buf into a newly allocated block.
     mix= (Mix_Chunk *)PyMem_Malloc(sizeof(Mix_Chunk));
-    mix->allocated=0;
+    mix->allocated=1;
 	mix->abuf = (Uint8 *)PyMem_Malloc((size_t)len);
     memcpy(mix->abuf, buf, len);
     mix->alen = (Uint32 )len;
     	
 	}
     ainfo->current_frame_size =len;
-    int chan = ainfo->channel;
-    
-    //SDL_mutexV(ainfo->mutex);
-    int paused = Mix_Paused(channel);
-    if(paused)
+    int playing = Mix_Playing(channel);
+    if(playing)
     {
-    	ainfo->ended=0;
-        Mix_Resume(-1);
+    	return channel;
     }
-    
-    int playing = Mix_Playing(chan);
-    if(playing && allocated &&false)
-    {
-    	return chan;
-    }
-    if(mix==NULL)
-    {
-    	return chan;
-    }
-    int ret = Mix_PlayChannel(chan, mix, 0);
+    int ret = Mix_PlayChannel(channel, mix, 0);
     ainfo->channel = ret;
-    if(allocated)
-    {
-        PyMem_Free(buf);
-    }
     return ret;
 }
 
     return 0;
 }
 
+void playBufferQueue(void)
+{
+	uint8_t *buf;
+	int len=0;
+	int64_t pts;
+	if(!ainfo->ended && ainfo->queue.size<=0)
+	{            
+        //callback call but when the queue is empty, so we just load a short empty sound.
+        buf = (uint8_t *) PyMem_Malloc((size_t)128);
+        memset(buf, 0, (size_t)128);
+        ainfo->current_frame_size=1;
+        len=128;
+    }
+    else if(!ainfo->ended && ainfo->queue.size>0)
+    {
+        //callback call, and convenienty enough, the queue has a buffer ready to go, so we copy it into buf
+        BufferNode *newNode;
+        queue_get(&ainfo->queue, &newNode);
+        if(!newNode)
+        {
+            //SDL_mutexV(ainfo->mutex);
+            return;
+        }
+        ainfo->current_frame_size=newNode->len;
+        pts=newNode->pts;
+        buf = (uint8_t *)newNode->buf;
+        ainfo->current_frame_size=newNode->len;
+        len=newNode->len;
+        PyMem_Free(newNode);
+        newNode=NULL;
+    }
+    
+    //we assume that if stopped is true, then
+    if(ainfo->ended && !buf)
+    {
+        //callback call but when the queue is empty, so we just load a short empty sound.
+        buf = (uint8_t *) PyMem_Malloc((size_t)1024);
+        memset(buf, 0, (size_t)1024);
+        ainfo->current_frame_size=1;
+        len=1024;
+    }
+    //regardless of 1st call, or a callback, we load the data from buf into a newly allocated block.
+    Mix_Chunk *mix;
+    mix= (Mix_Chunk *)PyMem_Malloc(sizeof(Mix_Chunk));
+    mix->allocated=1;
+	mix->abuf = buf;
+    mix->alen = (Uint32 )len;
+    mix->volume = 127;
+ 	if(!ainfo->ended && len!=0)
+	{
+    	int bytes_per_sec = ainfo->channels*ainfo->sample_rate*2;
+    	ainfo->audio_clock+= (double) len/(double) bytes_per_sec;    	
+	}
+    ainfo->current_frame_size =len;
+    int chan = ainfo->channel;
+    
+    int ret = Mix_PlayChannel(chan, mix, 0);
+    ainfo->channel = ret;
+    return;
+}
+
 int pauseBuffer(int channel)
 {
     if(channel<=-1)
 int seekBuffer    (double pts);
 int setCallback   (void (*callback) (int channel));
 int resetAudioInfo(void);
+void playBufferQueue(void);
 #endif /*_GSOUND_H_*/
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.