tisdag 24 november 2015

Fix audacity startup bug - on ubuntu 15.10

For a while ago audacity stopped working. It hung on startup, and i tried to fix the problem in a lot of different ways.

I searched for the problem online and realised that there was some problem with the version that was distributed with ubuntu, that it was built to the wrong version of wx-widgets.

One obvious solution (for a linux user) was to try to compile the application from source. But for some reason i ended up with strange compilation errors. I tried for the code for version 2.0.6, 2.1.1 or 2.1.2 but all ended with some dependencies missing that was not possible to install from the ubuntu repositories.

The final solution that i found thas this repository that i found online. This is a newer compiled version than the one that i tried

sudo add-apt-repository ppa:ubuntuhandbook1/audacity
sudo apt-get update
sudo apt-get install audacity

...



Then audacity finaly started...

söndag 21 juni 2015

Add native code to Android Studio project.


I felt a bit lost when switching from eclipse to the Android Studio environment. Also i found it difficult to interpret the documentations and tutorials i found online that tried to explain how to setup android studio projects. After struggling for a while i decided to learn to compile a project with the terminal first to get a grip around what was happening in the background, and then the pieces started to fall in place.

First of i added the gradle and the ndk-build to the PATH variable. The path variable is a environment variable (both in Linux and Windows) that tells the environment which programs can be executed. Here is a bit discussion that i found about it. You fix it by adding som lines in your ~/.bashrc file. This makes it possible to be able to run grandle without specifying the path on every call. I did the same for ndk-build that is located in the native sdk folder.

Create a project

If you do not already have a project select File → New → New Project... and create a project that suits for you. Alternative you can import an eclipse project by selecting File → New → Import project... Some things will differ, as i will mention in the text.

Add a jni folder to your project.

In your project explorer (probably on your left) right-click on "app" then go to New → Folder → New JNI Folder ... Select it and create a folder. Note that if you are in Android-mode (the text "android" is visible above the explorer) it might work different, but den just press the drop down menu (with the text android) and select "project".

Include library files into your project

Android studio will automatically call ndk-build in the jni-folder, so what you will have to do is to simply include your library into your code. The fastest way I found is to simply add this to your app/build.gradle

build.gradle

android {
  ... //old code
    sourceSets.main {
        jni.srcDirs = [] //This prevents the auto generation of Android.mk

        jniLibs.srcDir 'src/main/libs' //this is not necesary unless you have precompiled libraries in your project
  }
}


You could do a bit more fancy stuff to match to different types of processors, but that will do for now.

If you have imported from a eclipse project you probably have a part in your grandle script that says "ndk {... }". I use to remove this statement because otherwise grandle will complain about duplicate libraries. An other alternative is to remove the jniLibs.srcDir statement above, it seems to have a similar effect effect.

I have also noticed that when importing from an eclipse project, Android Studio puts the old files in app/jniLib/...  This caused a error message for me that confused me at first, but the solution was to simply remove the old copied so-files (the ones in app/jniLib) because new ones will be generated anyway.

Create some source files

Because the ndk-build 

Lazy way to discover function names


You need to come up with a name that reflect the native functions. However if you are a bet lazy, like me you could just run the program, without implementing the right functions and then look for a error message like this in logcat.

java.lang.UnsatisfiedLinkError: No implementation found for void se.laserskold.mattias.android1.MainActivity.test() (tried Java_se_laserskold_mattias_android1_MainActivity_test and Java_se_laserskold_mattias_android1_MainActivity_test__)

then you know what to call your function.


Here is some more good resources
http://ph0b.com/android-studio-gradle-and-ndk-integration/

Git subtree

This blogpost describes the process of setting up a git subtree with a base repositories and one ore more subrepositories. One of the benefits of using subtree instead of gitmodules is that the subtree is checked out automatically with the main repository.

First of all. Make sure to have your sub-repository initialized and ready. If you don't have one, open a terminal, go to your projects root folder and run

git init


Add your subtree project


git subtree add --prefix matgui git@github.com:mls-m5/matgui.git master --squash

where the --squash parameter makes sure that you only checkout a shallow copy of the subrepository

One thing i noticed when doing this was that you need to commit or stash all your changes before adding the subtree so that you have a clean repository to work with.


Save the repository to be able to update it later on

git remote add -f matgui git@github.com:mls-m5/matgui.git

This will add a reference to the place where you got the code from

If you want to pull down changes to your project you then just have to use

git pull -s subtree matgui master

References

måndag 3 november 2014

Dvorak for my OSX machine

After quite a while i got my hands on a OSX, machine.. The first problem was of course me getting frustrated for not being able to write properly on it. Fortunately the process of changing keyboard layout on OSX is more or less straight forward.

1: (Optional) find a layout that you like to start from (i found a starting point here)
2: Download Ukelele (that is not a typo) 
3: Save the layout in ~/Library/Keyboard Layouts/
4: Open keyboard settings and add the new layout





