Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Tuesday, October 27, 2015

Casio SA-10 + FPGA = Audio Synthesis

Hello there!

First, yes, I'm alive... You just need to know where to look for (Hint: Flickr or dA).

Second, no, this won't be a long post.

Third, new project!
https://github.com/zebarnabe/music-keyboard-vhdl/

Regarding this last point, I'm building an audio synthesizer from my dead Casio SA-10 (non-destructive modifications, might be able to repair it in the future) and my old Digilent Nexys 2 FPGA board...

So far, so good... It works as a proof of concept, though I need to make it properly since to ensure polyphony I instantiated 32 basic synthesizers instead of properly serialize the processes ... processing everything at the same time is always fun in a FPGA.

Currently taken logic is at 10%, I guess I can make it a lot better by serializing the output mixer alone... or lower the precision... 144kHz sampling with 16bits and 32 channels is a bit overkill, specially when the PWM controller only does 8 bits.

Tuesday, March 8, 2011

Super Scribble (EDITED: 10/03/2011 v2)

EDIT:
You can download the latest package installer .sis file from here:
Downloads - super-scribble - Project Hosting on Google Code

If you have a Nokia with a touchscreen and a Symbian OS and ever tried Python I'm sure you know Scribble, a small sample application that shows Python touchscreen interaction by providing a simple doodling application.

One of the things you may have noticed it, it's the limited feature set, you have an eraser and a pencil, you also have a color picker from where you can pick a few colors, thankfully you can save the doodles or notes you make on it.

There are other applications for Symbian touch devices that deliver some similar features, some allow for zoom and a better color picker, but nothing else...

Here are some of the doodles I did on scribble's unmodified state:

As you may have noticed, they are only B&W pictures, this is because rarely any of the colors provided are truly useful.
I've used scribble for quite some time and this week I spent my time practicing Python and PyS60 API.

I messed up with pretty much everything on Scribble's source code, so I added a number of features to it, namely mix brush, smudge tool, opacity, RGB sliders to pick color and brush color history. All this while trying to maintain the performance and smoothness of the application.

With those changes I manage to do these doodles in little time:

Of course there is still room for improvement, like the ability to open and edit images (and by consequence the support for bigger images and removal of the white stripe on the bottom of the images)

You can download the application (sis and python source) from here (tested in Nokia 5800 XpressMusic, should work in N97, 5530, 5230, X6 or any other Symbian touch device that has PyS60 2.0.0 installed and working)

I'll perhaps post a video later explaining what each of the interface areas does... If you're going to try it, you should at least know that touching the red bordered rectangle on the will pick the rectangle fill color for the brush.

Have fun!

PS:
R.I.P. Nokia, if you didn't know Nokia it's now a Microsoft's partner and while Nokia isn't dead, Symbian S60 5th and S^3 will kinda meet a dead end, S40 will continue to survive in lower end devices where nokia still have a valuable market share.

EDIT (10/03/2011):
I improved it (currently version 0.6.x) but didn't released it yet - probably will release a video together with the new version (that will probably be version 0.7.x).

v0.6.0 features the following additions:
  • Support for any size of drawing (as long as the memory handles it)
  • Support for Zoom
  • Move tool (allows one to move the canvas by dragging it)
  • Color picker tool, picks a tool from canvas to use in brush

Stay tuned for updates :]

Tuesday, November 16, 2010

Line of code of the day #5

Another LOCOTD.... just for me to remember those nice *cough cough* code lines that i come around from time to time.... i'm currently working as a trainee (until February), i'm currently implementing a custom featured content management system (CMS), i'm using php, mySQL, xhtml, css and a bit of JS for some slideshows... but this LOCOTD is about SQL ...
Store Procedures (SPs), as you may not know, are supported by mySQL for quite some time, however not quite perfectly as you might expect.... On SPs, you basically build a wrap box around one or more SQL statements, you can set variables, use the usual SQL stuff.... in my case i was using a SELECT, like i did for the rest of the dozens of SPs i did before, i was implementing a page like feature in the CMS, for that LIMIT clause is god send, but guess what.... in SPs you can't use non-constant integers in LIMIT parameters... yes.... you can use the arguments of the SP anywhere, except on the LIMIT clause...
So ... "How to solve it?" that would be the question... one way would be using a prepared statement, like this:
CREATE PROCEDURE `fooTable`.`sp_fooTableList` ( limit_arg int ) 
BEGIN
SET @sql = concat('SELECT * FROM `fooTable` LIMIT ', limit_arg); 
PREPARE STMT FROM @sql;
EXECUTE stmt; 
END

