Thursday, 28 November 2013

Twin Prime conjecture:Bounded gap from 70 million to just 600... and still counting !

Prime Number Generator, Arduino Version(Pic by Nick Gammon)

After my last post in this same subject, i had to again share this news !
Seems like we went from 70 million to just 600... and still counting !

Thursday, 21 November 2013

Father Christmas to bring new PIC32MZ

I am a bit short for words, but had to share !
I actually thought it would never be more than a rumour eh eh !
December come...

Microchip will soon release new PIC32MZ, with first members of the family expected starting from December , with :
- 200 MHz & 330 DMIPS !!!
- 2 MB Flash with Live Update
- 512 KB high speed SRAM

Friday, 15 November 2013

WAV file format and brief intro into its structure

WAV file format and brief intro into its structure

detail of the table below

*One of the options that came to mind when developing this drum synth, was also follow the sampler route ! It can be quicker, easier but wasn't really what i was aiming at. Still, i still think that it can be a nice add on to it, so i share some of the concepts in order to understand a canonical wave player ( SD Card for storage, of course). Here is a brief introduction...

The WAV file-type is a standard for storing audio data in chunks and sub-chunks using the RIFF (Resource Interchange File Format) format , therefore a subset of it. The most common type of WAV file in audio is PCM (Pulse Code Modulation) .
Most common WAV formats contains uncompressed audio in the linear pulse code modulation (LPCM) format.
Although we will be focusing on PCM and LPCM( Linear Pulse Code Modulation) here, there are several compression codecs available to use with .WAV file format; including µ-Law, PCM, ADPCM, Microsoft GSM 06.10, CELP, SBC, Truespeech and MPEG Layer-3.
Most common WAV formats contains uncompressed audio in the linear pulse code modulation (LPCM) format. The standard audio file format for CDs, for example, is LPCM-encoded, containing two channels of 44,100 samples per second, 16 bits per sample. Since LPCM uses an uncompressed storage method which keeps all the samples of an audio track, professional users or audio experts may use the WAV format for maximum audio quality.
Since the sampling-rate of a WAV file can vary from 1 Hz to 4.3 GHz, and with as many channels as 65535, .wav files have also been used for non-audio data.
WAV format is limited to files less than 4 GB, because of its use of a 32-bit unsigned integer to record the file size header (some programs limit the size to 2 GB). The W64 format was  created for its 64-bit header, that allows much longer recording times.

A RIFF file is a generic file container format for storing data in tagged chunks. It  starts out with a file header followed by a sequence of data chunks.
RIFF files that are used to store audio and video information are called AVI files. The RIFF AVI file format normally contains only a single AVI chunk; however, other types of chunks may also appear. An AVI reader should ignore all chunks it does not need or recognize that are stored within a RIFF AVI file.

A WAVE file is often just a RIFF file with a single "WAVE" chunk which consists of two sub-chunks - a "fmt " chunk specifying the data format && a "data" chunk containing the actual sample data.

There is even a few quirks like “Audio CDs do not use WAV as their sound format, using instead Red Book audio ( almost a standard requirement on masters to be pressed and released as well). The commonality is that both audio CDs and WAV files have the audio data encoded in PCM. WAV is a data file format for a computer to use that cannot be understood by CD players directly. To record WAV files to an Audio CD the file headers must be stripped and the remaining PCM data written directly to the disc as individual tracks with zero-padding added to match the CD's sector size. In order for a WAV file to be able to be burned to a CD, it should be in the 44100 Hz, 16-bit stereo format.”

0000  | RIFF (in plain ASCII text)
0004  | Length of the entire file as a 32-bit unsigned integer
0008  | WAVE (in plain ASCII text)
000C | fmt[] (fmt[] in ASCII text denotes subchunk)
0010  | subchunk1size as a 32-bit unsigned integer
0032  | audio format 1=PCM
0016  | # of channels 1=1 2=2 (you can figure that out right?!)
0018  | Sample Rate!
001C | Byte Rate
0020  | Block Align
0022  | Bits/Sample
0024  | data (in plain ASCII text start of data subchunk)
0028  | Subchunk2Size
002C | First Sample (Left if 0016 = 2)
002E  | Second Sample (Right if 0016 = 2)

