<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1576988655277442038</id><updated>2011-07-29T01:51:59.764-07:00</updated><title type='text'>Aik's Curious Programming</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://aiks-curious-programming.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1576988655277442038/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://aiks-curious-programming.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Aik</name><uri>http://www.blogger.com/profile/15595247316818310663</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>4</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1576988655277442038.post-5242076021523783196</id><published>2009-06-10T20:12:00.000-07:00</published><updated>2009-06-10T20:31:21.967-07:00</updated><title type='text'>Oops, yeah, sound mixing tricks</title><content type='html'>Hehehe, so yeah, I got a bit behind... Anyway...&lt;br /&gt;&lt;br /&gt;So as we know, sound is mixed with addition, yeah? Yeah. So the most simple loop would be something like...&lt;br /&gt;&lt;br /&gt;int Divdn = Channel.Freq&lt;&lt;24 / MasterFreq;&lt;br /&gt;int Delta = Channel.Delta;&lt;br /&gt;s8  *Src0 = Channel.Source;&lt;br /&gt;s16 *Dst0 = LeftChannel; //Interleaved data as RRRRLLLL&lt;br /&gt;s16 *Dst1 = Dst1+1;&lt;br /&gt;do {&lt;br /&gt;    *Dst0++ += *Src0*LeftVol  / 128;&lt;br /&gt;    *Dst1++ += *Src0*RightVol / 128;&lt;br /&gt;   &lt;br /&gt;    Delta += Divdn;&lt;br /&gt;    if(Delta&gt;&gt;24) {&lt;br /&gt;        Src0 += Delta&gt;&gt;24;&lt;br /&gt;        Delta &amp;amp;= ~0xFF000000;&lt;br /&gt;        if(Src0 &gt;= Channel.End) Src0 = Channel.Loop;&lt;br /&gt;    }&lt;br /&gt;} while(--Smp &gt; 0);&lt;br /&gt;&lt;br /&gt;Ok, sure, it's simple and it works, but not as fast as it can be. So what do we do? We combine the left/right multiplications and stores into a single s32 pointer like this...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int Divdn = Channel.Freq&lt;&lt;24 / MasterFreq;&lt;br /&gt;int Delta = Channel.Delta;&lt;br /&gt;s8  *Src0 = Channel.Source;&lt;br /&gt;s32 *Dst0 = LeftChannel;&lt;br /&gt;u32 Volu  = ((RightVol+1)&lt;&lt;16) + LeftVol+1; //Could be 0 that stuffs up the next trick&lt;br /&gt;do {&lt;br /&gt;    s32 Value = Volu * *Src0;&lt;br /&gt;    if(Value &gt; 0) Value &amp;amp;= ~0x7F0000; //Clear bits&lt;br /&gt;    if(Value &lt; 0) Value |=  0x7F0000; //Fill sign&lt;br /&gt;    //A smart enough compiler should optimize it to...&lt;br /&gt;    //muls  value, volue, srcdat&lt;br /&gt;    //bicgt value, value, #0x7F0000&lt;br /&gt;    //orrlt value, value, #0x7F0000&lt;br /&gt;   &lt;br /&gt;    *Dst0++ += Value / 128;&lt;br /&gt;   &lt;br /&gt;    Delta += Divdn;&lt;br /&gt;    if(Delta&gt;&gt;24) {&lt;br /&gt;        Src0 += Delta&gt;&gt;24;&lt;br /&gt;        Delta &amp;amp;= ~0xFF000000;&lt;br /&gt;        if(Src0 &gt;= Channel.End) Src0 = Channel.Loop;&lt;br /&gt;    }&lt;br /&gt;} while(--Smp &gt; 0);&lt;br /&gt;&lt;br /&gt;So what started off as 2 multiplications, 2 adds and 2 loads/stores, has been transformed to 1 multiplication, 2 sign checks, 1 add, 1 load/store.&lt;br /&gt;&lt;br /&gt;The next optimization is just optimized unrolling with LDMIA/STMIA pairs but that's about it.. Another optimization would probably be to have different loops for looped samples/1 shot samples. But there's not much optimization after that. Unless you want to do .12 fixed-point rather than .24 and loading for every sample, regardless of if it changed. If you really want to get the best out of it, you could go to ARM ASM, which I've done and have managed to speed up things radically.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1576988655277442038-5242076021523783196?l=aiks-curious-programming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aiks-curious-programming.blogspot.com/feeds/5242076021523783196/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aiks-curious-programming.blogspot.com/2009/06/oops-yeah-sound-mixing-tricks.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1576988655277442038/posts/default/5242076021523783196'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1576988655277442038/posts/default/5242076021523783196'/><link rel='alternate' type='text/html' href='http://aiks-curious-programming.blogspot.com/2009/06/oops-yeah-sound-mixing-tricks.html' title='Oops, yeah, sound mixing tricks'/><author><name>Aik</name><uri>http://www.blogger.com/profile/15595247316818310663</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1576988655277442038.post-6315205573228661306</id><published>2009-06-06T22:24:00.001-07:00</published><updated>2009-06-06T22:35:12.000-07:00</updated><title type='text'>"Big maps" on a console (such as AGB)</title><content type='html'>&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;Well, I was going to do this, but BlogSpot screwed up my stuff, so tomorrow, I'll post some sound mixing tricks. Hopefully they won't screw up.. &gt;&lt;'&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1576988655277442038-6315205573228661306?l=aiks-curious-programming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aiks-curious-programming.blogspot.com/feeds/6315205573228661306/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aiks-curious-programming.blogspot.com/2009/06/big-maps-on-console-such-as-agb.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1576988655277442038/posts/default/6315205573228661306'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1576988655277442038/posts/default/6315205573228661306'/><link rel='alternate' type='text/html' href='http://aiks-curious-programming.blogspot.com/2009/06/big-maps-on-console-such-as-agb.html' title='&quot;Big maps&quot; on a console (such as AGB)'/><author><name>Aik</name><uri>http://www.blogger.com/profile/15595247316818310663</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1576988655277442038.post-7992679351840418067</id><published>2009-06-03T22:55:00.001-07:00</published><updated>2009-06-04T00:01:00.493-07:00</updated><title type='text'>Sound clipping</title><content type='html'>&lt;span class="Apple-style-span"  style="font-family:'trebuchet ms';"&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;So yeah.. as promised, I will (attempt) to explain how sound clipping works, a (slightly) faster solution and a faster solution yet (though not as safe). However, since compilers get silly over good code, we'll use ARM ASM and 32-bits per register here.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;So... the most common way to clip, say, 16-bit sound data is...&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ Assume r0 is 16-bit&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;cmp   r0, #32767 @ Is r0 greater than 32767?&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;movgt r0, #32767 @ \ Yes, clip&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;cmn   r0, #32768 @ Is r0 less than -32768?&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;mvnlt r0, #32767 @ \ Yes, clip&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;Yup, that works cos it's the most logical way: If it's greater than the maximum allowed, then clip up, otherwise if it's less than the minimum, clip down.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;Sure, it's good, but not the best performance-wise, so we'll look at how overflow works in terms of bits...&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ +32767 (note the left-bit is 0)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;01111111 11111111&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ -32768 (note the left-bit is 1)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;10000000 00000000&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;00000000 01111111 @ 127&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;00000000 00000001 @ + 1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;----------------- @ ---&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;00000000 10000000 @ 128; no overflow&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;                  @ Well, yeah, in&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;                  @ s8 there is but&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;                  @ this is 16-bits&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;01111111 00000000 @ 32512&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;00000001 00000000 @ + 256&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;----------------- @ -----&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;10000000 00000000 @ 32768; overflow&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;How do we know? Take a look at the left-most bit and the summary above: the left-most bit has become 1, thereby signifying that it is no longer 32768, but rather &lt;span class="Apple-style-span" style="font-style: italic;"&gt;negative&lt;/span&gt; 32768!&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;"So does that mean that 01111111 11111111 is positive, but 10000000 00000000 is negative?"&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;Yes, those are the limits of 16-bit. "But that's just lim(short)+1!" Yes. Yes it is, which it why we can come up with THIS trick...&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;movs   r1, r0, asr #15&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;mvnmis r1, r1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;subne  r0, r2, r0, asr #31&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;"@__@ ... QUE?!?!?!" Thought as much. :P Therefore, I will comment this code and then explain...&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ r2 is lim(short) (32767)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ Fetch the "bad" bits&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;movs   r1, r0, asr #15&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ If it was negative, flip&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ all bits for the next trick,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ as negatives are sign-extended&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;mvnmis r1, r1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ Here's the neat part: If we had&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ any bits left in r1, that means&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ we overflowed, as these are&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ considered "bad" bits. And as we&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ saw before, lim(short)+1 is the&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ minimum number 16-bit can form,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ and since ASR sign extends, ASR 31&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ would simply fill the register&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ the "real" sign, which would make&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ it either 0 or -1 for positive and&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ negative respectively. And as we&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ learn in math...&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ x - (-1) = x + 1,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ x - ( 0) = x - 0&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ Therefore, replace x with the limit&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ of 16-bits and we get...&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ 32767 - (-1) = 32767 + 1 = 32768,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ which is the lowest number for 16&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ bits, and if it was positive, then&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ it will turn into 32767 as can be&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ seen above.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;subne  r0, r2, r0, asr #31&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style=" ;font-size:13px;"&gt;Neat, huh? But wait! There's &lt;span class="Apple-style-span" style="font-style: italic;"&gt;another&lt;/span&gt; trick!&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;Since sound data *usually* won't go past +17FFFh (98304), we can take advantage of the "16-bit sign" and the "32-bit sign" and the neat instruction known as XOR (eor in ARM ASM) and do this..&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ r1 is lim(short) (32767)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ XOR turns a bit on if the bits of&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ the 2 values were different, so...&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ 00000000 00000000 01111111 11111111@ (32767)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ 01111111 11111111 00000000 00000000@^(32767&lt;&lt;16)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ -----------------------------------@------------&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ &lt;span class="Apple-style-span" style="color: rgb(255, 0, 0);"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;0&lt;/span&gt;&lt;/span&gt;1111111 11111111 01111111 11111111@No overflow&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style=" ;font-family:'courier new';"&gt;                                     @ CPSR n=0&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ And for an overflowing case...&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ 00000000 00000000 10000001 11111111@ (33279)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ 10000001 11111111 00000000 00000000@^(33279&lt;&lt;16)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ -----------------------------------@------------&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ &lt;span class="Apple-style-span" style="color: rgb(255, 0, 0);"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;1&lt;/span&gt;&lt;/span&gt;0000001 00000000 10000001 11111111@Overflow&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;                                     @ CPSR n=1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;teq   r0, r0, lsl #16&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ Same trick as above; different&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;@ condition since n=1 on overflow&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;submi r0, r1, r0, asr #31&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;Be careful with this one, though: If you know the values are going to use the 15th bit on overflow, then use the 3 instruction one.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-size:small;"&gt;Well, there we are: A full blog post dedicated to sound clipping and stuffs. Hope you guys liked it. :)&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1576988655277442038-7992679351840418067?l=aiks-curious-programming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aiks-curious-programming.blogspot.com/feeds/7992679351840418067/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aiks-curious-programming.blogspot.com/2009/06/sound-clipping.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1576988655277442038/posts/default/7992679351840418067'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1576988655277442038/posts/default/7992679351840418067'/><link rel='alternate' type='text/html' href='http://aiks-curious-programming.blogspot.com/2009/06/sound-clipping.html' title='Sound clipping'/><author><name>Aik</name><uri>http://www.blogger.com/profile/15595247316818310663</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1576988655277442038.post-9101438430974858743</id><published>2009-06-03T22:52:00.000-07:00</published><updated>2009-06-03T22:54:47.634-07:00</updated><title type='text'>Yay, first post!!!</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: 'trebuchet ms';"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;YAY!! YAY!!!!! YAYYYYY!!!!!!!!!!!1111111111111111ONE...&lt;/span&gt;&lt;/span&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'trebuchet ms'; font-size: 13px;"&gt;Ahem... Sorry about that, got carried away, there. :P&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'trebuchet ms'; font-size: 13px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'trebuchet ms'; font-size: 13px;"&gt;So... This blog is about me and my curious little adventures through code, which include silly optimizations, STRANGE optimizations and stuff that will probably just bloat your code rather than help (hopefully I'll see it in time instead of making an a** out of myself. :P).&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'trebuchet ms'; font-size: 13px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'trebuchet ms'; font-size: 13px;"&gt;So yeah..! Next post up: The dreaded sound clipping! *gasp*&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1576988655277442038-9101438430974858743?l=aiks-curious-programming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://aiks-curious-programming.blogspot.com/feeds/9101438430974858743/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://aiks-curious-programming.blogspot.com/2009/06/yay-first-post.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1576988655277442038/posts/default/9101438430974858743'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1576988655277442038/posts/default/9101438430974858743'/><link rel='alternate' type='text/html' href='http://aiks-curious-programming.blogspot.com/2009/06/yay-first-post.html' title='Yay, first post!!!'/><author><name>Aik</name><uri>http://www.blogger.com/profile/15595247316818310663</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