An obvious problem with this, is that it broken the extra security provided by SPs... but it is a valid workaround.... quite simple as well.... but because this prepared statement kinda defeat the purpose of having a nicely wrapped SQL statement, i found a way of doing it without recurring to it, i say found and not invented, credit goes to Gert Brigsted, it's easy to follow and understand how and why it works: 
CREATE PROCEDURE `fooDB`.`sp_fooTableList` ( limit_start, limit_end )
BEGIN
SET @rownum:=0;
SELECT *
FROM ( 
  SELECT ( @rownum:=@rownum+1) AS Rownumber, `fooTable`.* 
  FROM `fooTable` 
  ORDER BY `fooTable`.`mooCol` DESC
) AS t
WHERE Rownumber > limit_start AND Rownumber <= limit_end;
END

It looks messy, but it works... sure, it has some impact on performance, but that's the price to pay for security... until LIMIT clause accepts variables as parameters.

Perhaps you are thinking, "That surely is, or will be, fixed in current iterations of mySQL" ... you are right it is, in versions 5.5.6 and 6.0.14, it is 'fixed' ... But not all servers allow you to change mySQL version installed... so, I guess you may run into this problem and have to use one of the above workarounds...
As a side note, this was submitted as bug #11918 by 13 July 2005 and 'solved' by 24 September 2010 .... more than 5 years to 'fix' this 'bug' ... i use quotes because wasn't exactly a bug but more like a non-feature...
Hope it helps someone out there bashing at "ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near: s_limit, e_limit;"

Thursday, June 18, 2009

Line of code of the day #4

