Sunday, May 31, 2015

Changing the mouse wheel behavior for Kodi/XBMC

Kodi (A.K.A. XBMC) is a wonderful media center software that can do pretty much everything about the home entertainment and more.   I am using a wireless mouse to control a laptop connected to TV.  One annoying thing is that the mouse wheel is linked to forward/backward in the video playback.  This leads to some sudden jumps in playback when I accidentally hit the mouse (wheel).   Meanwhile, there is no mouse action that can controls the volume up/down easily.  I have to either uses the keybaord (i.e. stand up and step forward) or use the TV/receiver control (i.e. fumble around).  It finally get to the point that I have to find a solution.  After some search and experiments, particularly thanks to the excellent explanation on the wiki (http://kodi.wiki/view/keymap) I was able to change the mouse wheel to control volume.  Here is what I did:

<?xml version="1.0" encoding="UTF-8"?>
<keymap>
   <fullscreenvideo>
 <mouse>
  <wheelup>VolumeUp</wheelup>
  <wheeldown>VolumeDown</wheeldown>
 </mouse>
  </fullscreenvideo>
  
  <!--Change the video control -->
   <videoosd>
 <mouse>
  <wheelup>VolumeUp</wheelup>
  <wheeldown>VolumeDown</wheeldown>
 </mouse>
  </videoosd>
</keymap>

There are several posts talking about the <fullscreenvideo> window (http://kodi.wiki/view/keymap#Windows), which controls the behavior when there is no menu.  But often any movement of the mouse first activates the <videoosd> window, and we have to change the wheelup/down in that window as well. Otherwise, you still get forward/backward bahavior.  Hopefully this can help you as well.

Saturday, April 18, 2015

A Talk about Financial Engineering and MBS

I was giving a quick talk about the Financial Engineering and MBS.  Here is the link.  Hopefully you might find it intereseting.


link to the slide

Wednesday, July 16, 2014

Don't return in a closure for each() in Groovy

I am a Java coder.  All these years I heard so many buzz about functional language, but I never took the dive.  Well, I got some tastes recently. 

We recently switched to use Gradle to manage the project.  I was banning my head for a couple hours yesterday for an odd problem.


boolean isStartedWithList(String str){
  def list=['pre','post']
  list.each {
    if (str.startsWith(it)){
       println 'hit true'
       return true
    }
   }
  println 'hit default false'
  return false
  }  
If you run isStartedWithList('prehistorical'), you will see
hit true
hit default false
The returned value is false. This is because the (first and the second) return only gets you out of the closure, not the method. There are two ways to fix it: to switch back to the traditional for loop, or not to rely on the early exit from the return call.
boolean isStartedWithList(String str){
  def list=['pre','post']
  for (it:list) {
    if (str.startsWith(it)){
       println 'hit true'
       return true
    }
  println 'hit false'
  return false
  }  
boolean isStartedWithList(String str){
  def list=['pre','post']
  result=false;
  list.each {
    result=result||str.startsWtih(it)
   }
  return result
  }  
Both will get you the expected answer. Lessons are learned!

Edit 7/18/2014:

I just learned a even better one using the groovy collection method any
boolean isStartedWithList(String str){
  return ['pre','post'].any{str.startsWith(it)}
}

Sunday, June 3, 2012

A combiner works in a reducer as well as a mapper

In Hadoop, a combiner is usually introduced as a way to collapse the output record of maps in the end of a map task and therefore reduce the network traffic and alleviate the load of reducers. A less known function of a combiner that I recently discovered is that  it also works in the reduce phase. (CDH3u3, 0.20.2).  A reducer task, more specifically, its shuffle functions,  usually kicks off before all maps finish.  Once it collects enough records from all maps tasks (controlled by "mapred.inmem.merge.threshold" property) through network, records will be merged and a combiner will be called upon those records if one is specified in the job conf.  This is a very useful feature in the case where there are too many map tasks for the system to start them simultaneously.  Map outputs are collapsed by the combiner in the reducer task during the time, and final reduce run will not need to work no more records than "mapred.inmem.merge.threshold".  Many commercial or third-part versions of Hadoop actually specifically advertise some function similar as "proactive execution" or so.  It is great to know that the plain vanilla Hadoop already has something like this.

Saturday, November 12, 2011

Using a csv file as a metadata database in Greenstone (2.84)

Alternative to entering metadate manually in GLI in Greenstone (currently 2.84).  One can create a csv file with all the metadatas and import it into greenstone using gli.  This is very useful for at least two cases: 
  1. You can get the metadata from somewhere else in csv format.
  2. You want to experiment on a collection.  And you manage metadata in a separate csv file and  do not need to reenter all the metadatas after you screw up the collection. 

Greenstone has a metadatacsv plugin and unfortunately the author cannot figure out how to use it.  Alternatively, Greenstone has a explode metadata mechanism which is very useful.   Let us detail it. 

Let us first assume we have three files in Greenstone:

a.txt, b.txt, c.html


Filename,dc.Title,dc.Creator,Description,Contributor
a.txt, a, GZ, first test, GJZ
b.txt, b, GZ, second test, GJZ
c.html, c, GZ, third one, GJZ

A couple of point to notice for this file:
  • The first line is the label
  • The name of the label must exactly match the your metadata name (including the case, title will be mapped into "dc.title", instead of the standard "dc.Title")
  • Later we will choose the default metadata set, the non-qualified label will be mapped into this default set (Dublin Core in this case)
  • One can use a simple text editor do it. 
  • Google Doc is also an excellent tool with the  built-in collaborating function. One can create a spreadsheet first, fill it with your team mates, then "download as" a csv file. 

Here is the procedure in GLI
  1. (optional) create a new collection
  2. In the "Gather" tab, add all three files (a.txt, b.txt, c.html)
  3. In the "Gather" tab, add meta.csv (a window will pops up asking about plugin to use, choose either one and this does not matter much)
  4. In the "Enrich" tab, right-click "meta.csv", choose "explode metadata database", a new window will pop up (it shows as CSVPlugin, it is fine)
  5. choose "metadata_set" accordingly (Dublin Core in the example), this will be your default set, all your non-qualified metadata will be mapped into this (in our example, "Description" is mapped as "dc.Description")
  6. tick "document_field", enter the label of the file name column ("Filename" in the example)
  7. click "explode"
  8. In the Enrich tab, you will notice that all three files becomes the sub-levels of meta. And the metadata field is populated.  You can enter more
  9. Build the collection, tweak the display, do the rest 


Adding metadata with a new metadata scheme


Sometimes one has some special metadata fields that do not fit any existing schema.  One might want to create a new one from scratch or by modifying the existing one.  This can be done in Greenstone GEMS. Or one can do the same in  GLI "Manage Metadatasets..." in "Enrich" tab. 



I made a new metadata set "my new DC" from Dublin Core.  So it has no complication of the sublevel, all flat.  I specify it with a new "namespace"  ---"mdc" in order to distinguish it from normal dc.  I then use the same procedure as in the coruse website to explode a csv metadata file and it work fine. 



Subtlety with line end in CSV

Sometimes it just gives you a 000001.nul when you explode the csv file. It seems that CSVplugin of greenstone (@2.84) only works well with unix time line end "LR", but not well with Windows type "CR LR". Unfortunately, Excel generates wrong style. One way around is to copy the full sheet from excel to google doc spreadsheet and then "download as" CSV. Google doc seems to produce the CSV Greenstone likes. This csv file should explode fine. Greenstone team needs to work on the CSVplugin.

Thursday, November 10, 2011

Adding Media Player into Greenstone with jPlayer


"jPlayer" is a very nice javascript libraryto play various types of media files in browser using html 5 or flash. It can be integrated into the digital library software Greenstone (2.84) to enable it play various digital files. In this example, we enable the following types:
audio: mp3, m4a, oga
video: mp4(m4v), webmv, ogv, flv
(note: These are not all playable in every browsers due to the inconsistent media-type support across them.)

Let us assume the collection folder is {mycollection}="{greenstone folder}/collect/mycollection"

This example uses jPlayer 2.1.0, Greenstone 2.84.




  1.  Download jPlayer files and expand them into the "{mycollection}/script" folder
    ./add-on/jplayer.playlist.min.js
    ./add-on/jquery.jplayer.inspector.js
    ./Jplayer.swf
    ./jquery.jplayer.min.js
    
  2. Download one of the jPlayer skins (Blue Monday : Pink Flag) and expand all files except the *.psd file into the "{mycollection}/style" folder
    jplayer.pink.flag.css
    jplayer.pink.flag.jpg
    jplayer.pink.flag.seeking.gif
    jplayer.pink.flag.video.play.png
    
  3. Put a new player button image into "{mycollection}/images" folder.  I use this link. A small size one (48px or even 16px) is enough. One can even skip this step, if a "player" button or pure text is preferred.
  4. Create a new javascript file (playMedia.js) with the following text and put it into "{myCollection}/script" folder
    function playMedia(playerId,inspectorId,fullcontainerId,containerId,base,type,src){
    
     $("#"+fullcontainerId).show();
     $("#"+playerId).jPlayer("destroy");
          $("#"+playerId).jPlayer({
            ready: function () {
              $(this).jPlayer("pauseOthers").jPlayer("setMedia",media(type,base+"/"+src)).jPlayer("play");
            },
            errorAlerts:false,
            warnAlerts:false,
            swfPath: base+"/script/",
            supplied: type,
            cssSelectorAncestor: "#"+containerId,
            solution:"html,flash",
            ended:function(){
                
             $("#"+fullcontainerId).hide();
            }
          });
          $("#"+inspectorId).jPlayerInspector({jPlayer:$("#"+playerId)});
    }
    
    function media(type,src){
    
    switch (type.toLowerCase()){
    case "mp3":return {mp3:src}; break;
    case "oga":return {oga:src}; break;
    case "ogv":return {ogv:src}; break;
    case "m4a":return {m4a:src}; break;
    case "webmv":return {webmv:src}; break;
    case "m4v":return {m4v:src}; break;
    case "ogv":return {ogv:src}; break;
    case "flv":return {flv:src}; break;
    default:
    }
    }
    
    
  5. Start GLI of greenstone, open collection, paste the following into the "Collection Specific Macros" in "Format" tab. Alternatively, one can create a file "extra.dm" in "{myCollection}/macros" folder
    
    
    # extra.dm file
    # You can add collection specific macros in here
    # Lines starting with a '#' are comments.
    # Remember to include the package declaration
    
    package Style
    
    # will be applied to all pages 
    # add css style lines inside the style tags
    _collectionspecificstyle_ {
    <style type="text/css">
    div.jp-audio,
    div.jp-video {
      /* Edit the font-size to counteract inherited font sizing.
       * Eg. 1.25em = 1 / 0.8em
       */
      font-size:1.25em;
    \}
    </style>
    }
    
    # add any javascript functions here
    _collectionspecificscript_ { 
    $(document).ready(function(){
          $("#jquery-jplayer-1").jPlayer({
            ready: function () {
              
            \},
            errorAlerts:false,
            swfPath: "_httpcollection_/script/",
            supplied: "mp3,oga,m4a",
            cssSelectorAncestor: '#jp-container-1',
            solution:"html,flash",
            ended:function(){
            $("#jplayer-div").hide();
            \}
          \});
        $("#jplayer-inspector").jPlayerInspector({jPlayer:$("#jquery-jplayer-1")\});
    
    
    $("#jquery-jplayer-video").jPlayer({
            ready: function () {
             
            \},
            errorAlerts:false,
            swfPath: "_httpcollection_/script/",
            supplied: "webmv,ogv,m4v",
            cssSelectorAncestor: '#jp-container-video',
            solution:"html,flash",
            ended:function(){
            $("#jplayer-div-video").hide();
            \}
          \});
        $("#jplayer-inspector-video").jPlayerInspector({jPlayer:$("#jquery-jplayer-video")\});
    
        \});
    
    function play(type,src){
    type=type.toLowerCase();
    switch (type.toLowerCase()){
    case "mp3":
    case "oga":
    case "m4a":$('#jplayer-div-video').hide();
       playMedia("jquery-jplayer-1","jplayer-inspector","jplayer-div","jp-container-1","_httpcollection_",type,src);
       break;
    case "webmv":
    case "m4v":
    case "flv":
    case "ogv":$('#jplayer-div').hide();
       playMedia("jquery-jplayer-video","jplayer-inspector-video","jplayer-div-video","jp-container-video","_httpcollection_",type,src);
       break;
    default:
    \}
    \}
    
    }
    
    
    _pagescriptfileextra_{
    <link href="_httpcollection_/style/jplayer.pink.flag.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
    <script type="text/javascript" src="_httpcollection_/script/jquery.jplayer.min.js"></script>
    <script type="text/javascript" src="_httpcollection_/script/add-on/jquery.jplayer.inspector.js"></script>
    <script type="text/javascript" src="_httpcollection_/script/playMedia.js"></script>
    
    }
    
    
    
    _document:footer_ {
     _jplayer_
    </div> <!-- document:footer -->
    
    <div class="navarrowsbottom">
    _navarrowsbottom_
    </div>
    _endspacer__htmlfooter_
    }
    
    _jplayer_{
    
      <div id="jplayer-div" style="display:none;">
      <div id="jp-container-1" class="jp-audio" ">
    <div id="jquery-jplayer-1" class="jp-jplayer"></div>
      
    <div class="jp-type-single">
          <div class="jp-gui jp-interface">
            <ul class="jp-controls">
              <li><a href="javascript:;" class="jp-play" tabindex="1">play</a></li>
              <li><a href="javascript:;" class="jp-pause" tabindex="1">pause</a></li>
              <li><a href="javascript:;" class="jp-stop" tabindex="1">stop</a></li>
              <li><a href="javascript:;" class="jp-mute" tabindex="1" title="mute">mute</a></li>
              <li><a href="javascript:;" class="jp-unmute" tabindex="1" title="unmute">unmute</a></li>
              <li><a href="javascript:;" class="jp-volume-max" tabindex="1" title="max volume">max volume</a></li>
            </ul>
            <div class="jp-progress">
              <div class="jp-seek-bar">
                <div class="jp-play-bar"></div>
              </div>
            </div>
            <div class="jp-volume-bar">
              <div class="jp-volume-bar-value"></div>
            </div>
            <div class="jp-time-holder">
              <div class="jp-current-time"></div>
              <div class="jp-duration"></div>
              <ul class="jp-toggles">
                <li><a href="javascript:;" class="jp-repeat" tabindex="1" title="repeat">repeat</a></li>
                <li><a href="javascript:;" class="jp-repeat-off" tabindex="1" title="repeat off">repeat off</a></li>
              </ul>
            </div>
          </div>
          
          <div class="jp-no-solution">
            <span>Update Required</span>
            To play the media you will need to either update your browser to a recent version or update your <a href="http://get.adobe.com/flashplayer/" target="\_blank">Flash plugin</a>.
          </div>
        </div>
     
      </div>
      <div id="jplayer-inspector"></div>
    </div>
    
    <div id="jplayer-div-video" style="display:none;">
    <div id="jp-container-video" class="jp-video ">
        <div class="jp-type-single">
          <div id="jquery-jplayer-video" class="jp-jplayer"></div>
          <div class="jp-gui">
            <div class="jp-video-play">
              <a href="javascript:;" class="jp-video-play-icon" tabindex="1">play</a>
            </div>
            <div class="jp-interface">
              <div class="jp-progress">
                <div class="jp-seek-bar">
                  <div class="jp-play-bar"></div>
                </div>
              </div>
              <div class="jp-current-time"></div>
              <div class="jp-duration"></div>
              <div class="jp-controls-holder">
                <ul class="jp-controls">
                  <li><a href="javascript:;" class="jp-play" tabindex="1">play</a></li>
                  <li><a href="javascript:;" class="jp-pause" tabindex="1">pause</a></li>
                  <li><a href="javascript:;" class="jp-stop" tabindex="1">stop</a></li>
                  <li><a href="javascript:;" class="jp-mute" tabindex="1" title="mute">mute</a></li>
                  <li><a href="javascript:;" class="jp-unmute" tabindex="1" title="unmute">unmute</a></li>
                  <li><a href="javascript:;" class="jp-volume-max" tabindex="1" title="max volume">max volume</a></li>
                </ul>
                <div class="jp-volume-bar">
                  <div class="jp-volume-bar-value"></div>
                </div>
                <ul class="jp-toggles">
                  <li><a href="javascript:;" class="jp-full-screen" tabindex="1" title="full screen">full screen</a></li>
                  <li><a href="javascript:;" class="jp-restore-screen" tabindex="1" title="restore screen">restore screen</a></li>
                  <li><a href="javascript:;" class="jp-repeat" tabindex="1" title="repeat">repeat</a></li>
                  <li><a href="javascript:;" class="jp-repeat-off" tabindex="1" title="repeat off">repeat off</a></li>
                </ul>
              </div>
              
            </div>
          </div>
          <div class="jp-no-solution">
            <span>Update Required</span>
            To play the media you will need to either update your browser to a recent version or update your <a href="http://get.adobe.com/flashplayer/" target="\_blank">Flash plugin</a>.
          </div>
        </div>
        
      </div>
      <div id="jplayer-inspector-video"></div>
      </div>
    }
    
    
    
    
    A few notes for macro
    • "}" and "_" have to be escaped as "\_", "\}"
    • Two jPlayer instances and containers are created. One for audio and one for video.
    • A new macro "_jplayer_" is created.
    • One javascript function "play(type,scr)" is created. It plays media files (scr) with jPlayer.
  6. Import media files into Greenstone.  We need to preserve the file format of the media file to call the play(type,scr) function.  Therefore, we need to either extract file format automatically or enter the metadata manually.  "MP3 plugin" can handle mp3 file and create "ex.fileformat" metadata automatically.
     To extract other format automatically, a simple way is to config a new "UnknownPlugin", tick the "file_format" and enter the desired format for play function. Tick the "process_extension" and enter the real file extension (e.g. ogv) . Alternatively, tick "process_exp" and enter the regular express for the filename.  (e.g. m4v)  
    Process_extension File Format
    mp4(video)m4v
    mp4(audio)m4a
    m4vm4v
    m4am4a
    ogg(video)ogv
    ogg(audio)oga
    ogaoga
    ogvogv
    flvflv
    webmwebmv

    One can add some new process_extension if unconventional extensions are used. Due to the above limitation, for "*.mp4" or "*.ogg", one can use either all as video or audio. A mixture does not work.

Monday, August 1, 2011

A very bizarre behavior (bug) of "mv" command

I spend all the afternoon to find this bizarre behavior (bug) of "mv" command.

Let us say we have a directory of "source" with files "a" and "b", and we want to move it to "dest/sub/". So the final should look like "dest/sub/source/...". The command should be simple enough "mv source dest/sub".

Well, this works fine when "dest/sub" exists. When we only have "dest", a sub directory "sub" is automatically created, however, files ("a" and "b") are directly copied over instead of the directory "source".

It is like this in Linux (CentOS5.4) "GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)", as well as Mac OS 10.6 "GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin10.0)".