Varför skriver jag ens det här på Engelska. Finns det någon som inte förstår svenska som kommer att läsa det här.

Här är en länk till layouten som jag använder

Aja.. Hoppas att det fungerar bra

torsdag 12 juni 2014

Build native application for android with stl-support

Once a while you want to use your c++ code on your android device. Since i don't like the process of looking into references and manuals all the time i prefer to use the built in tools in the android-sdk to make it all a bit leaner. So this is how to do it quick and simple in eclipse.

I do this on my ubuntu computer but many of the steps are generic for all operating systems (as long as you have eclipse, android-sdk and ndk installed.

I write specifically about the stl-stuff (the std::string, std::list, std::vector and so on) because they tend to be a bit tricky to get to work, I if you dont want to use them feel free to skip them. I have noted when it is possible to skip code.

Make sure you have your environment set up

The following steps requires you to have the android sdk (in it eclipse is included) and the ndk (used for native/c++ support). You might also have to install the cdt for c++ functionality in eclipse.

Create the project.

In eclipse, create a ordinary android project, and create some kind of standard activity if you want to quickly get some test output

Add native support to the project

The simplest way i found to do this is to right click on the project in the project explorer, goto android tools → Add native support 


Then enter a nice name for your library like "ndktest" as I will use here

You will then have a directory called jni, filled with one file called something with cpp, like ndktest.cpp and one Android.mk. I will not use the cpp-file but, just let it be there, it feels safe :)

Automatic builds

I use to make sure to enable automatic build, to be sure everything is up to date when i change things. To do this go to project properties. Select C/C++ build → Behaviour and check the "Build on resource save"

Connect it together

This is how my native class file could look like anyway. Any class can contain native functions. I use to keep it in a separate class to automatic generate the c function definitions.


 //NativeClass.java:  
 package com.experiment.ndktest;  
 public class NativeClass {  
      static native void test(); //This is our native function. Create as many as you like  
      static { //This is loading our native library at startup time  
           System.loadLibrary("gnustl_shared"); //must be first, skip this if you're not using stl-stuff  
           System.loadLibrary("ndktest"); //this is your library  
      }  
 }  

For some devices (like my older phone) you need to explicitly link to the stl, and you need to link to it first. Otherwise you will have the unsatisfied link exception. For some newer devices you will not have to do this at all, but if you are planning to have a large user base it can be a good idea (This confused me for a while when I got the unsatisfied link exception on some devices but not on others)


Generate header file (or write them your-self)

I will use javah-program to generate my header files. more specifically in this example i would run

javah -jni -classpath ../bin/classes/ com.experiment.ndktest.NativeClass

while I'm in the jni-path of the project. This will generate a file called com_experiment_ndktest_NativeClass.h in the directory i am currently in. (This may be linux specific) You can also write the function definitions yourself. Then you will have to make sure to know the form of how it should look.

For this example I will have the file (plus some comments I just wrote)

 //com_experiment_ndktest_NativeClass.h  
 /* DO NOT EDIT THIS FILE - it is machine generated */  
 #include  
 /* Header for class com_experiment_ndktest_NativeClass */  
 #ifndef _Included_com_experiment_ndktest_NativeClass //standard include block  
 #define _Included_com_experiment_ndktest_NativeClass  
 #ifdef __cplusplus //no avoid name mangling  
 extern "C" {  
 #endif  
 /*  
  * Class:   com_experiment_ndktest_NativeClass  
  * Method:  test  
  * Signature: ()V  
  */  
 JNIEXPORT void JNICALL Java_com_experiment_ndktest_NativeClass_test  
  (JNIEnv *, jclass);  //the function definition  
 #ifdef __cplusplus //end of name mangling-handling :)  
 }  
 #endif  
 #endif  


There is some things to take note of:
1. The extern "C" statement is used to not have trouble with name mangling. Java is using c-names and not c++-ones (which are not the same). If you miss this when you write your own, you will probably have unsatisfied link exeption

2. The look of the function... well that is how it looks....

Create a source file

You can write the function declarations in the auto-generated source-file. However i prefer to create a source file named the same as the header file to make use of eclipses source tools to automatically create function definitions, in this case com_experiment_ndktest_NativeClass.cpp.

When done a file like that could look like this

 #include "com_experiment_ndktest_NativeClass.h"  
 #include <string>  
 #include <android/log.h>  
 std::string apa = "bepa"; //stl-stuff  
 void Java_com_experiment_ndktest_NativeClass_test(JNIEnv*, jclass) {  
      apa = "cepa";  
      int x = 0;  
      auto y = x + 1; //c++11 stuff  
      __android_log_write(ANDROID_LOG_INFO, "ndktest", "hej");  
      __android_log_print(ANDROID_LOG_INFO, "ndktest", "x = %d", x);  
 }  



That would not do anything special but just use som stl-stuff (string) and then write to logcat (the debug output)

The Android.mk

The Android.mk-file describes how to build the native library that you are using. When using the "Add native support" function mentioned above we will have it automatically generated for us. However we will have to change it a bit for our needs. This is how it will look like

 #Android.mk  
 LOCAL_PATH := $(call my-dir)  
 include $(CLEAR_VARS)  
 LOCAL_MODULE  := ndktest #the name of the library  
 LOCAL_SRC_FILES := NDKTest.cpp com_experiment_ndktest_NativeClass.cpp #our new file  
 LOCAL_LDLIBS  += -llog #For logcat output. Can also be skipped if you're not using it  
 LOCAL_CPPFLAGS := -std=c++11 #to be able to use c++11 stuff. You can skip this if you're not  
 include $(BUILD_SHARED_LIBRARY)  

The Application.mk

To use the stl-library you must refer to it in the Application.mk. Note that it is not the same as the Android.mk. But it should be created in the jni directory. If you are not using stl-you will probably not need this file

#Application.mk:APP_STL:=gnustl_shared

now in my jni directory i have

Android.mk
Application.mk
com_experiment_ndktest_NativeClass.cpp
com_experiment_ndktest_NativeClass.h
NDKTest.cpp

Finally - Call the native functions from java

Now we can use the NativeClass in java to call our native functions how much we like. For example I could put a call in the onCreate of the main activity of the project to check if everything is working


//MainActivity.java
...
public class MainActivity extends ActionBarActivity {  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
           if (savedInstanceState == null) {  
                getSupportFragmentManager().beginTransaction()  
                          .add(R.id.container, new PlaceholderFragment()).commit();  
           } 
           //End of generated code
           NativeClass.test();  
      }
...


Extra - Add headers for Eclipse syntax checking

For Eclipse not to report non existing problems because of "missing headers" for stl-elements you need to add the android headers to the project. To do this. Open project properties. Go to "paths and symbols". Add the following paths to the include directories.

 some/path/to/android-ndk-r9c/sources/cxx-stl/stlport/stlport

For the rest of the needed includes it will hopefully be creating when adding native library through Eclipse

And now probably it will work as it should... cheers


onsdag 11 juni 2014

Finding maximum correction for three samples

I always recalculate this when I need it, but now it is time to save it somewere.


You have three samples y1 y2 y3. and you want to know there the maximum is, if you do a quadratic regression from it. We here assume that the distance between the points in the x-axis is 1.

First, lets do the quadratic regression
We describe the graph as y = ax² + by + c

So we want the values of a, b and c

for y2 = y(x = 0) we have
y2 = a·0 + b·0 + c → 
c = y2

We then have that 
y1 = y(-1) = a·(-1)² + b·(-1) + c →
y1 = a - b + c →   (c = y2)
y1 = a - b + y2

and also in a similar fashion 
y3 = y(1) = a·1² + b·1 + c →
y3 = a + b + y2

Therefore we can take
y3 - y1 = 2b →
b = (y3 - y1) / 2

and
y1 + y3 = 2a + 2c = 2a + 2y2 →
2a = y1 + y3 - 2y2 →
a = (y1 + y3)/2 - y2


To get the maximum of the graph, we just have to find the zero of the derivative y'.
y = ax² + bx + c →
y' = 2ax + b

0 = y'
0 = 2ax + b
0 = 2((y1 + y3) / 2 + y2)·x + (y3 - y1)/2
2((y1 + y3) / 2 - y2)·x = -(y3 - y1)/2
(y1 + y3 - 2y2)·x = (y1 - y3)/2
x =  (y1 - y3) / (y1 + y3 - 2y2) / 2

...Done once for all.


torsdag 14 november 2013

Fixing strange apt-get update error in xubuntu


I got this strange error when trying to do apt-get update on my xubuntu:

W:Failed to fetch http://archive.ubuntu.com/ubuntu/dists/saucy/Release Unable to find expected entry 'universedeb/source/Sources' in Release file (Wrong sources.list entry or malformed file)


Or in swedish:
Hämtade 13,0 kB på 17s (750 B/s)
W: Misslyckades med att hämta http://archive.ubuntu.com/ubuntu/dists/saucy/Release  Kunde inte hitta förväntad post "universedeb/source/Sources" i Release-filen (Felaktig post i sources.list eller felformulerad fil)

E: Några indexfiler gick inte att hämta. De kommer att ignoreras eller så används de gamla istället.

So after searching the web: i got the idea to inactivate random repositories in my /etc/apt/sources.list file.

I found that by deactivating part of this section this :
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://archive.ubuntu.com/ubuntu saucy universe
deb-src http://archive.ubuntu.com/ubuntu saucy universedeb http://se.archive.ubuntu.com/ubuntu/ saucy-updates universe
deb-src http://archive.ubuntu.com/ubuntu saucy-updates universe


by commenting (putting # before)
deb-src http://se.archive.ubuntu.com/ubuntu/ saucy universedeb http://se.archive.ubuntu.com/ubuntu/ saucy-updates universe
The problem disappeared.

Still dont krow what's behind the strange error.. but now I can at least use my computer as normal.