Weee... another LOCOTD ... (no ... that doesn't mean Crazy Tower Defence) ... today i present here the following line of code: 
solution[i] = solution[j] | (~0)&(1<<z)

The most familiarized with C and pointers will notice that if i is not related in bounds with j value (or vice-versa) something really bad will happen with memory access/write... the lovely usual segmentation fault or worse... the line should have look like this:
solution[j] = solution[j] | (~0)&(1<<z)

Or in a more compact form:
solution[j] |= (~0)&(1<<z)

I lost 3h to find where the error on my code was... you see, when instead of j the i was there I was writing in some area, that area was where a pointer inside a struct was, at first I though the error was in functions that manipulate variables of that struct... when I noticed something very strange when I switch some entries on struct... it looked like something impossible to happen, but after I've done it some stuff worked, so struct control looked fine... only answer would me that I was modifying struct data unknowing it... after a bunch of printf's (like... everywhere) I tracked the error down... cost me a lot of time... and now I 'only' have to finish some algorithmic stuff that is implemented, polish some of the code and optimize some memory accesses...

Well... good coding... i hope you have better luck than me...

Tuesday, June 9, 2009

Line of code of the day #3

So... remember that little problem i had to 'compact' selected bits? i think i solved it... By using a very special look-up table i manage to do the trick using this 'easy' to read line:
(( f[(m & 0xF0) | (val & 0xF0) >> 4] >> 3) >> (f[(m & 0x0F)<< 4 | (val & 0x0f )] >> 3 ) & 0xF)

Oh, this code is just for 8bits, a simple expansion allows me to use it for 32bits (with look-up table taking up 2Mb)... and no... I won't explain what is in f table has... and yes... I might screw up with the parentesis... I didn't compile that piece of code, but tested it manually instead... it should work... efficiency wise it's better that scan all bits one by one...

You can now commit suicide.

PS: By reading this you agree that i'm not responsible for any harm occoured both physically or mentally when reading the text above... really...

Tuesday, June 2, 2009

Line of code of the day #2

Who would guess its another LOCOTD, this time we have some C code to show... solution[batch] = solution[batch] | ~((polar_0[batch] ^ polar_1[batch])&polar_0[batch]) | ((polar_0[batch] ^ polar_1[batch])&polar_1[batch]); In case you're wondering ... yes i could load the values of polar_0 and polar_1 on index batch, but since i don't know what kind of effect that will have on performance, i'll try both and see... compilers and assemblers do magic sometimes... :p I leave the moment to show you some magic ... long live to SWAR Algorithms! Jokes aside, using general CPU instructions to make some crazy highly efficient operations is always good, while it is wise to check such approaches also keep moderation in mind, you can end up with some brain damage... Now... a little puzzle for anyone who is willing to take it: I have an array of 32-bit masks, that in reality represents a very huge mask for a very huge matrix, now ... i want to grab those bits masked (the number of masked bits is known) and pack them all in another array, all of that must be done fast, since speed is what we want. I leave a little example for 8-bit masks, 4 length array: mask 01111000 01001001 10101110 10111100 masked 01101011 10100101 11101110 11010010 result 00000000 00000001 10100111 11110100 I do not have a (eficient) solution for this problem (yet - i kinda have an idea), i hope to find one since this is kinda crucial to optimize the speed of the rest of the algorithm. With that said, feel free to comment and give sugestions about it... Good coding...

Tuesday, April 28, 2009

Line of code of the day

Hmmm ... you know... sometimes you create pieces of code that look like crap but do wonderful things and very fast... so i'll paste a line of a VHDL architecture module... now imagine that 90% of the rest of the module is similar... yes... a nightmare to debug... div_buffer <= (NUM_VARS-LOG2_NUM_VARS-1 downto 1 => '0')&((offset-(stack(top)(NUM_VARS+LOG2_NUM_VARS-2 downto NUM_VARS)&'0'))&'0'); But work! I might clean up the code later ... anyway, this calculates a divider for a bit vector calculation ... in software takes around 2*N clock cycles to calculate the bit vector, in hardware takes N*log(N) clock cycles... i would say my implementation is efficient .. XD In other news, my pen tablet and gimp (or should i say GTK+?) are always punching eachother... i found a way of solving the issues ... reboot! ... This only happens in windows Vista... so... yeah ... Vista is not only sluggish, slow and bad... is even worse ... specially since my pen tablet says "Certified for windows Vista" ... go figure!

Tuesday, March 24, 2009

VHDL considerations...

While programming in VHDL can't be considered a very easy deal, there are certain things on it that are very easy to do... i lost 2 days with one of those things...

Making a crash course into VHDL sintax (and assuming that you know how a computer works in hardware terms), you control bits in vhdl... yes... bits... the simplest think in computers world... of course that it isn't very easy to do complex things of a thing so simple, but anyone understands that shifting bits is just picking a bunch of them and putting them a little bit aside.

In VHDL you can create arrays - vectors - of bits, and you can access each bit or a pack of them by indexing them, sounds scary, but it's very easy actually, one with no knowledge in programming should have no problem doing a static shifter in vhdl.

To index bits, you need to 'create' them... well ... there is a lot of mambo-jambo in VHDL for doing declalations, and while i mastered most of VHDL sintax i still fail in the most basic stuff (well, i'm always learning from mistakes but it doesn't seem enough xD).

Well... lets say you have a array of bits, better saying a vector, in vhdl it is called std_logic_vector, and it's simple a vector of std_logic elements.. std_logic elements are the representation of a signal value in hardware (well, kinda) they can be 1, 0 or other ugly stuff you really don't wanna know...

Ok... we have our vector puppy in a signal, this should be human readable (or i'm loosing my human side):

signal puppy : std_logic_vector (31 downto 0);

So puppy has 32 bits ordered like 31 down to 0... how nice...

if you want a bit from puppy, you just do like this:

puppy(3)

And that is bit number 3... it's 4th bit if you count from right, since there is a bit 0... easy to get right?

Now harder! >:D you want... the 8 first bits... that will be hard, right?

puppy(7 downto 0)

Damn... was too easy... well... this is being great but grabbing bits without interact with them is a bit... worthless... well... you have a nice operator for concatenate bits! So now we can make our shifter... to concatenate bits you just use & operator like so:

'0'&"0000001"

So ... easy to get ('0' is a bit with zero value .... and "0000001" is a vector of bits) ... so 8 bits with the least significant bit (if you consider the right the least significant bit) at '1'

Now ... how to shift? You should get it now without any help, let's shift our puppy 8 bits to right!

"00000000"&puppy(31 downto 8)

Easy... right? I mean... you couldn't fail that... no way.... right? I did fail at doing that... and lost 2 days of work >:(

I wanted to shift one bit to right, i did like this (taking puppy as an example):

'0' & accu(30 downto 0)

That simply replaces the last bit with '0' ... >___>''

PS: Sorry for the length of this post... 

Friday, February 27, 2009

Monday, February 23, 2009

FPGA - Nexys2

While was not today, the Nexys2 from digilent has finally arrived, i spent the weekend playing a bit with it, it is a quite neat package, i had already the latest Xilinx ISE Webpack installed, so was just a matter of reading the reference manual, set the pins and start testing stuff. Just a tip: If you adquired one of these, you may feel some trouble in using the segment display, to activate an anode you must use zero value there and to activate a catode (that controls each segment independently) you must also use zero value there. Also, buttons and segment displays are inversed, so ... button zero is at right and segment display controled by anode zero is at left, so if you're using buttons to do some action on the display above them take this in consideration. The demo/test provided gave me some trouble to compile in ISE 10 ... it was made for version 8, eitherway source code of it provides a nice tutorial in how to use VGA port and PS/2 for a mouse. If i get time i shall show you some demos of it running crazy stuff i made to test some code (an Hello World running into segment displays.. scrolling of course, 4 displays aren't enough for 11 chars) I also leave some links that might be useful if you didn't already knew it: fpga4fun (digilent site has changed)

Thursday, January 8, 2009

RTSS@2007 CiberMouse

Ok .... i made a ciberMouse within RTSS2007 rules, if you don't know what it is ... is simple... imagine a mouse in a labyrinth, there are more mouses there, they all want the cheese and to come back to their home, but the mice have a problem, they are kinda blind, have 4 really bad eyes (one eye is on their back - don't ask!) and they only sniff the cheese at 120º on they front, to make. things worse they can only sense 2 things at the same time and they take a while to sense the cheese, and some walls don't let cheese be sensed.... oh... they also carry a compass that takes a while to view the way north is... This is, of course, the metaphore for this contest... kinda... xD ... well i think an image explains it better, so here it is, the final state of a run, my mouse is the pink... the other ones are the samples one that come with simulator... So... i won the run... like ... i went to cheese and come back without hit anything... sure, took a while ...is kinda funny to see how near i come to the start point (mouses have 2 engines that have error and inertia associated when energy applied) so is not easy for such thing to happen... Well ... it's 6:10am ... i think i go take some rest...

