Nsound Introduction Video

I’ve made an introduction video for Nsound!

Posted in nsound | Comments Off on Nsound Introduction Video

SWIG C++ & Python: C++ & Python Object Lifetime

I’m currently struggling with a bug in my open source project and it has to do with temporary object lifetimes.  I believe the core issue is that I have a Python object that holds a pointer to an underlying C++ object that’s gotten deleted too early.  I’ve created a toy SWIG project to try to isolate the issue.

The code: swig_test.tar.gz

To build the project on your machine, just unzip and run make:

This will build a Python C++ extension called swig_test and install it into your home directory.

The Buffer class:

The Buffer class is just a container, I’m using operator<< to concatenate data to the end of the Buffer.  This is just a toy example illustrating the problem I’m having with SWIG, my actual class does a whole lot more.

The _data member is just a list of numbers that the container holds, _id is a unique identifier so we can tell which Buffer gets deleted.

The swig_test.i file:

This is about as basic as it can be.

The go_test.py Python script:

Now when running make, the following test script is executed that illustrates the issue I’m running into:

The ‘Deleting Buffer(id = X)’ is being generated from inside the C++ code, so we can see here that in the ‘Funny business’ section, the C++ Buffer objects are getting deleted too early! The Python objects ‘b2’ and ‘b3’ should be holding references to the C++ Buffer objects with id=2 and id=4.

This summarizes the issue I’m having.


Pure Python Implementation

I’ve been searching the web for information to learn what’s going one here. I found this stackoverflow question and though it might be related, so I’ve implemented a pure Python version of Buffer in the script go_test_pure.py, here’s it’s output:

However, everything works as expected. Notice that Buffer ids 2, 4 are deleted at the end, so the pure Python version works as I expect.


Experiment 1: No Destructors

So the first experiment I tried was to explicitly turn off destructors in the SWIG file with %nodefaultdtor and ignoring the C++ destructor:

And here’s the output:

Now the Python objects b2, b3 are pointing to ids 2, 4 and they are valid. But this approach leaks memory, so this is not a solution.


 

Experiment 2: Using A Typemap

My next experiment involves using a typemap like so:

If you were to compare the generated SWIG wrapper code in swig_test.cpp, you will see that this typemap appears to do the right thing, by returning the original PyObject that points to the C++ Buffer object on the left-hand-side of the expression, just like the pure Python method returns self. However, at run-time, a SWIG internal conversion error occurs and the code fails with:


Asking The Community For Help

I’ve run out of ideas and now I need some new eyes to take a look. Hopefully a SWIG expert can quickly point out my error and I can move on with writing code.

So I’ve posted this question to stackoverflow with a link to this blog post. If a solution comes up I’ll be sure to link to it from here.


A Solution!

After some more searching I came across this thread which eventually lead to a working solution. Using a typemap(out) in combination of a Py_INCREF() did the trick.

Now I get the desired behavior with no memory leaks!

Posted in programming | Tagged , , | Comments Off on SWIG C++ & Python: C++ & Python Object Lifetime

How to clone Linux disk of different sizes; large HDD to smaller SSD

WARNING: MISUSE OF THE COMMANDS IN THIS TUTORIAL CAN CAUSE DATA LOSS!
BACKUP YOUR DATA!
YOU’VE BEEN WARNED!


Step 1: Clone original HDD to larger, spare HDD

1.1. Connect SRC and DST disk to computer

1.2. Disconnect external drives that you may be using for backups, you won’t want to accidentally overwrite them!

1.3. Boot off of Ubuntu 14.04 live USB stick or your Ubuntu tower with SRC and DST connected

1.4. Use gparted to identify your boot OS, original SRC, and DST disk device ids, for example:

Write these down! If you mix these up you will LOSE ALL YOU DATA! Using all different sized disks makes this easier. If you have multiple disks of the same size, connect one disk at a time and label them. You don’t want to make a mistake here.

1.5. Clone SRC disk to DST disk using the pv & dd utilities (pv is available in the Ubuntu “universe” apt-get repo):

where:

for example on my system:

This may take several hours depending on your disk sizes, using pipe view (pv) will give you an estimate for how long it will take.

1.6. Power off your computer after the disk is cloned, the Linux kernel may not read the newly cloned disk correctly.


Step 2: Resize Cloned HDD

2.1. After powering off, disconnect the original drive to keep it safe from modification

2.2. Boot computer again, use gparted to resize and move all partitions so that they will fit on the new SSD

You can launch gparted with the DST device id to operate on just that disk, for example:

NOTE: gparted should display all partitions on the new disk without any warnings like /!\ next to a partition. All partitions should be readable by gparted with partition size and used listed next to each partition. If you are booting off a live USB stick, you will probably need to turn off the swap partition by right-clicking and selecting swapoff in the menu.

2.3. After resizing everything power off the computer


Step 3: Clone HDD To New SSD

3.1. With the power off, connect the new SSD to the computer

3.2. Boot computer with the resized HDD and the new SSD

3.3. Use gparted to identify your boot OS, original SRC, and DST disk device ids, for example:

