Creating arrays from waves

Discussion in 'Software' started by toni_aguirre, May 2, 2015.

  1. toni_aguirre

    toni_aguirre Newbie

    Joined:
    Jul 6, 2014
    Messages:
    24
    Likes Received:
    1
    I bought an Arduino and with very simple coding and an R-2R 8-bit resistor ladder, I managed to play a sine waveform. I made a symple array of values of a sine wave from 0 to 255 with python and imported to my project. Does anyone know a program with which I could obtain an array of the voltages of a given waveform for a given sample rate? for example: a square would be [0,0,0,0,255,255,255,255] or a saw [1,2,3,4,5,...,255] Maybe then I could upload the code so you could make an extremely cheap 8-bit waveform-synthesis synthesizer (my arduino costed me 3 $, but I can not get a 44100 hz sample rate, just a shitty 500 Hz; hopefully with a better version you could )
    Thanks!
     
  2.  
  3. fiction

    fiction Audiosexual

    Joined:
    Jun 21, 2011
    Messages:
    1,914
    Likes Received:
    701
    Cool! Great to see some tech hackers here :wink:

    The simplest answer to your question is using a wave editor that can export to RAW waveform data.
    I can highly recommend Wavosaur for Windows!
    It's not only a great free and compact wave editor, but it also contains a basic synthesizer that helps you creating Sine/Saw/Triangle/Square/Noise etc. waveforms and export them to raw 8bit format.
    You would then simply read the raw file into a python array or your Arduino project and ... voilá, done!

    If you cannot go beyond 500Hz it's most likely because of a lot of overhead in your programming.
    The Arduino can definitely do more.
    Directly programming in assembler would be the best bet of course, but maybe you can mix some simple assembler code that e.g. reads a cycle of, say, 1024 samples in a loop, and the high level language for the Arduino controls the content of the 1024 bytes buffer and the readout speed so you can play different pitches. You could even create a wavetable with different waveforms and select from them, given you have enough memory left...
     
  4. toni_aguirre

    toni_aguirre Newbie

    Joined:
    Jul 6, 2014
    Messages:
    24
    Likes Received:
    1
    I don't understand what you're saying. This is the code I made btw
    int i=0;
    int Val[] = {128 , 132 , 136 , 139 , 143 , 147 , 151 , 155 , 159 , 163 , 167 , 171 , 174 , 178 , 182 , 185 , 189 , 192 , 196 , 199 , 202 , 206 , 209 , 212 , 215 , 218 , 220 , 223 , 226 , 228 , 231 , 233 , 235 , 237 , 239 , 241 , 243 , 245 , 246 , 247 , 249 , 250 , 251 , 252 , 253 , 253 , 254 , 254 , 255 , 255 , 255 , 255 , 255 , 254 , 254 , 253 , 253 , 252 , 251 , 250 , 249 , 247 , 246 , 245 , 243 , 241 , 239 , 237 , 235 , 233 , 231 , 228 , 226 , 223 , 220 , 218 , 215 , 212 , 209 , 206 , 202 , 199 , 196 , 192 , 189 , 185 , 182 , 178 , 174 , 171 , 167 , 163 , 159 , 155 , 151 , 147 , 143 , 139 , 136 , 132 , 127 , 123 , 119 , 116 , 112 , 108 , 104 , 100 , 96 , 92 , 88 , 84 , 81 , 77 , 73 , 70 , 66 , 63 , 59 , 56 , 53 , 49 , 46 , 43 , 40 , 37 , 35 , 32 , 29 , 27 , 24 , 22 , 20 , 18 , 16 , 14 , 12 , 10 , 9 , 8 , 6 , 5 , 4 , 3 , 2 , 2 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 8 , 9 , 10 , 12 , 14 , 16 , 18 , 20 , 22 , 24 , 27 , 29 , 32 , 35 , 37 , 40 , 43 , 46 , 49 , 53 , 56 , 59 , 63 , 66 , 70 , 73 , 77 , 81 , 84 , 88 , 92 , 96 , 100 , 104 , 108 , 112 , 116 , 119 , 123};
    int v=0;
    void setup() {
    pinMode(2, OUTPUT);
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(6, OUTPUT);
    pinMode(7, OUTPUT);
    pinMode(8, OUTPUT);
    pinMode(9, OUTPUT);
    }

    void loop() {
    int R2=Val & 1;
    int R3=Val & 2;
    int R4=Val & 4;
    int R5=Val & 8;
    int R6=Val & 16;
    int R7=Val & 32;
    int R8=Val & 64;
    int R9=Val & 128;
    digitalWrite(2,R2);
    digitalWrite(3,R3);
    digitalWrite(4,R4);
    digitalWrite(5,R5);
    digitalWrite(6,R6);
    digitalWrite(7,R7);
    digitalWrite(8,R8);
    digitalWrite(9,R9);

    i=i+1;
    if (i==199)
    {
    i=0;
    }
    delayMicroseconds(1000);
    Is there a way to read the array faster?
    And thanks, I downloaded wavosaur and it is just what I needed. If anyone is interested, I exported as txt which converted the audio in [-1,1] array.
     
  5. fiction

    fiction Audiosexual

    Joined:
    Jun 21, 2011
    Messages:
    1,914
    Likes Received:
    701
    Well, even if you don't want to code in assembly language, there might be some room for optimization.
    First, depending on which CPU your Arduino has, can't you use a PORT instead of its individual pins?
    You won't be able to read the array faster, but you'd be able to write the DAC values faster, without the need to do any bitwise logic and bitwise pin writes.

    Something like:
    int i=0;
    int Val[] = {128 , 132 , 136 , 139 , 143 , 147 , 151 , 155 , 159 , 163 , 167 , 171 , 174 , 178 , 182 , 185 , 189 , 192 , 196 , 199 , 202 , 206 , 209 , 212 , 215 , 218 , 220 , 223 , 226 , 228 , 231 , 233 , 235 , 237 , 239 , 241 , 243 , 245 , 246 , 247 , 249 , 250 , 251 , 252 , 253 , 253 , 254 , 254 , 255 , 255 , 255 , 255 , 255 , 254 , 254 , 253 , 253 , 252 , 251 , 250 , 249 , 247 , 246 , 245 , 243 , 241 , 239 , 237 , 235 , 233 , 231 , 228 , 226 , 223 , 220 , 218 , 215 , 212 , 209 , 206 , 202 , 199 , 196 , 192 , 189 , 185 , 182 , 178 , 174 , 171 , 167 , 163 , 159 , 155 , 151 , 147 , 143 , 139 , 136 , 132 , 127 , 123 , 119 , 116 , 112 , 108 , 104 , 100 , 96 , 92 , 88 , 84 , 81 , 77 , 73 , 70 , 66 , 63 , 59 , 56 , 53 , 49 , 46 , 43 , 40 , 37 , 35 , 32 , 29 , 27 , 24 , 22 , 20 , 18 , 16 , 14 , 12 , 10 , 9 , 8 , 6 , 5 , 4 , 3 , 2 , 2 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 8 , 9 , 10 , 12 , 14 , 16 , 18 , 20 , 22 , 24 , 27 , 29 , 32 , 35 , 37 , 40 , 43 , 46 , 49 , 53 , 56 , 59 , 63 , 66 , 70 , 73 , 77 , 81 , 84 , 88 , 92 , 96 , 100 , 104 , 108 , 112 , 116 , 119 , 123};
    int v=0;
    void setup() {
    //
    // Prepare PORTD, whatever is required on your Arduino
    // (PORTD is just an example, could be a different port in your case)
    //
    }

    void loop() {
    PORTD = Val
    i=i+1;
    if (i==199)
    {
    i=0;
    }

    delayMicroseconds(1000); <== Why the heck are you doing this?

    If you want to play a sinewave, there is no reason for waiting between cycles.

    If the PORT mode of operation is still too slow (I don't think so ;-), you might have to get into assembly programming to get some more speed.
    It's not *that* hard, and there are many many good examples on-line, also on the Atmel home page.
    Not that current C compilers for the ATMEL weren't very efficient already, but in assembly language you can reliably count CPU clock cycles and that helps especially when you get close to the limits of your hardware platform and you want to get the best possible speed and/or quality out of it.
     
Loading...
Similar Threads - Creating arrays waves Forum Date
This may be a stupid question (regarding recreating music) Lounge Nov 1, 2024
Creating a Live Jam with Pyramid, MC-202, SE-02, Blofeld Our Music Oct 3, 2024
Kontakt 7 creating two instances of a library Kontakt Aug 19, 2024
Recreating Antimatter Sound how to make "that" sound Aug 4, 2024
Currently creating a reverb, and I need some opinions Working with Sound Apr 17, 2024
Loading...