Friday, July 10, 2015

Git most often used command

1. add untracked files: git add *
2. show status in a git folder: git status
3. remove file A in git folder: git rm A
4. rename A to B: git mv A B
5. commit with comment: git commit -m 'message'
6. push: git push

Monday, July 6, 2015

git ssh low speed push solution

git push uses ssh, if you meet low speed push problem, try do this:
  1. $ git remote -v
  2. if you see your git folder begins with http://, please use
    git remote set-url origin commande to change it to ssh url.
  3. $ sudo sysctl -w net.ipv4.tcp_sack=0
  4. then git push

Friday, May 22, 2015

Set JRE environment for smartgit in Linux

1. download smartgit linux version, try run ./ in the its bin folder
2. if it complains JRE version problem, run sudo apt-get install openjdk-7-jre
3. locate to your openjdk-7-jre path, see here 
4. gedit ~/.smartgit/smartgit.vmoptions, add line: jre = path-to-your-openjdk
5. redo step 1

Saturday, April 18, 2015

Youtube data client library iOS, oauth2, HTTPfetcher

1. Compiling google api

This page shows the method to compile the google api to a project. I used the second way: compiling the source files directly into iOS project.

When I downloaded its source file, the oauth2 and HTTPfetcher are not there. We need to download them separately from here and here. Then just follow the procedure in the first page to finish compiling.

2. Youtube search

To search the data on youtube, firstly, import three headers:

#import "GTLServiceYouTube.h"
#import "GTLQueryYouTube.h"
#import "GTLYouTubeSearchListResponse.h"

Then for example, we search here for the youtube id by giving a searching word string:

GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init];
service.APIKey = yourAPIKey;
GTLQueryYouTube *query = [GTLQueryYouTube queryForSearchListWithPart:@"id"];
query.type = @"video";
query.q = aSearchingString;
query.maxResults = aInt;

The code is very clear itself. We declare a youtube service instance, give it the APIKey, declare a query instance. The meaning of type, q and maxResults can be found here. Finally we execute this query which will give us a ticket. In the completionHandler, we receive the returned object.

GTLServiceTicket *ticketSend = [service executeQuery:query
                                   completionHandler:^(GTLServiceTicket *ticketReceive, id object, NSError *error)
        if (!error) {
            GTLYouTubeSearchListResponse *items = object;
        } else {

Wednesday, March 18, 2015

Install Scipy through pip install in mac os x

When canopy visual environment has been installed, you might find your can't install Scipy to /usr/local/lib/python2.7/ anymore, or you might encounter pip "DistributionNotFound" Error. The solution is to reinstall python like this, using the soft way. Then pip install scipy.

Python package "intonation" needs Scipy.

Plot histogram of a recording object might cause float error in mac os backend.  Try to use:

import matplotlib

before import anything from pylab.

Tuesday, March 3, 2015

Using AcoustID in IOS

AcoustID is an audio finger print project where Chromaprint allows you to calculate finger print from audio sources, and its web service allows you to search or to submit audio information in its database by Chromaprint.

Chormaprint is the core component of AcoustID project, it is written in C and can be compiled by cmake. However to use it in IOS, one needs a toolchain file.

1. Compile Chromaprint for IOS
1) You need to download the toolchain file here:

2) unzip the file "iOS.cmake" to Chromaprint source files' root folder. Open it, then change IOS_PLATFORM according to what you need. The default building architecture for "OS" is armv7 and arm64, this is sufficient for nowadays IOS devises.

3) build source files into a static library

1. mkdir build.ios 
2. cd build.ios 
3. cmake -DCMAKE_TOOLCHAIN_FILE=../../../iOS.cmake -GXcode ..
4. open Xcode project, compile chromaprint_p target for "iOS Device"

4) the static library will appear in the folder src/Debug-iphoneos/libchromaprint_p.a

2. Calculate finger print
Chromaprint only receives LinearPCM as the input audio format, so one should do the conversion if his original audio file format is not LinearPCM. The conversion method can be consulted here.

Once we get the URL of the audio file, we can open it by function AudioFileOpenURL, and save the fileID to outAudioFile.

The procedure of the finger print calculation is:

1. define a finger print calculation algorithm, here we use the default one: 

2. declare a finger print context and start calculation:
ChromaprintContext chromaprintCtx = chromaprint_new(algo);
chromaprint_start(chromaprintCtx, samplerate, channels);

3. read data packet, feed audio buffer outBuffer into this context, this could be done in a while loop:
AudioFileReadPackets(outAudioFile, false, &outNumBytes, NULL, inStartingPacket,&ioNumPackets, outBuffer);
chromaprint_feed(chromaprintCtx, outBuffer, ioNumPackets * framesPerPackets * 2)
where ioNumPackets is the packet number of the buffer,  inStartingPacket is the starting packet of the buffer, framesPerPackets = 1 for linearPCM audio format. Notice that AcoustID web service needs only 2 minutes audio finger print for database searching.

4. finish the finger print calculation and finally get the finger print:
char *fingerPrint;
chromaprint_get_fingerprint(chromaprintCtx, &fingerPrint)