Write these down! If you mix these up you will LOSE ALL YOU DATA!

3.4. Clone SRC disk to DST disk using the pv & dd utilities:

where:

for example on my system:

This may take several hours depending on your disk sizes, using pipe view (pv) will give you an estimate for how long it will take.

Since the destination SSD is likely smaller than the source HDD, dd will error out at the end stating the destination disk is out of space:
dd: error writing ‘/dev/sdc’: No space left on device
This is OKAY.

3.5. Power off the computer and disconnect the spare HDD, so moving forward we are only modifying the SDD


Step 4: Repair The Partition Table

The original partition table that got cloned to the SSD is now incorrect, the physical size of the disk has changed, we need to repair it. With Ubuntu 14.04, the partition table being used is something called a GPT. We will repair the partition using the tool gdisk.

4.1. Boot the computer without the spare HDD

4.2. Use gparted to identify your boot OS, and the new SSD device ids, for example:

Write these down! If you mix these up you will LOSE ALL YOU DATA!

4.3. Use gdisk to repair the GUID Partition Table (GPT):

4.4. Reboot the computer so the Linux kernel sees the new partition table


Step 5: Repair Boot Image

After all the work of resizing the partitions and repairing the partition table, the GRUB boot image is likely non functional. From my experience the BIOS can try to boot from the disk but we are left with a single ‘-‘ blinking character and nothing happens. The second stage boot loader can’t be found. Here we’ll repair it.

5.1. Boot computer

5.2. Use cgdisk to create a tiny partition to install grub2 on:

5.3. Select the free space at the beginning of the disk to create the new partition, it MUST be the first partition on the disk:


Step 6. Install Grub2

Now that we have a dedicated grub boot partition, we can proceed to install grub2 on the disk.

6.1. Mount the data partition disk for chroot operations:

6.2. Change root into mounted disk:

6.3. Reinstall grub2, recheck, recreate menu:

6.4. Exit the chroot shell and unmount everything:

6.5. Shutdown the computer and install the SSD into the new computer, it should now boot!

Posted in computing | Tagged | Comments Off on How to clone Linux disk of different sizes; large HDD to smaller SSD

Nsound Intro & Realtime Demo

I’ve decided to start bloging about Nsound on weegreenblobbie.com since I haven’t been really using this space for anything else.

Nsound is a C++ library with a Python module that makes it easy to manipulate sound.  Here’s the project’s homepage, misspellings and all.

I’ve created two mailing lists so if you’re interested you can join here:

nsound-users@lists.sourceforge.net
nsound-developers@lists.sourceforge.net

I’ve posted an introductory video about Nsound on youtube:

Posted in nsound | Tagged | Comments Off on Nsound Intro & Realtime Demo

How To Use Macros And Blender

I wanted a way to perform a “Repeat Last Action” command in Blender.  I found an easy way to do it on Ubuntu Linux with a little tool called xmacro.  Xmacro is a simple keyboard and mouse recording utility.  It will record keystrokes that can be played back at a later time.  It is quite old but it works with a little help.

Step 1. Install Xmacro

$ sudo apt-get install xmacro

Step 2. Install Nick’s Helper Scripts

Save these two scripts somewhere on your Linux box.

record_macro.bash:

#!/usr/bin/env bash

ESC_KEY_CODE=9

# A delay is needed to ensure all keys are released that
# activate the macro prior to recording.

sleep 1.0

# Press escape to stop recording the macro.
xmacrorec2 -k $ESC_KEY_CODE | tee /tmp/temp_xmacro.txt

play_macro.bash:

#!/usr/bin/env bash

# WARNING, a delay is needed to ensure all keys are released
# that activate the macro playback, if a key is still being
# held on the keyboard, who knows what key combinations will
# play on your desktop!

sleep 0.5

# A loop is used to control the playback speed.

while read line; do

        echo $line | xmacroplay $DISPLAY

        sleep 0.005  # the delay between events

done < /tmp/temp_xmacro.txt

Step 3. Assign Macro Scripts To The Keyboard

In Gnome, open up the keyboard shortcuts by selecting: System –> Preferences –> Keyboard Shortcuts

Click on the Add button at the bottom.

Name: Record Macro

Command: /install_path/record_macro.bash

where install_path is the location you saved the helper scripts.

Next, assign a keyboard shortcut to “Record Macro”, I use CTRL+ALT+M

Click on the Add button again.

Name: Play Macro

Command: /install_path/play_macro.bash

where install_path is the location you saved the helper scripts.

Next, assign a keyboard shortcut to “Play Macro”, I use CTRL+ALT+P

Step 4. Try It Out In Blender

Open Blender, with the default cube open, enter edit mode.

We’ll record extruding two verticies.

Select two verticies.

Now press CTL+ALT+M to start macro recording.

Press e

Press RETURN

Press x

Press 1.0

Press RETURN

Press ESCAPE

Pressing ESCAPE will end macro recording.

Now repeat the last action by pressing CTR+ALT+P.  Make sure you release the keys on the keyboard while the macro plays!

Enjoy!

-Nick

Posted in computing | Tagged | Comments Off on How To Use Macros And Blender