Friday, December 19, 2008

Exaustion...

So... i just delivered my assignment... i lost 3 weeks to nothing i guess... is just like a huge amount of code that does a lot of stuff in background, but it does little to be shown to user, so my guess is that i'll have a negative grade or something - teachers usually only look to the interface - go figure... Oh! And teacher requested us to do an webpage to show the work we have done... yeah... he can go to a certain place i know if he thinks we have time to do it.... Well, even if teacher allow to proceed the development in case of negative grade i won't do it, cos i simple have no time: i have to finish another work 'till 10th january (this includes a report and quite some code) and to study to finals (2 classes), not to forget my thesis (that is barelly started), so.... i'll just forget the pratical side of this class, and just study for the finals and do the other work. Yeah.. while unlikely, i'm angry and exausted both physically and mentally... so sorry if my english is not the best .... and sorry for rant about this ... but is better than yell it out loud.... That's all.... i'll just take some - very long - vacations over .NET and C#... now ... i only look to VHDL, C and javascript - they are like my puppies x3, VHDL to have stuff done in hardware, C to do just the stuff i like for most of the stuff, and JS to prototype algorithms or just mess around with browsers... >__>'' good old times.... Damn! I can't even draw now... my tablet is dead and i don't think i'll get a new one so soon.... *sigh* sorry again for the rant... bare with me while i yell at my problems ...

Thursday, December 11, 2008

C# a quanto obrigas...

Sim, claro ... após 600 linhas de codigo para fazer um servidor não há muito que possa estar mal além de uns typos ... certo? .... errado... estaria certo se estivesse a falar de C .... o velhinho C .... mas com o C# aquilo que parecia estar certo pode estar errado ... muito errado... (e de notar que este problema aqui descrito nem foi o pior ... apenas o mais simples de entender). Vejamos como exemplo o seguinte pedaço de codigo: foreach (ClientInfo client in clientList) { if (clientSocket == client.socket) { client.state = ClientInfo.State.READY; } if (client.state == ClientInfo.State.READY) { msgToSend.current++; } } onde clientList é um ArrayList da struct ClientInfo (podia ser um List mas havia problemas semelhantes, mesmo tentando com clientList[index].state).... e onde está o problema? simples, dentro de um foreach podem apenas ler .... yep .... aprendi isso da pior forma.... ou seja, após ter finalizado toda a implementação e testado tudo o resto ... eu definitivamente não me dou bem com OO... Parece que não fui o unico (mais detalhado)... >.< ... bem problema resolvido ... struct pra class ... hora de implementar o cliente ... (definitivamente não vou acabar isto a horas)

