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

  • SEARCH
  • Home
  • 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 8849673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:42:44+00:00 2026-06-14T12:42:44+00:00

I am generating a simple musical instrument using a ST board. basically I have

  • 0

I am generating a simple musical instrument using a ST board. basically I have a sensor which detects motion or spatial angle and generates sounds of differnt pitch and volume depending on the angle. I already have the driver for the audio codec so all I need to do is to generate sound samples and feed the samples to it. Now I am able to get the angle readings from the sensor but the challenging part is how to generate sound. From google search so far, I think I need to generate a sin function with frequency and amplitude(volume) set according to the reading from the sensor. But my codec assumes a sampling rate 0f 48KHz so how would I go about generating sinusoids with different frequencies for a fixed sampling rate?

So far I have done this:

 samplingRate = 48000;
 n = 0;  // reset once there is a change in frequency

// this function is called 48000 times a second
int generateSineWave(float frequency, float volume)
{
  int temp = volume*(sin(2*pi*frequency*n);
  n = n + 1; 
  if (n == samplingRate) {
    n = 0;
  }
  if (abs(temp) > MAXVAL) {
    return ERROR_CODE;
  }
  return temp;
}

This seems to be working (I am hearing something),but I am not sure if it’s generating the right frequency sinusoid. also the sound I am hearing is not very pleasant, how would I go about generating complex tones(like the ones in piano for example)? I guess also my control variables(spatial angles) needs to be low pass filtered. But apart from that any idea on how I could generate more audibly pleasant waves?

  • 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-06-14T12:42:45+00:00Added an answer on June 14, 2026 at 12:42 pm

    Since you’re sampling at 48 kHz (48,000 samples per second), it will take one second, or 48,000 samples, for a 1 Hz sinusoid to complete a single cycle. So you need to normalize, or divide, by the sampling rate:

    temp = volume * sin(2 * pi * n * frequency / samplingRate);
    

    Note that temp must always be in the range from -volume to volume, so you need only check once that volume < MAXVAL, and you can do that before you calculate temp, which will detect an error (very slightly) more efficiently. You’ll find pi is already defined for you as M_PI in math.h.

    You mentioned the tones aren’t very pleasant. That’s a sign that something may be wrong, because pure sinusoids sound, well, pure and are generally pleasing. One simple thing you might try is to start and stop your tones exactly at a zero-crossing; otherwise you’ll hear clicks or distortions at the transitions. Alternatively, you can fade them in and out by multiplying by a “ramp” function, which you can create by decreasing a float from 1 to 0 over some short length of time.

    Some C language tips:

    • You can replace n = n + 1 with ++n.

    • You can replace

     if (abs(temp) > MAXVAL) {
       return ERROR_CODE;
     }
     return temp;
    

    with

    return abs(temp) > MAXVAL ? ERROR_CODE : temp;
    

    • If you choose to keep the if, it’s a good idea to use braces even for single-line bodies, because you may someday add another line and forget about the braces. That’s a particularly hard bug to find because you’ll read the code as you expect it to be, and not as it actually is. Trust me on this. 🙂

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

Sidebar

Related Questions

I have a simple form for generating reports that I am adding a jqueryUI
I am having troubles generating a simple database form model. I am using: Doctrine
I'm using SimpleModal plugin ( http://www.ericmmartin.com/projects/simplemodal/ ) for generating a simple modal dialog. Now
I am generating a DataGrid from an Entity Model, which was very simple in
So I have this simple PHP loop that is generating html table data with
Have a simple one-off tasks which needs a progress bar. OpenSSL has a useful
I have a pair of simple classes generating a database in Code First in
I am generating a simple csv file using php. The file contains some user's
I am generating a PDF Document through XSL-FO. I have a simple xhtml structure
I have this html code generating by JavaScript function, in this simple case I

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.