Wednesday, July 20, 2022

Docker BadBlocks

I was wanting to run badblocks on a couple of external hard drives before shucking, and of course I had to over-complicate it.

I bought a Lenovo ThinkCentre last year for the express purpose of serving as a Docker server, so other than the tutorial, this is the first thing I’m doing with it.

Writing the Dockerfile was easy enough:

FROM alpine:latest
RUN apk add --no-cache e2fsprogs

Build with sudo docker build -t badblocks . (note the period at the end.)

A docker-compose.yml would probably be the correct thing to do, but for now I’m just going with the following command:

sudo docker run -v "/home/matt/bbout:/home/bb" -v "/dev/sda:/dev/sda" --name bbsda -dit badblocks

This runs in detached mode, but lets me dip in and out as I please. I think the proper way would be to just run the badblocks command in the initial command, but that will be in the next iteration.

Per the documentation: 

To stop a container, use CTRL-c. . . If the container was run with -i and -t, you can detach from a container and leave it running using the CTRL-p CTRL-q key sequence.

Start badblocks with sudo badblocks -c 2048 -sw -o /home/bb/sdaout.txt /dev/sda

I’m getting a response “ash: badblocks: not found” when I run as su. I’m sure it’s something dumb.

I appreciate the lightweightness of Alpine, but sometimes it gets to me when I can't even run man badblocks to see if everything is installed like I expect.

A tiny bit more experimentation also gives me the "badblocks: not found" when I install and run in Alpine via WSL rather than Docker. So I guess it's more likely than not that it's an Alpine problem (or more accurately, a PEBAAC (Problem Exists Between Alpine And Chair)) rather than a Docker problem. In some ways this is frustrating, but at least I have a better idea of where to focus my searches.

I'll just keep flailing in the meantime.

Update 2022-07-21

I got it! Alpine has more than just one e2fsprogs package. And as it turns out, badblocks is only found in e2fsprogs-extra.

So now my Dockerfile is as follows:

FROM alpine:latest
RUN apk add --no-cache e2fsprogs-extra

 Build and run using the same commands, and:

badblocks: Operation not permitted while trying to determine device size
Do some more digging, and aha! I need to set the hard drive up as a device, not a volume. I kind of wondered about this at the beginning, and wasn't sure that /dev/sda would be interpreted correctly. So now my command is:

sudo docker run -v "/home/matt/bbout:/home/bb" --device=/dev/sda --name bbsda -dit badblocks

 And yet another issue!

badblocks: Value too large for data type invalid end block (13672382464): must be 32-bit value

 Update 2022-07-23

Seems like the 32-bit limit is not only a known issue, but also intentional design, at least as of 2021.

Anyways, the workaround I found is to just change the command to actually run badblocks to

sudo docker exec bbsda badblocks -b 4096 -c 2048 -sw -o /home/bb/sdaout.txt /dev/sda

Upping the block size to 4096 lets it run! So now I have it running on both hard drives.

If I feel particularly inspired, I may write a simple bash script that lets me just type badblocks.sh [drive name here] and slot in sda/sdb/whatever to the Docker commands. The way I did it this time leaves a lot of room for error on forgetting to change sda to sdb in all the different places.

But hopefully these drives will all run without errors and I can shuck'n'drive them into my NAS and not worry about it for a few years.

Monday, May 16, 2022

How to Sweep Resistance During Simulation in Altium

I wanted to simulate an op-amp circuit with Altium Designer to determine the Vout as I sweep through RTD (Resistance Temperature Detector) values.

"But Matthew! Why not just use LTspice like all of us cool kids?"

Well first off, LTspice has the most baffling shortcut key setup. I'm sure it made sense in the late 80s or something, but in this century I expect Ctrl+C to be "copy." Also, if my MSRP $8,000 piece of software includes simulation support, seems like it ought to work.

Unfortunately, by default, Altium does not offer an easy way to simulate the response of sweeping a resistor or potentiometer. A parameter sweep is offered, but this essentially runs a new simulation for each parameter change, rather than a continuous, interpolated output curve. This would be handy if I wanted to see what happens when I sweep Vref from 0 V to 5 V, with the RTD at a few values from 1 kΩ to 1.887 kΩ, but that's not what I want here.

BUT! After some searching, I discovered that SPICE does support using DC Sweep Analysis to change a resistor value, even though Altium does not show this as an option.

Here's my solution:

  1. Create a dummy voltage source. I called it Vrtd to keep track of it, but it doesn't really matter.
  2. Note the name of your resistor you want to vary. In my example, I cleverly named it "RTD".
  3. Set up a DC Sweep Analysis, using Vrtd as the primary source.
    1. Set this up to sweep from 1k to 1.887k, with a step size of 443.5.
  4. Save all, validate project.
  5. Go to Design > Netlist for Project > Xspice.
  6. Open the resulting .nsx file.
  7. Find the line that says ".DC Vrtd 1000 1887 443.5".
  8. Change this to ".DC RTD 1000 1887 443.5".
  9. Save.
  10. Run the simulation from the .nsx file.

If all goes well, you should see the voltage curve of your desired net plotted with the resistor value along the X axis, and the resulting voltage along the Y axis.

Huzzah!

Sunday, November 8, 2020