52 49 46 46 90 56 00 00 57 41 56 45 66 6d 74 20 10 00 00 00 01 00 01 00 44 ac 00 00 44 ac 00 00 01 00 08 00 4c 49 53 54 42 00 00 00 49 4e 46 4f 49 4e 41 4d 16 00 00 00 44 72 75 6d 20 53 79 6e 74 68 20 66 6f 75 6e 64 61 74 69 6f 6e 00 49 41 52 54 0a 00 00 00 44 75 62 77 6f 72 6b 73 00 00 49 43 52 44 06 00 00 00 32 30 31 33 00 00 64 61 74 61 22 56 00 00 80 80 81 82 84 87 8a 8e 92 97 9c a1 a7 ae b4 bb c2 c8 cf d7 de e4 eb f1 f8 fd ff ff ff ff ff fe fb f8 f5 f2 ee e9 e4 df da d4 ce c7 c1 ba b3 ab a4 9c 95 8d 86 7e 76 6f 67 60 58 51 4a 44 3d 37 31 2b 26 21 1c 18 14 11 0e 0b 09 08 06 06 05 06 06 07 09 0b 0d 10 13 17 1b 20 24 29 2f 35 3b 41 48 4f 55 5d 64 6b 73 7a 82 89 91 98 a0 a7 ae b5 bc c3 c9 cf d5 db e0 e5 ea ee f2 f5 f8 fb fd ff ff ff ff ff ff ff ff fd fb f8 f5 f1 ed

So lets see what we have...
1- RIFF in ASCII characters, is 52 49 46 46 in HEXADECIMAL
2- 0x90 0x56 0x00 0x00 is b10010000 b01010110 b00000000 b00000000 BINARY, which is 36,950 in DECIMAL
Despite not relevant in this specific case remember, this is little endian oriented (check table above).
3- 0x57 0x41 0x56 0x45, which translates to WAVE, in ASCII characters

The WAVE format supports a number of different compression algorithms. The format tag entry in the fmt chunk indicates the type of compression used. A value of 1 indicates Pulse Code Modulation (PCM, the one we often used in embedded systems), which is a "straight," or uncompressed encoding of the samples. Values other than 1 indicate some form of compression..
The standard Format codes for waveform data are given below. The references usually give many more format codes for compressed data, a good fraction of which are now obsolete.

4- The data section  is easy, as 0x64 0x61 0x74 0x61 is "data" in ASCII.
To make a player using an SD Card, usually you can search straight away to the location on the file (; kinda thing), hence a lot of projects with smaller microcontrollers using 8 bit Unsigned PCM encoding, as that makes everything easier; Plus, its  lighter to implement on 8 Bit architectures.

Lets take a smaller example, in order to establish some concepts, making it easier to illustrate it, better .

Hex Dump

52 49 46 46 6e 00 00 00 57 41 56 45 66 6d 74 20 10 00 00 00 01 00 01 00 44 ac 00 00 44 ac 00
00 01 00 08 00 4c 49 53 54 3a 00 00 00 49 4e 46 4f 49 4e 41 4d 0e 00 00 00 54 65 73 74 20 44
75 62 77 6f 72 6b 73 00 49 41 52 54 0a 00 00 00 44 75 62 77 6f 72 6b 73 00 00 49 43 52 44 06
00 00 00 32 30 31 33 00 00 64 61 74 61 08 00 00 00 80 c3 db d4 be a6 94 89

ASCII equivalent

RIFFn...WAVEfmt ........D¬..D¬.
.....LIST:...INFOINAM....Test D

We can recognize the start of data easy, as HEXA - 64 61 74 61 is "data" in ASCII, as mentioned before..
After we have 8 samples size  specifically for this test (check test wave file listen and download links below).

The samples DECIMAL - 128 195 219 212 190 166 148 137 ( HEXA 80 c3 db d4 be a6 94 89 , 8 bit unsigned PCM) , gives us this , graphed ( using Gnumeric) :

And as seen with an audio editor !

* TEST FILES HERE and HERE ( listen) and (download) HERE and HERE
One is a basic tom sound synthetized and the other a handmade colection with 8 samples, for example purposes, also seen in the previous graphs

