element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
STEM Projects
  • Learn
  • Learning Center
  • STEM Academy
  • STEM Projects
  • More
  • Cancel
STEM Projects
Blog Shedule & Homework Robot - Sound System's Software (part #3)
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join STEM Projects to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: yuritikhonov
  • Date Created: 21 May 2015 7:07 AM Date Created
  • Views 338 views
  • Likes 4 likes
  • Comments 3 comments
  • schedule_bot
  • teachers_pet
  • robot
  • matlab
  • teachers pet robotics design challenge
  • arduino
Related
Recommended

Shedule & Homework Robot - Sound System's Software (part #3)

yuritikhonov
yuritikhonov
21 May 2015

Good day comrades!

 

Today we talk about the software part of our melody generator. To begin, I want to explain why do we need it, because the Arduino has a built-in PWM, through which a lot of people do stuff like that. Let's see:

  • wav -  the most simple and clear audio format having a sampling frequency of 44,100 Hz, each signal is coded 16 bits. Uncompressed format, thereby obtain 700 kbit / s;
  • mp3 - very popular format, but with a lot of difficulties, bit rate is usually 320 kbit / s;
  • yaf - "Yuri's Audio Format" has a bit rate of 0.8 kbit / s and is compatible only with the described sound card.

 

Given the relatively low productivity of Arduino everything falls into place: the work of classic audio formats requires a full download of MCU, whereas, yaf offloads MCU providing simultaneous playback of sound and movement of the robot.

 

Now, when it became clear why I made my own analog sound card, let's see how to work with it. Let's start with the simplest example - an electronic piano. By the way!

You know it? Yes Yes! It's my good old stand, known to readers of my series of articles about tricopter.

image

 

So, the idea is this: connecting some contacts of sound cards with GND we achieve playing back a note. Since the last time I‘ve improved the circuit and now I‘ve managed to avoid stray harmonics 3.3 kHz (if you're interested - I can write about this in the next article). To play sound I use Arduino Duemilanove, digital lines which are connected to the inputs of the sound card, it uses the following program:

char pins[] = {7,6,5,4,3,2,2,2}; // pin2 - reserved

void set_bit(char b) // set a single bit to GND
{
  if (b==-1)
  for (char i=0; i<8; i++)
  {
     pinMode(pins[i],OUTPUT);
     digitalWrite(pins[i],LOW);
  }

  else
  for (char i=0; i<8; i++)
  {
    if (i != b)
      pinMode(pins[i],INPUT); // high impedance
    else
    {
      pinMode(pins[i],OUTPUT); // grounded (0V)
      digitalWrite(pins[i],LOW);
    }
  }
}

void simple_loop(void) // piano
{
  static char i = 0;
  delay(1000);
  set_bit(++i);
  if (i > 4) i = 0;
}

void setup(void)
{
  for (char i=0; i<8; i++) // all pins = GND
  {
    pinMode(pins[i],OUTPUT);
    digitalWrite(pins[i],LOW);
  }
}

void loop(void)
{
  simple_loop();    // piano
  //sound_loop(10); // music
}

 

Let's see how it works:

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

But then the question arises, how to play music? It's very simple - if you change the tone in every 10ms, you get a monophonic melody. The melody is stored in the array sound_data and to play it, I use the procedure sound_loop:

 

void sound_loop(int del) // music
{
  static int i = 0;
  delay(20);
  set_bit(sound_data[++i]-1);
  i++;

  if (i >= 992) // length of array
  {
    i = 0;
  }
}

 

But where can we take the melody? My favorite Matlab will help us with it! The problem can be divided into 5 steps:

 

1. We need a melody. For the experiment, I chose the Imperial March from Star Wars. The file is called imperial2.wav

 

2. matlab has a function audioread(), Let’s use it to read the file:

close all
clear all
clc
[x,fs] = audioread('imperial2.wav');

 

3. To select the notes we use the fast Fourier transform. An important point: we need only one harmonic, so let’s find the one whose amplitude is maximal:

% ------------------------------------------------
% fft codding
% ------------------------------------------------

T = 0.01;

t = length(x) / fs;
Ts = t / T;
x = x.';
ff = zeros(int32(Ts),1);
d = length(x) / Ts;
amp = zeros(round(Ts),1);

for l = 1:Ts
    start = (l-1)*d + 1;
    stop = l*d;

    now = x(start:stop);

    L = length(now);
    NFFT = 2^nextpow2(L); % Next power of 2 from length of y
    Y = fft(now,NFFT)/L;

    f = 2*abs(Y(1:NFFT/2+1));
    s = length(f);
    r = find(f == max(f),1);

    ff(l) = r;
    amp(l) = max(f);
end

amp = amp ./ max(amp);
tm = max (fs/2*linspace(0,1,NFFT/2+1));

 

4. After the preceding paragraph, we have an array that stores the codes of notes (and even amplitude, but more about that another time), forming the composition, let's try to restore the melody:

% ------------------------------------------------
% uncodding
% ------------------------------------------------

time = linspace(0,t,length(x));

for l = 1:Ts
    strt = int32 ( (l-1)*d + 1 );
    stp =  int32 ( l*d );
    w = 2*pi* ff(l) * tm/s;
    time(strt:stp) = sin(w.*time(strt:stp));%.*amp(l);
end

sound(time/2.0,fs)

 

5. And now, instead of thousand words, I suggest you to see one short video about how the magic is created:

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

Well, today that is probably over. Now, briefly about my future plans: the fact is that I have not got yet the components to build the robot, but I hope that they can come to me next week. So the plan is as follows:

  • If I get components this week - the next week there will be unboxing;
  • If I do not get components – I will wait until June 1;
  • if by June 1 will not have got them - I write the continuation of  series of articles about the sound-card

 

In any case, do not switch – more interesting is ahead! See you soon!

  • Sign in to reply

Top Comments

  • Robert Peter Oakes
    Robert Peter Oakes over 10 years ago +1
    I like the nice secure way your keeping the hardware laid out. Easy to wire up and easy to demo I'm following the project, interested to see where it leads
  • DAB
    DAB over 10 years ago +1
    Nice post. Are you using the version of Matlab sold with the Arduino or some other version? DAB
  • yuritikhonov
    yuritikhonov over 10 years ago in reply to DAB +1
    Hi DAB! I use Matlab 2015a that provided free of charge to all finalists of this RoadTest.
  • yuritikhonov
    yuritikhonov over 10 years ago in reply to DAB

    Hi DAB!

    I use Matlab 2015a that provided free of charge to all finalists of this RoadTest.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB over 10 years ago

    Nice post.

     

    Are you using the version of Matlab sold with the Arduino or some other version?

     

    DAB

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Robert Peter Oakes
    Robert Peter Oakes over 10 years ago

    I like the nice secure way your keeping the hardware laid out. Easy to wire up and easy to demo

     

    I'm following the project, interested to see where it leads

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube