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 1052395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:05:11+00:00 2026-05-16T17:05:11+00:00

The following code writes a simple sine at frequency 400Hz to a mono WAV

  • 0

The following code writes a simple sine at frequency 400Hz to a mono WAV file. How should this code be changed in order to produce a stereo WAV file. The second channel should be in a different frequency.

import math
import wave
import struct

freq = 440.0
data_size = 40000
fname = "WaveTest.wav"
frate = 11025.0  # framerate as a float
amp = 64000.0     # multiplier for amplitude

sine_list_x = []
for x in range(data_size):
    sine_list_x.append(math.sin(2*math.pi*freq*(x/frate)))

wav_file = wave.open(fname, "w")

nchannels = 1
sampwidth = 2
framerate = int(frate)
nframes = data_size
comptype = "NONE"
compname = "not compressed"

wav_file.setparams((nchannels, sampwidth, framerate, nframes,
    comptype, compname))

for s in sine_list_x:
    # write the audio frames to file
    wav_file.writeframes(struct.pack('h', int(s*amp/2)))

wav_file.close()
  • 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-16T17:05:11+00:00Added an answer on May 16, 2026 at 5:05 pm

    Build a parallel sine_list_y list with the other frequency / channel, set nchannels=2, and in the output loop use for s, t in zip(sine_list_x, sine_list_y): as the header clause, and a body with two writeframes calls — one for s, one for t. IOW, corresponding frames for the two channels “alternate” in the file.

    See e.g. this page for a thorough description of all possible WAV file formats, and I quote:

    Multi-channel digital audio samples
    are stored as interlaced wave data
    which simply means that the audio
    samples of a multi-channel (such as
    stereo and surround) wave file are
    stored by cycling through the audio
    samples for each channel before
    advancing to the next sample time.
    This is done so that the audio files
    can be played or streamed before the
    entire file can be read. This is handy
    when playing a large file from disk
    (that may not completely fit into
    memory) or streaming a file over the
    Internet. The values in the diagram
    below would be stored in a Wave file
    in the order they are listed in the
    Value column (top to bottom).

    and the following table clearly shows the channels’ samples going left, right, left, right, …

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

Sidebar

Related Questions

The following code sample writes a structure variable of type EMPLOYEE to a file
Using the following code to write to a file int main(int argc, char *
Given the following code (it's supposed to write helloworld in a helloworld file, and
I am trying to write some simple Rcpp code examples. This is remarkably easy
I have written the following code to perform some simultaneous HTTP posting and file
I know there is something dead simple I'm missing, but the following code won't
I'm using the following code to write data through a named pipe from one
If I write the following code: session_start(); $_SESSION['user_id']='daniel'; the variable stays fine as long
When I write the following code I get garbage for an output. It is
I write the following code all the time to handle when the enter key

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.