Sunday, December 24, 2017

Volatile Variables and Restricted Pointer

A variable defined as volatile if the value of the variable may change outside the program.  For example, the variable value may be changed by hardware or by another program (multi-processing).  The compiler will reload the value from the memory every time before the variable is used.  This will disable some code optimization.

A pointer defined as restricted means the associated memory is only pointed by this pointer.  This allows the compiler to optimize code.  As there no other pointer points to the same memory area (pointer aliasing), the compiler may not save the value back to the memory immediately after it is changed.

Sunday, November 5, 2017

Scale

A note an octave higher than another is vibrating exactly 2 times of frequency of the lower note. The Just (meant correct, true) system divide an octave using notes which is simple faction of the base note.  For example, the fifth is 2/3 of the frequency of the base note.  The problem is that the Just system notes may not harmonized so well among them except with the base note.  For example, the tonic and the fifth frequency coincide every a few cyclerates and thus they sound harmonized.  But the 2nd (8/9) and 6th (3/5) coincide every 27 cycles and 40 cycles respectively and thus they do not sound harmonized.

The equal temperament system uses formula instead of fraction to divide the scale.  Specifically, ratio of the frequency for every 2 consecutive semitone are the same.  Both Just and ET system use 440 Hz for A.  Comparing with Just system, the note in ET is slightly out of tune with the tonic t still close enough to sound harmonized.  However, while not all chord in Just sound harmonized, all chord in ET sounds moderately harmonized.  This is the main difference for the 2 systems

Applying Chord

Typically, chord is chosen based on the rhythmically emphasized notes, which usually the first note  in each bar.  As melody is made up by a stream of notes usually rise or fall in small tone step, not every notes in the bar would fit to the chord chosen.  Choose the chord which included most of the notes in the bar.  When transiting from one chord to next, use inverted chord could "smooth" out the transition.  For example, to transit from C to F

C: C, E, G
F: F, A, C

Each note transverse a distance of 4 tones

If we use a inverted chord for F would reduce the movement for the tones between the 2 chords

C: C, E, G
F: A, C, F

Similar we can invert C to achieve similar effect

C: E, G, C
F: F, A, C

Saturday, October 28, 2017

Finding a Tune

How do we identify a tune (melody) from a music piece amidst  the sound made by many instrument that may be playing different notes at the same time?  Several of human survival skills helps us to group related items (tune) and identify pattern from chaos:

(1) Proximity - related item is likely to be close to each other.  Music usually uses notes that are close (1 or 2 steps up of down) in a melody.  Notes in a melody also has temporal proximity.  The notes flow one after another in succession

(2) Similarity - notes in a melody is usually carried by a same musical instrument like voice or piano

(3) Continuity - power to connecting the dots.  Following the note flow, one can "predict" the following notes that made up a melody, even if the melody is carried by multiple instruments successively.

(4) Common fate - melody and accompaniment flows in different note pitch level, different trend (one going up and the other going down) and different rhythm.


Sunday, October 22, 2017

Sensory Disonance

A note entering an ear causes the ear drum to vibrate.  The in turn stimulate a patch of hairs in the cochlea in the inner year.  Different frequency will stimulate a different cochlea patch.  When several notes is heard, multiple cochlea patches are stimulated.  Patches for 2 semitone may have overlapped area.  The brain would need to work herder to identify them.  This biologically caused us to sense the sound combination of 2 semitone as dissonant.  

Interestingly, this same condition may appear to tones that are greater than semitones in the lower frequency range.  In other words, chords in lower frequency range did not sound as smooth as the same chord in a higher frequency range.  That's why musician does not play chord with bass instruments.

Object Serialization

Serialization is to make an object persistent across session.  An object exists in memory of a program instance (e.g. Javascript).  Once the program terminated, memory is released and the object ceases to exist.  To preserve am object across different program instances, the object must be stored in an persistent media (disk).  The object memory layout is converted to string and kept on disk.

JSON2 library contains routine to serialize a JavaScript object (stringify) and to recreate the object in memory from a serialize object (parse).

JSON

In addition to using "new" keyword, JavaScript Object can also be created directly from literal values.  For example,

Obj1 = {“member1” : “this is member 1”, “member2” : “2”, member 3 : 3, member4 : Obj2};

Or using array notation:

Obj1 = [“this is member 1”, “member2”, 3, Obj2];

JSON is derived from the literal form of the Object in Javascript.  The possible JSON value can be a string, number, object, array, true, false and null.

HttpServlet

