473,405 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

unpack() function generates Fatal error

Hello all,
I am trying to unpack big wav file (more than 10 mb), because i want to generate Visualization of an wav file, for smaller files (less than 2 mb) it works fine, but for bigger files it gives
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 35 bytes) in /var/www/phptest/mp3processor/thescript.php on line 64
I am pesting the code below
PLEASE HELP
====================
Expand|Select|Wrap|Line Numbers
  1. <?
  2. class wavVisualization{
  3.     var $afiles = array();        
  4.     var $arrData = array();
  5.     var $wavs = array();
  6.  
  7.     function wavVisualization(){}
  8.  
  9.     function loadFile($filenam){
  10.         $content = file_get_contents($filenam) or die("HALT");
  11.         $this->getStructure($content);
  12.         $this->afiles['structure'] = $this->wavs;
  13.         $this->unpackData($this->wavs['Data']);
  14.     }
  15.  
  16.     //== Get structure of wav file ==//
  17.     function getStructure($binstr){
  18.         $this->wavs['ChunkID'] = substr($binstr,0,4); 
  19.         $this->wavs['ChunkSize'] = $this->unpackHeader(substr($binstr,4,4));
  20.         $this->wavs['Format'] = substr($binstr,8,4); 
  21.         $this->wavs['Subchunk1ID'] = substr($binstr,12,4);
  22.         $this->wavs['SubChunk1Size'] = $this->unpackHeader(substr($binstr,16,4));
  23.         $this->wavs['AudioFormat'] = $this->unpackHeader(substr($binstr,20,2));
  24.         $this->wavs['NumChannels'] = $this->unpackHeader(substr($binstr,22,2));
  25.         $this->wavs['SampleRate']  = $this->unpackHeader(substr($binstr,24,4));
  26.         $this->wavs['ByteRate'] = $this->unpackHeader(substr($binstr,28,4));
  27.         $this->wavs['BlockAlign'] = $this->unpackHeader(substr($binstr,32,2));
  28.         $this->wavs['BitsPerSample'] = $this->unpackHeader(substr($binstr,34,2));
  29.  
  30.         //check extra format bytes
  31.         if($this->wavs['SubChunk1Size']==16){
  32.             $this->wavs['Subchunk2ID'] = substr($binstr,36,4);
  33.             $this->wavs['SubChunk2Size'] = $this->unpackHeader(substr($binstr,40,4));
  34.             $this->wavs['Data']= substr($binstr,44,strlen($binstr));
  35.         }
  36.         elseif($this->wavs['SubChunk1Size']==18){
  37.             $this->wavs['ExtraFormatBytes'] = substr($binstr,36,2);
  38.             $this->wavs['Subchunk2ID'] = substr($binstr,38,4);
  39.             $this->wavs['SubChunk2Size'] = $this->unpackHeader(substr($binstr,42,4));
  40.             $this->wavs['Data']= substr($binstr,46,strlen($binstr));
  41.         }
  42.         $this->wavs['NumberSamples']= ($this->wavs['SubChunk2Size']*8)/($this->wavs['NumChannels']*$this->wavs['BitsPerSample']);
  43.     }
  44.  
  45.  
  46.     //== Get Decimal Values ==//
  47.     function unpackHeader($str,$opt=""){
  48.         if(strlen($str)>2){
  49.             $arr = unpack("V*",$str);
  50.         }
  51.         else{
  52.             $arr = unpack("v*",$str);
  53.         }
  54.         return $arr[1];
  55.     }
  56.  
  57.  
  58.     //== unpackData ==//
  59.     function unpackData($strbin){
  60.         if($this->wavs['BlockAlign']==1){
  61.             return unpack("C*",$strbin);                //unsign Character
  62.         }
  63.         else{
  64.             return unpack("s*",$strbin);                //sign int16 //error occuring here//
  65.         }
  66.     }
  67. }
  68. ?>
  69. <?php
  70. $srcaudio="temp_audio_store/lal_1192620354.wav"; //==An WAV file more than 10 mb==//
  71. $audViz = new wavVisualization();
  72. $audViz->loadFile($srcaudio);
  73. ?>
Oct 23 '07 #1
1 1759
pbmods
5,821 Expert 4TB
Heya, santrooper. Welcome to TSDN!

You're getting an out-of-memory error.

Probably the best way to circumvent this would be to load the file in 2 MB chunks using the fread() function.

Of course, in order to avoid out-of-memory errors, you'll also need to keep writing your temporary data to a file as you process. This will slow down your script significantly, so be sure to check the size of the file first.
Oct 23 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
5
by: Geoffrey | last post by:
Hope someone can help. I am trying to read data from a file binary file and then unpack the data into python variables. Some of the data is store like this; xbuffer:...
5
by: grant | last post by:
Hi All, I am pretty new to python and am having a problem intepreting binary data using struct.unpack. I am reading a file containing binary packed data using open with "rb". All the values are...
3
by: Elezar Simeon Papo | last post by:
Hello All, I have a tab separated input file (data.txt) in text format - the file looks like this SCHOOL DEPART1 DEPART2 DEPART3 Harvard Economics Mathematics Physics...
3
by: Andrew Robert | last post by:
Hey everyone, Maybe you can see something I don't. I need to convert a working piece of perl code to python. The perl code is: sub ParseTrig {
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
0
by: Ping Zhao | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I am writing a small program to decode MS bitmap image. When I use statements as follow, it works fine: header = str(struct.unpack('2s',...
17
by: JRough | last post by:
I have used this function to create a string called $headers: function GetHeaders($file_name){ return "<th><a href='".$file_name."&order_by=l_e'>L_E</a></th> <th><a...
19
by: JRough | last post by:
I have used this function to create a string called $headers: function GetHeaders($file_name){ return "<th><a href='".$file_name."&order_by=l_e'>L_E</a></th> <th><a href='"....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.