Wednesday, December 10, 2008

.NET Framework

While the development of the application for CM classes has stalled due to extreme complexity, i did not stop searching solutions to simplify the application (i don't think i'll finnish the application on time anyway). Regarding network comunication using .NET Framework i found two very nice samples that may provide some help, i share my findings here with you. I hope to post my own solution in the future (or just failing epically as i usually do in high-level languages). Samples are both in C#. Links: A Chat Application Using Asynchronous TCP Sockets A Chat Application Using Asynchronous UDP sockets

Sunday, December 7, 2008

DataSet.WriteXml

Let's talk about .NET Framework (Compact Framework to be more specific) .... again... After spent several hours trying to put my XML file control class to work properly i finnally had some success. When i saw the methods WriteXml and ReadXml for DataSet object i said to myself: "Zeca take a note here... this is pretty usefull!"... only for after a few minutes of coding bash to find out that it didn't quite work like i wanted... things is quite simple to be explained, at least the problem, i check if file exists (Sistem.IO.File.Exists(filepath) - or similar), if not, create a new one with default data on it, then return the data, if file exists, use ReadXML on DataSet and get the data... well ... this works pretty well, ok, now i loaded data into some forms (a settings form) then modify some and exit the form to main menu, when i exit i store the changes on DataSet, and do a WriteXml at the end, simple right? Well it works .... until you do the drill a second time.... it reads correctly, it calls WriteXml at exit, it seems to save the changes .... but it doesn't! Now my solution: Don't use the same DataSet! Create a new one, and yes i tried commitChanges methods before trying this solution... in the end this was the only thing that work. I don't know why this happens, anyway it could be a file open mode problem - when reading the information the file is openned in read mode, and later when writing on it, DataSet.WriteXML uses the same file handler to do the job... failing silently... If anyone had the same problem or know what i was/am doing wrong please fell free to comment it out. Thanks in advance. PS: I really start to hate ALL object oriented languages - not all object-able languages

Saturday, December 6, 2008

.NET Compact Framework experiences

Today i made my first custom control for .NET Compact Framework, nothing too fancy and mostly was copy-paste from msdn website. Well, it does the job and is compatible with Visual Studio interactive designer (that was the tricky part actually). A few days ago i made some experiences with sound on managed code, i found out an awsome class for playing .wav files, the API is just amazingly simple. System.Media.SoundPlay is it's name, you can find all info about it on msdn library, it allows syncronous or assyncronous playback of .wav files, but please remember that .wav files must be encoded in PCM and that big .wav files means huge resources being allocated, so don't just put that 10min studio quality audio pcm file and expect the handheld to be all fine, but is quite useful for little sound effects. Adding to that i adapted a class i found on the internet to manage XML files in such a way that can make them behave as small databases for persistency on the device.

Saturday, April 26, 2008

Action Script 3...

Ok ... hoje resolvi ver o que tinha mudado no action script, a mudança de AS1 para AS2 foi algo pacifica pra mim, mas quando exprimentei AS3, tive algumas dificuldades. Tudo se deve ao simples facto de o paradigma de programação ter sido renovado, agora em vez de por o codigo nos objectos, isso é feito em runtime, o melhor exemplo é mesmo os butões, antes pra associar uma acção a um botão, o mais facil seria simplesmente selecionar o butão, carregar F9, por o codigo e já tá, agora... bem agora não é dificil, apenas diferente, pra criar um butão é só fazer uma função que receba um objecto do tipo Event ( function teste(event:Event) ) despejar o codigo desejado para o butão, e associar um EventHandler ao butão com a função ( butao.addEventHandler(MouseEvent.Click, teste) ) e voilá, butão a funcionar... Bem... tal como disse anteriormente eu não sou muito dedicado a blogging ... mas espero que isto tenha ajudado alguém de algum modo.