Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 520177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:07:05+00:00 2026-05-13T08:07:05+00:00

I am trying to manipulate a wave file header. I have loaded the file

  • 0

I am trying to manipulate a wave file header. I have loaded the file into a NSMutableData and am trying to change the wav file header info. My problem is that on the line:

[maindata replaceBytesInRange:range withBytes:((UInt32 *)tfSC1Length)];

I am receiving a EXC_BAD_ACCESS error. Can’t understand why. Any ideas?

The code I am using is as follows:

// calculate total file length (minus 8 for the header), chunk size, number of channels, sample rate and bits per sample
int tfLength        = ([maindata length] - 8);
int tfChannels      = 1;   // mono
int tfSampleRate    = 44100;
int tfDataLength    = ([maindata length] - 44);
int tfBitsPerSample = 16;

int tfSC1Length     = 16;
int tfAudioFormat   = 1;

//we are rebuilding the header for the wave files...

// chunk identifier
((char *)[maindata mutableBytes])[0] = 'R';
((char *)[maindata mutableBytes])[1] = 'I';
((char *)[maindata mutableBytes])[2] = 'F';
((char *)[maindata mutableBytes])[3] = 'F';

// size (less 8 bytes)
NSRange range;
range.location = 4;
range.length = 4;
[maindata replaceBytesInRange:range withBytes:(UInt32 *)tfLength];

// the file format
((char *)[maindata mutableBytes])[8]  = 'W';
((char *)[maindata mutableBytes])[9]  = 'A';
((char *)[maindata mutableBytes])[10] = 'V';
((char *)[maindata mutableBytes])[11] = 'E';

// subchunk1 ID
((char *)[maindata mutableBytes])[12] = 'f';
((char *)[maindata mutableBytes])[13] = 'm';
((char *)[maindata mutableBytes])[14] = 't';
((char *)[maindata mutableBytes])[15] = ' ';

// subchunk length
range.location = 16;
range.length   = 4;
[maindata replaceBytesInRange:range withBytes:((UInt32 *)tfSC1Length)];

// audio format
range.location = 20;
range.length   = 2;
[maindata replaceBytesInRange:range withBytes:((UInt16 *)tfAudioFormat)];

// number of channels
range.location = 22;
range.length   = 2;
[maindata replaceBytesInRange:range withBytes:((UInt16 *)tfChannels)];

// sample rate
range.location = 24;
range.length   = 4;
[maindata replaceBytesInRange:range withBytes:((UInt32 *)tfSampleRate)];

// byte rate
range.location = 28;
range.length   = 4;
[maindata replaceBytesInRange:range withBytes:((UInt32 *)(tfSampleRate * ((tfBitsPerSample * tfChannels) / 8)))];

// block align
range.location = 32;
range.length   = 2;
[maindata replaceBytesInRange:range withBytes:((UInt16 *)((tfBitsPerSample * tfChannels) / 8))];

// bits per sample
range.location = 34;
range.length   = 2; 
[maindata replaceBytesInRange:range withBytes:((UInt16 *)tfBitsPerSample)];

// adjust the length field of the wave file...
((char *)[maindata mutableBytes])[36] = 'd';
((char *)[maindata mutableBytes])[37] = 'a';
((char *)[maindata mutableBytes])[38] = 't';
((char *)[maindata mutableBytes])[39] = 'a';

// length of the audio data
range.location = 40;
range.length   = 4;
[maindata replaceBytesInRange:range withBytes:(UInt32 *)tfDataLength];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:07:05+00:00Added an answer on May 13, 2026 at 8:07 am

    You need to use the “&” operator to get the address of the integer values if you want to copy their values to the data buffer:

    So instead of:

    [maindata replaceBytesInRange:range withBytes:(UInt32 *)tfLength];
    

    use:

    [maindata replaceBytesInRange:range withBytes: &tfLength];
    

    This is causing your crash since it’s treating your lengths as pointers right now.

    Also, what is the byte order expected by the wav file format for 16 bit and 32 bit integers? Is it the same as the byte order on the iPhone and on the iPhone simulator? You can use the CFByteOrder utilities to convert to the correct byte order.

    Also, I’d use the exact types for the counts (UInt32, UInt16) instead of int when declaring the lengths to guarantee that you are copying the lengths correctly. The way it’s implemented now you are assuming that UInt32 is binary identical to int (probably true) and the first 2 bytes of an int are the same as a UInt16 with the same value (not necessarily true).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The problem I'm facing is that I'm trying to manipulate (in this particular case
So I have an existing database that I'm trying to manipulate through ActiveRecord. All
I'm trying to manipulate an audio file (mp3), using <audio> and javascript but nothing
I am trying to manipulate mp3 tags on a certain web service that is
You see I'm trying to manipulate some elements when comparing their index. The problem
I'm trying to manipulate my mysql tables with PHP but when I update them
I'm trying to work with a scroll view controller that needs to adjust it's
I have a website that has a secure area (https) and the rest of
I'm trying to execute a few python scripts in order to manipulate some images
I've been fighting with this for a few days now and it looks like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.