50222. Truncate Wave File

I'm a slow walker, but I never walk backwards.

Task Description

Write a program to truncate a WAVE file. You have a WAVE file $A$, and you need to generate a new WAVE file $B$ that plays the first $k$ seconds of data in file $A$. For simplicity, this task assumes that a WAV starts with a file header followed by a single data block of a single channel. Please refer to the following figure to understand the header. Note that the number of data bytes per second (the data rate) is the product of the sampling rate and the number of bytes per data since we have only one channel.

headerheader

Also, you should read this sample code to understand how to truncate a WAVE file.

Sample code here

Note

Note that you need to modify the chunk size(總區塊大小) and subchunk size(子區塊2大小) in the header of the new WAVE file. Also, note that you can use diff to compare two files in UNIX or use FC in Windows.

Sample Command of comparing in Windows

There are two types of terminals in Windows, cmd and Powershell. This example is in cmd.

fc /b A.wav B.wav

This example is in Powershell.

fc.exe /b A.wav B.wav

Sample Command of comparing in Linux

This example is in a linux terminal. If there is no difference, diff will not output anything.

diff A.wav B.wav

Input Format

There are two lines in the input.

  • The first line has the file name of the original WAVE file.
  • The second line has the file name of the truncated file.
  • The third line has the duration of the truncated WAVE file in seconds.

Output Format

You should generate a WAVE file with the file name from the third line in the input.

Sample Input 1

You can download the sample file sample.wav

sample.wav
0.wav
3

Sample Output 1

You can download the sample truncated file 0.wav

Estimated Cyclomatic Number

3

Discussion