Wednesday, 13 November 2013

Some insight into compilers ...

I had to share this link, due to its substance and context !
Craziest Compiler Output due to Undefined Behaviour

A lot of examples and links to detailed examples, causes and workarounds !
Every now and then, compilers still play a few tricks on us, due to our habits in coding !

Tuesday, 12 November 2013

PLBP for chipKit DP32 { Proverbial (4) Led Blink Program }

Ok I just could not resist, and had to post the Proverbial Led blink Program adapted for the DP32, that has 4 LED's on the board ( Digital Pins 11 to 14).
Will scroll them from right to left, speed varying on the variable resistor on board( trim pot).

/* Proverbial (4) Led Blink Program
 Scrolls 4 light emitting diodes(LED), by turning on and off connected to 4 different digital  
 pins, using the delay() function and the current millis alike :) . 
 The circuit:
 * 4 LED's on board - pins D11, D12, D13 and D14 
 ** Variable resistor/trimpot on A2
 *** Version done for the chipKit board DP32 **

// constants won't change. Used here to 
// set pin numbers:
const int ledPin =  11;      // the number of the LED pin
const int ledPin1 =  12; 
const int ledPin2 =  13; 
const int ledPin3 =  14; 
int i;
// Variables will change:
int ledState = LOW;             // ledState used to set the LED
int ledState1 = LOW;
int ledState2 = LOW;
int ledState3 = LOW;
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
//long interval = 500;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(A2, INPUT);
  ledState = !ledState;  //Toggle the bits to start
  ledState1 = !ledState1;
  ledState2 = !ledState2;
  ledState3 = !ledState3;  

void loop()
  boolean b; 
  long interval = analogRead(A2); // interval at which to enter function.(milliseconds)
  interval= interval/3; //So it is not too slow
  digitalWrite(ledPin, ledState);
  digitalWrite(ledPin1, ledState1);
  digitalWrite(ledPin2, ledState2);
  digitalWrite(ledPin3, ledState3); 

  // check to see if it's time 
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {
    // save the last time  
    previousMillis = currentMillis;   
    int var;
    var=interval-(currentMillis - previousMillis);
    // conditional depending on increment accumulator
      switch (i) {

      case 0 :    
        ledState = !ledState;
        digitalWrite(ledPin, ledState); 

      case 1 :    
        ledState1 = !ledState1;
        digitalWrite(ledPin1, ledState1); 

      case 2 :    
        ledState2 = !ledState2;
        digitalWrite(ledPin2, ledState2); 

      case 3 :    
        ledState3 = !ledState3;
        digitalWrite(ledPin3, ledState3); 

      switch (i) {

      case 0 :    
        ledState = !ledState;
        digitalWrite(ledPin, ledState); 

      case 1 :    
        ledState1 = !ledState1;
        digitalWrite(ledPin1, ledState1); 

      case 2 :    
        ledState2 = !ledState2;
        digitalWrite(ledPin2, ledState2); 

      case 3 :    
        ledState3 = !ledState3;
        digitalWrite(ledPin3, ledState3); 

    i++; // Increment our accumulator
    // Keep the value within  the parameters needed for our switch case
    // in this case 5, so we can have 4 + 1 
    // (0, 1, 2 and 3) + 1 for default toggling of boolean variable b. (aesthetic reasons ?!)  

Monday, 11 November 2013

Scroogled ?!

If you haven't, start by watching this first video, and then see the similarities with the subjects and links below...

There are many, many more on all walks of life !!

Are you also being scroogled ?!
*mind the proper English"

Saturday, 9 November 2013

Cryptography from the vaults

Despite coming from the vaults of time, and some of the technologies mentioned being outdated, it still manages to be a good overall lecture on cryptography as a motivation for more hackers to get involved with it !

"HOPE took place on August 13-14, 1994 at Hotel Pennsylvania in New York City.
There have been quite a few articles in the national media recently about cryptography and privacy. Bob Stratton will attempt to provide an introduction to the terms and technology, how it affects the average citizen, and insights into the public policy debate currently raging in Washington and around the world. There will be a special emphasis on the relationship of cryptographic technology and emerging personal communications tools.

Also, a demonstration by Bernie S. and John Turbo of the AT&T 3600 Surety telephone device, the world's first Clipper Chip telephone encryption unit. Attendees will be invited to make telephone calls between two AT&T 3600's and a loudspeaker will enable all present to actually hear the encrypted audio. An actual Clipper Chip will be given away as a door prize with the opportunity to smash it with a hammer in front of all.

Also featuring Eric Hughes and Matt Blaze."

They also mention a classic book "The code breakers" as a good reading !

Friday, 8 November 2013

R/C Low Pass filter (passive) cheat sheet

Cutt Off freq= 1 / ( π R C )

C =  33 nF; R = 220 R (Ohms); Cut-off freq.= 21.92 KHz
C = 150 nF; R = 100 R (Ohms); Cut-off freq.= 10.61 KHz
C = 150 nF; R =    1 K (Ohms);  Cutoff freq  = 1.06 KHz
C =  2.2 uF; R = 100 R (Ohms); Cut-off freq.= 723.43 Hz
C =   10 uF; R = 100 R (Ohms); Cut-off freq.= 159.15 Hz
C =   22 uF; R =   68 R (Ohms); Cut-off freq.=  106.39 Hz

* I tried to use some of the most common values for the Resistors and Capacitors; keep in mind this drops at +- 20 dB per octave   ( 2 f ).

C =  220 nF; R =    1 K (Ohms); Cut-off freq.=723.43

Graphs and simulations done with Falstad online sim

Wednesday, 6 November 2013

Quick pictures update on ADSR

 Exp.attack at 0.4 and sustain level at 7/8 ( 20 Ms div)

A quick update to show that first try, worked as expected. Spent a few good hours in the process of starting to optimize the different building blocks id gathered, and make them interact. Tried to keep it as readable as possible and document the little quirks, and possible variations, references, etc in the implementation methods.
We have officially an ADSR with variable exponential attack, variable "decay-sustain ratio" ( allows both bypassing the sustain stage, and also looping/extending the sustain stage ), and variable duration/period... For now !

Next comes LFO's from all the wavetables available, simple filters, and start implementing simple convolution reverb. Lets see how far can i  "push the envelope" ( No pun intended).

Square wave and exp.attack at 0.5 ( .2 s Div )

Saturday, 2 November 2013

A DP32 chipKit from Dangerous prototypes... for free ?!? Yes Please !!

As some of you might have noticed, i love to dig into,  learn with, bash my head against, and ultimately discover new platforms. In my perspective it is essential to develop new and wide skills that ultimately will make one a better prepared and experienced programmer and engineer !
Recently i have been looking into the PIC32 as the most probable option to the Retro-inspired drum-synth i been developing for quite a while now, despite the fact i been using several chips along the way to develop ideas for the final version.

So, i took my chance and proposed an idea i been working in for a while, as part of a Dangerous Prototypes give-away with Microchip and Diligent in the form of a contest .
And guess what ?! I won !

Another winner is Ras B, and his idea for a CHipKit version of a Retro-Inspired Cynare Drum Synth scored him a chipKIT DP 32:
Id use the DP32 to make a CHipKit version of a Retro-Inspired Cynare Drum Synth( ADSR with variable exponential attack, LFO’s included) and some additional features ( Modulation related Like phaser, flanger and some basic reverb and delay- This through an additional memory chip to allow some leverage in the chips memory limitations) 
I am still working on implementing some of it, while a lot of it i have or am currently implementing it
* Funny thing was i was actually working on it when , during a break, i came across the news that i'd won !! :) .

I have already worked with the dip version of the PIC32 , even posted about it.
And i am not sure as yet, if all these features are all possible in a DIP version of it ( as it only runs at around 40 MHz, compared with th 80MHz of SMD versions).
But i vow to make a lighter version ( if need be) for the dip version, as i love it myself.
There are some limitations that might become an issue to take it as far as intend , due to the abstraction created by higher level languages as the "Arduino-based" used by the MPIDE. But, bit by bit (pass the pun) , i intend to present any limitations i might come across and reference them, so it helps anyone else !