Now we have the finger print, and we can use it with AcoustID API to search its database.

Friday, January 23, 2015

AB Jukebox FAQ English

1. A simple tutorial

1) Download music information at first launch

2) Manually start/stop download in Settings.

3) You can download single's song's information in Library.

4) Enable/disable Download when launching in Settings.

5) Choose horizontal/vertical features (mood, timbre, rhythm)

6) 10 features available

7) Choose songs by drawing

 8) Play!

9) You can watch this simple tutorial in app Settings.

2. Browse song information in Wikip├ędia

1) Tap a song point on feature pad, then tap its title.

2) Swipe left a song cell in Playlist.

3) Swipe left a song cell in Library.

4) You can search Wikip├ędia by song title, artist name or album name.

3. What's the two points on the right of a song in Library?

The upper point is an indicator of MusicBrainz (, the lower point is an indicator of AcousticBrainz ( 

The blue color means this song's information has been found in the database. The gray color means its information has not been found in the database.

4. Why I can't find some songs (or all of them) on the feature pad?

1) One possible reason is your unfound music files are not tagged properly. AcousticBrainz Jukebox queries song information from database by matching its song title, artist name and album title (optional).

You can download music tagging software Picard, and tag your unfound music files. A tutorial of tagging music file can be found here.

2) If you still can't find these songs on pad, it's probably that the information of these music don't exist in database. You can use this software to calculate the information and then upload it to the database.

Tuesday, January 13, 2015

Hide UITableView index of empty sections iOS

In iOS, we use "UILocalizedIndexedCollation" to partition music library song objects into sections to display them in table view. The "sectionTitles" array of "currentCollation" instance contains all letters in alphabet from A to Z if our system preferred language is English.

UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
NSArray *sectionTitles = [collation sectionTitles];

Sometimes, due to lack of song titles which begin with certain letters, we have empty sections. For the sake of redundancy, we don't want display their titles and indexes in table view.

To hide them, we could declare a NSMutableArray to store those sections which are not empty:

// declare this in @interface
@property (nonatomic, strong) NSMutableArray *sectionIndexTitles;

// put this snippet in objects partition function
int i = 0;
for (NSMutableArray *section in unsortedSections) {
    if ([section count] != 0) {
        [sections addObject:[collation sortedArrayFromArray:section collationStringSelector:selector]];
        [self.sectionIndexTitles addObject:[[collation sectionIndexTitles] objectAtIndex:i]];

Finally we set up the table view delegate methods by using our "sectionIndexTitles":

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.sectionIndexTitles count];

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [self.sectionIndexTitles objectAtIndex:section];

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.sectionIndexTitles;

// touching the index jump to corresponding section
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    return [self.sectionIndexTitles indexOfObject:title];

Read this article to know how to configure UITableView by UILocalizedIndexedCollation.

Background download iOS

1. Setup background download session 

To setup a background download session in iOS is quite straightforward. Firstly setup a configuration, and use this configuration to declare a NSURLSession.

NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfiguration:identifier];
_downloadSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];

Then we declare a NSURLSessionDownloadTask, and start this task by calling resume:
NSURLSessionDownloadTask *task = [fh.downloadSession downloadTaskWithURL:aNSURL];
[task resume];

After that, one should also add four delegate methods which are:
- (void)URLSession:(NSURLSession *)session
      downloadTask:(NSURLSessionDownloadTask *)downloadTask
- (void)URLSession:(NSURLSession *)session
      downloadTask:(NSURLSessionDownloadTask *)downloadTask
- (void)URLSession:(NSURLSession *)session
      downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location
- (void)URLSession:(NSURLSession *)session
              task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error

One could collect the download results in the third method "didFinishDownloadingToURL" by using:
NSData *aData = [NSData dataWithContentsOfURL:location];

Notice that the fourth method "didCompleteWithError" will always be called. If the task is finished without error, it returns nil. Otherwise, it returns some error code for us to catch the error.

The task started in foreground will continue its downloading after entering to background.

2. Batch background download data from MusicBrainz 

MusicBrainz is a music meta-information database, one can find and download these information by searching song title, artist name, or album title, etc. It offers a public API for developers to query its database.

But there is a query/request rate limit which is about 1 request/second. That is to say if we launch, say , 100 request tasks at the same time, the server will reject them all. To achieve a successful query without being rejected, I schedule the tasks in a queue by "dispatch_after":

for (MPMediaItem *item in songLibrary) {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // start background music meta-data download task here.

The problem arises when we enter to background, even our download task support background download mode, but our dispatch_after block will stop running. To deal with this problem, I manage to ask for more running time after the app entering to background:

- (void)applicationDidEnterBackground:(UIApplication *)application {
    _backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
    [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
    _backgroundTask = UIBackgroundTaskInvalid;

The method "beginBackgroundTaskWithExpirationHandler" will be called when our _backgroundTask remaining time expired. In my iPhone 4, the system leaves 178 seconds for background task executing.

This method is far from good and needs to be improved.