Solutions to Common Windows 10 Audio Issues


    I have been doing a fair bit of streaming, Skyping, Zooming, and Discording over the past few months, as I'm sure you have too. As a result, I have involuntarily become a mild expert in getting audio in Windows 10 to (mostly) behave. Here are all the solutions to the problems I have encountered, and I hope it helps you from pulling your hair out.
     
    Nothing here is difficult, it's all just a matter of knowing where to look. 

    This holds true as of 2023-08-30. I will do my best to update as I encounter problems and solutions. Some of these may still work for Windows 11, but I have no desire to upgrade, so I can't tell you which are still relevant.
     

    Where to Find These Settings

    To start us out, most everything will be found in what Windows 10 calls the Sound Control Panel. My friend Brett said it best: "Rule of thumb: if the page looks like it's in the Metro interface, it is full of shit. If it looks like a Win98 menu, you are in the right place."
     
    To get to the Sound Control Menu, first go to Settings > System > Sound (or just right click on the volume adjust icon on the taskbar and click "Open Sound settings").
     
    From here, click Sound Control Panel under Related Settings as shown below. If your window is particularly narrow, you may have to scroll down to find related settings rather than it being a sidebar.
     

    Why Is My Background Audio Quiet All of a Sudden?

    In the Sound Control Panel, click the Communications tab at the top. It will probably oh-so-helpfully be set to reduce the volume of other sounds by 80%. Click "Do nothing" and apply.

     

    Why Are Random Devices Hijacking My Audio?

    (Black and white video) Has this ever happened to you? You plug your shiny new Blue Yeti in to record the premier episode of your podcast, and your headphones stop working! Or maybe you plug in your DualShock 4 to settle in with some Trials, and now nobody can hear you in Discord.

    Basically what happens is you plug your device in via USB, and it decides "I am obviously the most important thing here. Hand over the wheel!"

    This solution is less of an actual fix, and more of a mitigation. But depending on how ruthless you are with disabling your devices, it can turn into a non-issue. 

    First, figure out which devices you'll ever want to use. My desktop setup is pretty permanent, so I know that all I'll ever use for audio input and output is either my Focusrite interface for physical, or Voicemeeter Banana for virtual audio.

    Another common setup might be a Yeti mic, and computer audio for headphones/speakers.
     
    Now disable every other device. This lasts through plugging and unplugging, so you should never have to worry about it again... until Windows updates and re-enables the speakers in your three monitors that you've never once used.

     
     
    For extra neatness I recommend unchecking "Show Disabled Devices."

     
     
    Ahh... doesn't that just make you feel at peace? 

    Why Can't These Two Programs Pick Up My Mic? Or: Why Can I Only Hear Audio From One Program? Or: Why is Discord Changing My Sample Rate Without Permission?

    These problems all seem to stem from allowing programs to take exclusive control of your audio device. Unfortunately you will have to disable this on a device-by-device basis. Fortunately, if you already disabled most of them as explained above, this shouldn't take too long.

    Actually, first make sure your preferred device is set as both the default device and the default communications device. Yes, they can be different. No, I have never found that useful.

    No!

    Yes!

    Now that that's out of the way, click your device and go to Properties, then click on the Advanced tab.


    The "Allow applications to take exclusive control of this device" will probably be checked. Uncheck this box (the other checkbox will automatically disable once you do this). You may have to restart the offending applications or your computer for this to take effect.

    Why Do I Sound Like a Robot? Or: Why Does Everyone Else Sound Like a Robot?

    This tends to be caused by a mismatch between the sampling rate of your audio device and the application, or possibly an audio device buffer overflow. Fixing the sample rate mismatch can be a bit hit or miss (example below). You might be able to set it in the program you're using, or you might have to set the audio device to match the program.
     
    Where to adjust the sample rate can differ depending on your audio device. If it came with any sort of driver software package, that's the first place to look. For my Scarlett interface, the dialog looks like the following:

    For most devices, you can likely adjust it from the Device Properties > Advanced tab as reached in the previous section. Click the dropdown in the Default Format section and select the appropriate sample rate.


    Fixing a buffer overflow is a little easier. The simplest and most aggressive way is to just power cycle your audio device, whether it's unplug and replug your USB mic, or flipping the power switch on your USB audio interface. Sometimes you can do this while your program is running, but probably the most reliable method is to close the important program (Discord, Skype, etc), power cycle, start back up. 

    Why Do I Sound Like a Robot in Discord? 

    Fun fact! Despite the proliferation of prosumer audio equipment boasting sample rates of 96 kHz or more, Discord doesn't support sample rates higher than 48 kHz. Sorry to say, you're going to have to lower your sample rate down to 48 kHz, as explained in the previous section.
     
    This is only half of the solution however. Next, open Discord and go to Settings > Voice & Video. Scroll down until you reach the Audio Subsystem dropdown. Click this dropdown, and change the subsystem from standard to legacy.


    If all goes well, your problems should be solved!
     
    Caveat: sometimes my outgoing Discord audio still glitches out. After doing all of the above, I guess it must be a overflowed buffer, so I still have to occasionally close Discord, restart my interface, and restart Discord.

    Why Does My Mic Sound Blown Out?

    If you're using a separate mic and audio interface, the gain is probably too far up on your audio interface.

    On a USB mic, it's a little trickier. Go to Sound Control > Recording > [your microphone] > Levels. There's a slider that will probably be set to 100. Bring this down to 80 or 60.

    Conclusion

    While this is by no means all-encompassing, I hope this has been helpful! My plan is to update this post as I run into more issues, so please feel free to pass this on, or leave a comment with additional problems or solutions.