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 7954829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:33:00+00:00 2026-06-04T03:33:00+00:00

I try to output some sound with Scala. My problem is that i get

  • 0

I try to output some sound with Scala. My problem is that i get a short “noise”/”click” every second. I didn’t had this problem with a similar java program. Has somebody an idea what is wrong?

Scala 2.9.2
java 1.6.0_31
OS X 10.7.3

import javax.sound.sampled._

object SinSoundMain extends App {
  val SAMPLE_RATE = 44100
  val SAMPLE_SIZE = 16
  val CHANNELS = 1
  val SIGNED = true
  val BIG_ENDIAN = true
  var format = new AudioFormat(SAMPLE_RATE, SAMPLE_SIZE, CHANNELS, SIGNED, BIG_ENDIAN)

  var info = new DataLine.Info(classOf[SourceDataLine], format);

  val auline = (AudioSystem.getLine(info)).asInstanceOf[SourceDataLine]
  auline.open(format)
  auline.start

  val start = System.currentTimeMillis()

  // play 10s
  while(System.currentTimeMillis() < (start + 10000)) {
    var index = 0

    // output blocks of 10000 samples
    var samples = 0.until(10000).map {x =>  math.sin((x+index) * 800.0 / 44100 * math.Pi)}

    // convert samples to Byte Array
    var byteSamples:Array[Byte] = samples.flatMap{ s => 
      val ss = (s * Short.MaxValue).toShort
      List((ss >> 8).toByte, (ss & 0xFF).toByte)
    }.toArray

    auline.write(byteSamples, 0, byteSamples.length)
  }

  // cleanup      
  auline.drain
  auline.close
}
  • 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-06-04T03:33:05+00:00Added an answer on June 4, 2026 at 3:33 am

    The error in your code is that var index = 0 should be before the start of the while-loop, and you should have index += 10000 at the end of the loop (inside). Do this and it sounds fine.

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

Sidebar

Related Questions

I've installed gtk2hs-buildtools with no problem, but here's the output when I try to
When I try to output some data into a text file using FasterCSV, sometimes
I want to be able to get some output from mysql stored procedure within
I am writing a code-generator that will need to output some miniscule portions of
When I try to output an ASCII value to a file, with some characters
hello guys i am try to print the output of two element data simultaneously
I need to read the output from ffmpeg in order to even try the
Try as I might I cannot get my head around what the IteratorIterator class
Try as I might, I can't get a JNLP file to run locally (via
try: spam.foo except AttributeError: do_somthing() (Is it wise to check an attribute like that

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.