Saturday, March 3, 2018

JDBC Connection

The early version of JDBC provided a Driver class with a GetConnection method for application.  Underneath Driver uses Java Socket class to get a connection to the database.  Later, DriverManager class provide the connection using an url.  Using a generic interface saved the application the need to know which specific driver to use.  The connection returned is a physical connection (a socket) which is adequate for a 2-tier client-server architecture as each client will create and use its own connection and seldom sharable.

JDBC v2.0 uses DataSource to support Java Transaction API and a 3-tier architecture.  An intermediate layer (application server) sits between client and the database.  DataSource returns a logical connection from a connection pool which improves performance by avoiding set up time for every JDBC connection request. 

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).