As most servlet is written to handle request delivered by http protocol, the HttpServlet extends the GenericServlet and provide methods specially created to handle http requests.

HttpServlet defines doGet and doPut methods which can be overridden by the real servlet to process the request.  doGet and doPut eventually returns to the Servlet service method.  There are more "do" methods for the other http request types such as "head" etc.

HttpServlet also defines 2 new objects, httpRequest and httpResponse to be used in place of the ServletRequest and ServletResponse objects.

JavaScirpt Objects


A JS object is basically an associative array containing a series of key-value pair.  To create a JS object, use the new keyword.  For example,

Var obj1 = new Object();
Obj1.member1 = “this is member 1”;
Obj1.member2 = 3

Another way to create members of an object is to use square bracket (array?) notation:

Obj1[“member1”] = “this is member 1”

The square bracket notation has an advantage that it can use character not allowed for variable name because the member name is specified as string.  For example, Obj1[“1”] works as single digit 1 is not allowed in variable name.  Obj1.1 will flag as error by JS.

Array can be generalized (viewed alternatively) as an object with numeric keys (0, 1, 2, ..).

Sunday, June 18, 2017

GenericServlet

To implement the Servlet interface, one needs to provide classes for all methods specified in the interface, even if the method is not used by the servlet.  To simplify coding, GenericServlet is an abstract class that implemented the Servlet inteface. GenericServlet supplies implementation for the method in the interface so that the real servlet class only needs to override those method it uses.

GenericServlet also saves the ServletConfig object passed by the servlet container to the init method in a persistent variable so the real servlet can access it later on.  GenericServlet adds another init method with no argument for the real servlet to override with its specific code.

GeneicServlet is implemented as abstract class as it expects the real servlet implementation override one of more methods before it is used.

Servlet Interface

The Servlet interface specified s contract between the servlet container and the servlet.  Servlet container calls the init method of the servlet one time during start up.  When a request comes in, servlet engine calls the service method of the servlet to process it.

Servlet engine will also pass in a ServletConfig object to the servlet application's init method.  ServletConfig contains initialization parameters. ServletConfig also provide a SrvletContext object. The ServleCcontext contains global data accessible by all classes in the servlet. In a distributed environment, each servlet instance can access the same ServletContext data.

Servlet also defines two objects - ServletRequest and ServletResponse that used by service method.  ServletRequest defines methods to retrieve parameters submitted in a request.  For example, "id" is the parameter which the method returns as string value.

http:::.../servlet?id=1

ServletResponse simplifies the returning of response to the client. It defines methods to let the application to write the response to a certain stream and let servlet container to handle the rest.

To implement a serlvet, one create a class implement the Servlet interface or extend a class that implemented the Servlet interface.

Monday, May 1, 2017

Function Pointer

Pointer to a function is declared as follows

void (*fn)()

Void is the return type
fn is the function name
() is the parameter types

For example

int (*abc)(int, int) is a function pointer for a function abc which accepts 2 integer arguments and returns an integer result

To use a function pointer

int sum(int a, int b)
{
    return a + b;
)

abc = sum;
z = abc(x, y);


Pointer Arithmatic.

Adding an integer, say i, to a pointer is actually advancing the pointer to i unit of the underlying data type the pointer points to.  For example,

int *p;
p = &j
p = p + 3;  /* p = p + 3*4 as integer is 4 bytes in length */

Likewise, for subtraction, the pointer is deducted by i units of the underlying data type.

When subtracting 2 pointers, the result is the number of units of the underlying data type.  A minus result indicate the first pointer is less than the second pointer in the expression.

Constant and Ponter

const int *p is a pointer to a constant integer.  We cannot change the variable that the pointer is pointing to but we can change the pointer to point to another variable.  However, even if the new variable it points to is not a constant, C runtime will not allow us to change it as it thinks the pointer is pointing to a constant as declared.

For example

const int *p;
const int i = 100
int j = 20

p = &i;  /* ok */
p = &j;  /* ok */
*p = 300;  /* error */

int *const p is a constant point pointing to an integer variable.  The pointer value cannot be change but we can change the variable it points to.

*p = 100;  /* ok */
p = &j;  /* error */

const int *const p is a constant pointer pointing to a constant variable. When the pointer is declard, it must be initialized.

const int *const p = &i

const int *const *p is a pointer to a constant pointer to a constant integer variable

Reading C Pointer Declaration

Reading the pointer declaration backward is easier.

For example, int * p

p = p is a variable
* = p is a pointer variable
int = p is a pointer variable points to an integer

const int * p = a pointer to an constant intege