Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6785373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:07:08+00:00 2026-05-26T17:07:08+00:00

I have a list of bytes that represent raw samples read in from an

  • 0

I have a list of bytes that represent raw samples read in from an audio interface. Depending on the use case and H/W, each sample can be anywhere from 1 to 4 bytes long, and the total number of channels in the “stream” can be more or less arbitrary. The amount of channels and bits per sample are both known at runtime.

I’ll give an example of what I mean. There are four channels in the stream and each sample is two bytes.

List(A1, A2, B1, B2, C1, C2, D1, D2, A3, A4, B3, B4, C3, C4, D3, D4)

so A1 is the first byte of channel A‘s first sample, A2 is the second byte of the same sample and so on.

What I need to do is extract each channel’s samples into their own lists, like this:

List(List(A1, A2, A3, A4), List(B1, B2, B3, B4), List(C1, C2, C3, C4), List(D1, D2, D3, D4))

How would I go about doing this in idiomatic Scala? I just started learning Scala a few hours ago, and the only non-imperative solution I’ve come up with is clearly nonoptimal:

def uninterleave(samples: Array[Byte], numChannels: Int, bytesPerSample: Int) = {
val dropAmount = numChannels * bytesPerSample
  def extractChannel(n: Int) = {
    def extrInner(in: Seq[Byte], acc: Seq[Byte]): Seq[Byte] = {
      if(in == List()) acc
      else extrInner(in.drop(dropAmount), in.take(bytesPerSample) ++ acc)
    }
    extrInner(samples.drop(n * bytesPerSample), Nil)
  }

  for(i <- 0 until numChannels) yield extractChannel(i)
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:07:09+00:00Added an answer on May 26, 2026 at 5:07 pm

    I would do

    samples.grouped(bytesPerSample).grouped(numChannels).toList
      .transpose.map(_.flatten)
    

    I would not vouch for its performance though. I would rather avoid lists, unfortunately grouped produces them.

    Maybe

    samples.grouped(bytesPerSample).map(_.toArray)
      .grouped(numChannels).map(_.toArray)
      .toArray
      .transpose
      .map(flatten)
    

    Still, lots of lists.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Python script that read a list of path names from a
I have a List that I'm adding 3 bytes to one of which is
i have to read a sequence of bytes,that was written in different ways (writeBite,
i have list of rows that user select and i want to delete them,
I have a jpeg file that is being held as a list(of Byte) Currently
I have a page in JSP that list some file that could be downloaded
I have a hash table that I want to store to disk. The list
I have a list of numbers that i would like to send out onto
I have a list of character range restrictions that I need to check a
I have list of files stored inside arraylist that I need to download in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.