DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Essential Python Libraries: Introduction to NumPy and Pandas
  • Norm of a One-Dimensional Tensor in Python Libraries
  • How To Implement Cosine Similarity in Python
  • How to Use Python for Data Science

Trending

  • How to Set MX Records via API: Automate Email Routing Programmatically
  • 5 Warning Signs Your Data Architecture Needs a Redesign (Before It Falls Apart)
  • The Breach Was Never at the Door
  • The New Insider Threat Isn't Human: Securing AI Agents Before They Secure Themselves

Sound Synthesis with Numpy

By 
Giuseppe Vettigli user avatar
Giuseppe Vettigli
·
Nov. 14, 11 · Interview
Likes (0)
Comment
Save
Tweet
Share
15.4K Views

Join the DZone community and get the full member experience.

Join For Free
Physically, sound is an oscillation of a mechanical medium that makes the surrounding air also oscillate and transport the sound as a compression wave. Mathematically, the oscillations can be described as



where t is the time, and f the frequency of the oscillation. Each musical note vibrates at a particular frequency and to generate a tone we have to generate an oscillation with the appropriate frequency. The following table shows the complete musical scale between middle A and A-880, in the first column we have the tone and in the second the frequency that we have to use to generate the tone:
Tone Freq
A 440
B flat 466
B 494
C 523
C sharp 554
D 587
D sharp 622
E 659
F 698
F sharp 740
G 784
A flat 831
A 880
Sound on a computer is a sequence of numbers and we are going to see how to generate an array that represents a musical tone with numpy. The following function is able to generate a note using the formula above:
from numpy import linspace,sin,pi,int16

# tone synthesis
def note(freq, len, amp=1, rate=44100):
 t = linspace(0,len,len*rate)
 data = sin(2*pi*freq*t)*amp
 return data.astype(int16) # two byte integers
And we can use this function to generate an A tone of 2 seconds with 44100 samples per second in this way:
from scipy.io.wavfile import write
from pylab import plot,show,axis

# A tone, 2 seconds, 44100 samples per second
tone = note(440,2,amp=10000)

write('440hzAtone.wav',44100,tone) # writing the sound to a file

plot(linspace(0,2,2*44100),tone)
axis([0,0.4,15000,-15000])
show()
The script put the sound into a wav file and we can play it with an external player. This plot shows a part of the signal generated by the script:

Blog Source: http://glowingpython.blogspot.com/2011/09/sound-synthesis.html
Article Type:
How-to
NumPy

Opinions expressed by DZone contributors are their own.

Related

  • Essential Python Libraries: Introduction to NumPy and Pandas
  • Norm of a One-Dimensional Tensor in Python Libraries
  • How To Implement Cosine Similarity in Python
  • How to Use Python for Data Science

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook